-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onBeforeRouteEnter not triggered if coming from reject route #244
Comments
@stackoverfloweth is this when navigating from the rejection back to profile? Or just anytime you try and go to profile after hitting a rejection? |
@pleek91 an update on this The first time I enter my route with this hook I see a hook in The second time I enter my route the This line has a condition I made a branch in our router-preview repo setup to reproduce |
@stackoverfloweth I wrote a test that I think reproduces what you're talking about there. Responding here mostly so I can remember what I found. The issue is you didn't actually change routes. You navigated to So I think its working as designed. But maybe not working as it needs to? I think the logic for enter vs update is correct. But it doesn't maybe match up with developers expectations when combined with rejections. test('test hooks', async () => {
const enter = vi.fn()
const update = vi.fn()
let activeUser = false
const profile = createRoute({
name: 'profile',
path: '/profile',
component: { template: 'Profile' },
onBeforeRouteEnter: (to, { reject }) => {
enter()
if (!activeUser) {
reject('NotFound')
}
},
onBeforeRouteUpdate: () => {
update()
},
})
const router = createRouter([profile], {
initialUrl: '/profile',
})
await router.start()
const root = {
template: '<RouterView/>',
}
const app = mount(root, {
global: {
plugins: [router],
},
})
expect(app.text()).toBe('NotFound')
expect(update).toHaveBeenCalledTimes(0)
expect(enter).toHaveBeenCalledTimes(1)
activeUser = true
await router.push('/profile')
expect(app.text()).toBe('Profile')
expect(update).toHaveBeenCalledTimes(1)
// this fails because the hook wasn't run a second time
expect(hook).toHaveBeenCalledTimes(2)
}) |
@pleek91 interesting... that definitely makes sense now. I like that rejections keep you on the route but I'm concerned that devs will make the same mistake I did assuming that because it feels like they're on a rejection route (not found) that they can push to profile and trigger route enter. I'll continue to give this some thought but as of right now I'm tempted to treat rejection routes like separate routes and abandon the idea of just fix the error and call refresh. That's not intuitive and breaks hooks. Technically we have rejection routes, I'm wondering if we should just use those as the current route. For the use case of auth needed and returning to the route they were trying to access, maybe we can do something else to still create a solid devex 🤔 |
I added a
onBeforeRouteEnter
hook to my routeworks 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 thisonBeforeRouteEnter
hook.The text was updated successfully, but these errors were encountered: