Skip to content
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

Destination route hooks being executed from navigate when callHandler is false #265

Closed
rjgamble opened this issue Jan 13, 2021 · 4 comments

Comments

@rjgamble
Copy link

Hi @krasimir, following on from the issue I was seeing previously when using navigate(route, options) in a before hook. #262

I have noticed that the before hook is being executed in the destination route when I use router.navigate(route, options) even when I set callHandler to false. Given the below example:

const router = new navigo('/');

router.on({
    '/abc-1234': {
        as: 'routeA',
        uses: (match) => { console.log('Hit routeA handler.'); },
        hooks: {
            before: (done, match) => {
                console.log('Hit routeA before hook.');
                router.navigate('/item/abc-1234', {
                    historyAPIMethod: 'replaceState',
                    updateBrowserURL: true,
                    updateState: true,
                    callHandler: false,
                    force: false
                });

                done();
            },
            after: (match) => { console.log('Hit routeA after hook.'); }
        }
    }
});

router.on({
    '/item/abc-1234': {
        as: 'routeB',
        uses: (match) => { console.log('Hit routeB handler.') },
        hooks: {
            before: (done, match) => { console.log('Hit routeB before hook.'); },
            after: (match) => { console.log('Hit routeA after hook.'); }
        }
    }
});

router.resolve();
router.navigate('/abc-1234');

The output I'm seeing from this is:

Hit routeA before hook.
Hit routeB before hook. // <- Should I be seeing this?
Hit routeA handler.
Hit routeA after hook.

Notice, I only hit the before hook for routeB but not the after hook.

What I am expecting to happen is here is that because I've set callHandler to false, I don't execute any of the route lifecycle for routeB.

@krasimir
Copy link
Owner

@rjgamble looks like a bug yes. Will try to fix it today.

krasimir pushed a commit that referenced this issue Jan 13, 2021
@krasimir
Copy link
Owner

@rjgamble instead of restricting the hooks because callHandler is set to false I decided to introduce callHooks flag. This way we can still cover the case where your handler is not fired but the hooks are. So in your case:

router.navigate('/item/abc-1234', {
  historyAPIMethod: 'replaceState',
  callHandler: false,
  callHooks: false
});

You don't have to specify force, updateBrowserURL and updateState because their values are anyway false, true and true.

@krasimir
Copy link
Owner

krasimir commented Jan 13, 2021

New version 👉 8.6.0.

@rjgamble
Copy link
Author

Hi @krasimir, thanks for taking a look. I upgraded to 8.6.3 and retested. It is working as expected when passing callHooks: false.

Thanks for the tip regarding the default values for the other navigate options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants