Skip to content

Redirecting from beforeEnter guard will push state during popstate handling #3381

@fabianeichinger

Description

@fabianeichinger

Version

3.4.9

Reproduction link

https://jsfiddle.net/jwx6q3hc/4

Steps to reproduce

  1. Create a router with
{
  mode: 'history',
  routes: [
    { path: '/a' },
    { path: '/b', beforeEnter: (to, from, next) => next({ name: 'redirect-target' }) },
    //  { path: '/b', redirect: { name: 'redirect-target' } },
    { name: 'redirect-target', path: '/b' }
  ]
}
  1. Initially navigate to /a
  2. Navigate to /b
  3. Navigate to /a
  4. Hit the browser back button
  5. Hit the browser back button again

What is expected?

After hitting the back button in step 5. we should be back on /a.
The forward button should be enabled.
The history should look like

/a --- pushed by 3.
/b --- pushed by 2.
/a <-- current, pushed by 1.

What is actually happening?

After hitting back in step 5., we stay on /b.
The forward button is disabled, browser history looks like this

/b <-- current, pushed by 4.
/a --- pushed by 1.

I hoped this setup would work for a proposal to simplify some dynamic routing in Vue Storefront (take a look at this gist if you're interested in the usecase and some more details). Essentially I want use beforeEnter as a substitute for redirect where I can do some async requests before returning the redirection target.

The issue here is that calling next with some redirection arguments is always handled by calling router.push or router.replace (in the case of next({ replace: true, ... }). But in this usecase there should be no change to the browser history during popstate handling.

Doing the equivalent redirection with redirect works just fine (though I can't use it for my usecase, as I can't return asynchronously from the redirect function). I've marked this as a bug as from what I understand vuejs/rfcs#150 has the goal that both should work the same way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions