vue-router icon indicating copy to clipboard operation
vue-router copied to clipboard

A redirect with an alias keeps the original url

Open callumacrae opened this issue 8 years ago • 9 comments

Version

2.7.0

Reproduction link

http://jsfiddle.net/9r6xhqbp/37/

Steps to reproduce

Add the following to your router:

  {
    path: '/view',
    alias: '/view/*',
    redirect: '/analytics',
  },

Navigate to /view/blabla.

What is expected?

I'd expect to be redirected to /analytics

What is actually happening?

/view/* is now an alias for /analytics, where I would have expected it to redirect instead.

/view redirects as expected.

callumacrae avatar Sep 22 '17 13:09 callumacrae

Oh and for anyone else experiencing this (unlikely, it seems pretty specific), you can work around it by just adding two entries to your router:

  {
    path: '/view',
    redirect: '/analytics',
  },
  {
    path: '/view/*',
    redirect: '/analytics',
  },

callumacrae avatar Sep 22 '17 13:09 callumacrae

Callum please, take the time for the repro and instructions 😉

posva avatar Sep 22 '17 14:09 posva

I figured it would be quicker to add that block of code into any existing project using vue-router (and change the value of redirect:) than to clone a git repo and run npm install 😛 will set up a repo now

callumacrae avatar Sep 22 '17 14:09 callumacrae

You don't need a repo for that, use a fiddle: http://jsfiddle.net/posva/9r6xhqbp/

posva avatar Sep 22 '17 14:09 posva

that's easier 😄 added it to the main issue: http://jsfiddle.net/9r6xhqbp/37/

callumacrae avatar Sep 22 '17 15:09 callumacrae

Thanks, as you can imagine, the workaround is to use two routes, one for every redirect

posva avatar Sep 23 '17 07:09 posva

On second thought, this is working as expected, because the point of alias is to keep the original url, so I prefer documenting this and keeping current behaviour which is more flexible

posva avatar Sep 23 '17 07:09 posva

I suppose redirect should take a higher priority here.

User story:

  1. User navigates to /view/*
  2. Router finds out /view/* is an alias and it should keep the original url while following other rules defined under /view
  3. Router finds a redirect under /view
  4. Router performs redirection to /analytics

Since /analytics is totally a different thing from /view, so the expected behavior of the alias(i.e. keep the original url) is out of scope here, hence /analytics in the url bar and $route.path.

ping @posva

nandin-borjigin avatar Jan 29 '19 07:01 nandin-borjigin

~~For that scenario, use multiple redirects but currently, the situation allows to keep the alias, I don't want to remove that~~

Revisiting this, I think it makes more sense for the redirect to take precedence

posva avatar Mar 26 '19 18:03 posva