Skip to content

Issues with before hook and History state push when resolving same route several times #115

@pwrinc

Description

@pwrinc

I did figure out some strange non expected behavior when resolving the same path several times in sequence.

Using navigo release 4.6.0

Consider following router configuration:

let router = new Navigo(null);

router.on(
  '/page1',
  function () {
    console.log('Navigate to /page1');
  }
);

Scenario 1

When resolving route /page1 multiple times in sequence (e.g. 3 times) by either clicking a link <a href='/page1' data-navigo> several times or calling

router.navigate('/page1');
router.navigate('/page1');
router.navigate('/page1');

The route handler is called only once (which is correct behaviour). Thus Navigate to /page1 is only called once after the route changes the first time. In contrast each time the router.navigate() is called/link is clicked a history item is added to browser history by calling history.pushState(). So in that case I would expect history.pushState() is only called once at the first resolve for this route instead of three times.

Scenario 2:

When adjusting route above to add before hook like

before: function (done) {
  console.log('Before hook for /page1');
  done(false);
}

The browser console outputs Before hook for /page1. Navigo prevents calling the resolve function for this root. Thus Navigate to /page1 is not written to console. Although this should prevent route change a new history item is pushed by calling history.pushState(). The expected behavior would be to stop route handleing wen before handler calls done(false) and thus prevent browser history (and addressbar) to be changed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions