https://codesandbox.io/s/8yl9852pv9
pages/fail.vue
with:<template>
<div>
FAIL
</div>
</template>
pages/redirect.vue
with:<script>
export default {
asyncData({ redirect }) {
redirect("https://elg.no");
}
};
</script>
pages/index.vue
with:<template>
<nuxt-lint to="/redirect">Navigate to external page</nuxt-link>
</template>
Return to the index page where link was clicked from.
Navigates to /fail page
What happens is, when navigating in SPA mode, the code that handles redirect() call is using location.replace
to navigate to external URL. The problem is that that happens while URL is still set to /
so it replaces that navigation entry with external URL. Navigating back thus goes back to page visited before /
which is unexpected.
The solution would be to either allow the URL to change /redirect
and then trigger location.replace
or use location.assign
instead. Former is probably more risky as it would trigger extra routing events and potentially some more code that could possibly fail.
This is only an issue with redirects to external URLs.
Non-ideal workaround would be to trigger redirect from a short timeout. Not ideal because the redirect.vue
page will render before redirecting and that looks ugly.
And of course there is always an option of not using nuxt-link for those URLs but that is not always possible to control.
Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:
Issues that are labeled as 馃晲Pending
will not be automatically marked as stale.
reproducible
Redirect function now is useless in Nuxt.js, because of this bug. Any updates?
Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:
Issues that are labeled as 馃晲Pending
will not be automatically marked as stale.
go away stale bot
Have there been any issues opened for this? Is there a fix?
@rchl I know this is an old issue but it seems history it not working at all in this example?
When clicking Go to next page
I'm also unable to return to index using browser back button.
@TimvdEijnden You need to export to zip from codesandbox.
Or maybe try again. I've updated everything to latest versions.
@rchl Use middleware
instead of asyncData
:
<template>
<h1>Redirecting...</h1>
</template>
<script>
export default {
middleware ({ redirect }) {
return redirect('https://elg.no');
},
}
</script>
But yeah, still the same problem :(
Most helpful comment
go away stale bot