kitbagjs / router

A type safe router for vuejs
https://router.kitbag.dev
MIT License
173 stars 4 forks source link

onBeforeRouteEnter not triggered if coming from reject route #244

Open stackoverfloweth opened 1 month ago

stackoverfloweth commented 1 month ago

I added a onBeforeRouteEnter hook to my route

const profile = createRoute({
    name: 'profile',
    path: '/profile',
    component: defineAsyncComponent(() => import('../components/ProfilePage.vue')),
    onBeforeRouteEnter: (to, { reject }) => {
        if (!activeUser.value) {
            reject('AuthNeeded')
        }
    },
})

works totally as expected the first time, if I try navigating to /profile without an active user the rejection happens, is handled by sending me to rejection route (doesn't matter if I use built in rejection component or custom). However, if I click a router-link again to go to /profile it seems to totally bypass this onBeforeRouteEnter hook.

pleek91 commented 1 month ago

@stackoverfloweth is this when navigating from the rejection back to profile? Or just anytime you try and go to profile after hitting a rejection?

stackoverfloweth commented 1 month ago

@pleek91 an update on this I traced the issue to getBeforeRouteHooksFromRoutes, which takes to and from and returns all the hooks.

The first time I enter my route with this hook I see a hook in onBeforeRouteEnter where I'd expect

image

The second time I enter my route the onBeforeRouteEnter is empty

image

This line has a condition isRouteEnter, that is almost certainly the issue but I don't understand what it's trying to do assert.

I made a branch in our router-preview repo setup to reproduce https://github.com/kitbagjs/router-preview/compare/issue-244-repro?expand=1