-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Detecting redirects #1822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Solution (in entry-server.js): import { createApp } from './app';
export default context => new Promise((resolve, reject) => {
const { app, router, store } = createApp();
console.log(router.resolve(context.url).route); // object has redirectedFrom property
router.push(context.url);
router.onReady(() => { /* ... */ }, reject);
}); |
In case this helps anyone, after some digging I found that luckily when you push/replace a route, there is a vue-router/src/history/base.js Line 128 in d539788
On the server-side, in your entry-server, you can add router and fullPath to
That part doesn't handle the case of a redirect the app makes after loading data and trying to render. To address that, back inside the server/express side where you call
Hope this helps! And if there is a better way, let me know. |
@joenoon What do you mean by your solution? Caz if your url is not the same,
it would directly reject the req and would not do the redirect any longer. As far as I am concerned, It should be solved in the |
@NoraGithub its a bit different case. the The second one is harder, where async data is fetched, and a component ends up reacting to the data and decides to redirect instead. For example, when a user profile component is loaded, the component fetches the user but discovers the user no longer exists. The component does something like Your component still likely rendered something, like an empty |
Line 32 in effb114
To me this should not be a feature request but it's rather a documentation issue. With the hints from the comments above there could be a nice how-to. |
in Vue Router 3 |
What problem does this feature solve?
With vue-router 1.x, it was possible to see where a route had been redirected from using
router.currentRoute.redirectedFrom
. This was useful when server-side rendering, as you could send a 301 from the server instead of sending the HTML for the new page and then changing the URL on the client.With 2.x, the property has gone and I can't find it anywhere. Has it been removed? If so, it would be great if it could come back!
Reason it was nice that it existed: #968
What does the proposed API look like?
router.currentRoute.redirectedFrom === '/old-path'
The text was updated successfully, but these errors were encountered: