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

history.pushState to a unexsting path doesn't redirect correctly

Open pedrokohler opened this issue 4 years ago • 0 comments

window.history.pushState(null, '', redirectTo || 'menu');

I'm using the code above to decide where my Login page will redirect to when it is accessed while the user is already logged in.

The problem is that, even though my routes are as follows:

customElements.whenDefined('router-slot').then(async () => {
  const routerSlot = document.querySelector('router-slot');
  await routerSlot.add([
    {
      path: 'menu',
      component: () => import('./pages/menu-page'),
      guards: [sessionGuard],
    },
    {
      path: 'send-song',
      component: () => import('./pages/send-song-page'),
      guards: [sessionGuard],
    },
    {
      path: 'vote',
      component: () => import('./pages/vote-page'),
      guards: [sessionGuard],
    },
    {
      path: 'logout',
      component: () => import('./pages/logout-page'),
      guards: [sessionGuard],
    },
    {
      path: '',
      component: () => import('./pages/login-page'),
    },
    {
      path: '**',
      redirectTo: 'menu',
    },
  ]);
});

Meaning that I have a path: '**' with a redirectTo: 'menu', it doesn't work properly if the pushState's redirectTo is nonsense. In this case it just tries to access the nonsensical route instead of redirecting to 'menu'.

Is this a bug or am I doing something wrong?

Thanks

pedrokohler avatar Sep 08 '20 22:09 pedrokohler