koajs / koa

Expressive middleware for node.js using ES2017 async functions
https://koajs.com
MIT License
35.15k stars 3.23k forks source link

redirect 'back' does not work #1266

Closed cviewsupport closed 5 years ago

cviewsupport commented 5 years ago

Hello, right now my code redirects to a specific url when the form is submitted: this.redirect('/clients');

Instead of that I want it to redirect to the previous page when the form is submitted. I tried this but it does not work. this.redirect('back');

When I submit that form it just stays on that same page.

Any suggestions?

fl0w commented 5 years ago

What version of Koa are you using, v1?

fl0w commented 5 years ago

Actually, some abbreviated src would be appreciated. The special cased back is tested and is passing without issues, I'm assuming there's something wrong in your application code rather.

cviewsupport commented 5 years ago

Version = koa 1.2.0

fl0w commented 5 years ago

Then I would recommend you to upgrade to latest Koa 1.6.x (which is the latest 1.x version).

cviewsupport commented 5 years ago

Here is just a portion of the code. This is working in production with no issues. I can change the redirect from /cameras to any other url and it works fine. The problem is that I cannot get it to just go back to the previous page.

` try {

yield Camera.update(this.params.id, this.request.body);

// return to list of cameras
this.redirect('/cameras');

} catch (e) {
// stay on same page to report error (with current filled fi    elds)
this.flash = { formdata: this.request.body, _error: e.message };
this.redirect(this.url);
}

};`

cviewsupport commented 5 years ago

Instead of: this.redirect('/cameras');

I need it to go back to the previous page like this. this.redirect('back');

But when I do that it just submits the data then stays on that same page.

cviewsupport commented 5 years ago

I am trying to avoid upgrading koa because this is a fairly big app that is in production. If I upgrade it... I am sure a lot of stuff will break.. :)

fl0w commented 5 years ago

Well, 1.2 has a vulnerability from fresh, RE DoS. You should consider it either way. There shouldn't be any breaking changes from 1.2 to 1.6.

Anyway I just tested it, and it works in 1.2. I assume you're doing a form POST or something, which could explain things.

So, the redirect occurs from A -(POST)> B -> A (but you probably want to go to the page before A). There's nothing wrong with the redirect. All 'back' does is grab url from Referrer, set a Location headerfield and responds.

cviewsupport commented 5 years ago

Yes on page "A" there is a form post. When I submit the form it just stays on page "A" when I use 'back'

Is there a way to get it to go back to the page before "A"?

Maybe something like: window.history.back()

cviewsupport commented 5 years ago

Thanks for the info on the upgrade. I will give it a try on the dev server... Hopefully the whole app does not break. :)

fl0w commented 5 years ago

Yea, there's probably ways to achieve that, like keeping a record from session or manipulating window.history if you're doing a SPA. There's better forums to ask that question in though.