Skip to content

Conversation

@skirtles-code
Copy link
Contributor

The current example in the Relative redirecting section of the docs seems misleading:

const routes = [
  {
    // will always redirect /users/123/posts to /users/123/profile
    path: '/users/:id/posts',
    redirect: to => {
      // the function receives the target route as the argument
      // a relative location doesn't start with `/`
      // or { path: 'profile'}
      return 'profile'
    },
  },
]

Returning the value 'profile' won't necessarily yield a path of /users/123/profile. A relative path is resolved relative the current active route, not the to route.

When this was first pointed out to me I thought it was a bug, but based on #1902 it seems it's considered correct. The reproduction provided in #1902 is almost identical to the example in the docs - I'm not sure whether posva was aware of that when he closed that issue.

Perhaps this is something that should be changed in the next major version? I'm not sure how the current behaviour would be useful in practice, having the redirect resolve relative to the to route seems much more useful.

This example in the docs comes from #1090, which updated an earlier example. From reading through that PR I'm not entirely clear what the original intent was.

I think we have 3 options here to fix the docs (short of a major rewrite):

  1. We could delete the Relative redirecting section. Using a function for redirect is already covered further up, so we wouldn't be losing much.
  2. Tweak the existing example so that it actually works as described. It would no longer be using a 'relative redirect' in the strictest sense, but it would show how to achieve the desired outcome.
  3. Come up with a better example and an actual explanation of the current behaviour for redirect. I'm reluctant to go with this option because (as I noted above) this feels like an undesirable feature to me and it's easier to change it if it isn't explicitly documented.

This PR implements option 2, but I'm happy to switch to one of the other options if they seem preferable.

For testing purposes, I've put together a playground based on this example:

@netlify
Copy link

netlify bot commented Mar 17, 2025

Deploy Preview for vue-router ready!

Name Link
🔨 Latest commit 429eae3
🔍 Latest deploy log https://app.netlify.com/sites/vue-router/deploys/67d79d2c46f0da0008f3cbdd
😎 Deploy Preview https://deploy-preview-2479--vue-router.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 96 (🔴 down 1 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! This documentation is indeed a leftover of the past one and wasn't updated properly

@posva posva merged commit f4289d3 into vuejs:main Mar 17, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants