navigo icon indicating copy to clipboard operation
navigo copied to clipboard

Root handler not invoked in v8

Open mamacdon opened this issue 3 years ago • 3 comments

When running Navigo 7.1.2 as shown below:

console.log(`URL = ${window.location}`);
new Navigo(null, true /* hash = true */)
  .on("/foo", () => console.log("foo handler"))
  .on(() => console.log("root handler"))
  .resolve();

… calling resolve() invoked my root handler:

URL = http://localhost:10334/foo
root handler

After I upgraded to v8.11.0, my code looks like this:

console.log(`URL = ${window.location}`);
new Navigo("/", { hash: true })
  .on("/foo", () => console.log("foo handler"))
  .on(() => console.log("root handler"))
  .resolve();

… but now the root handler is not called:

URL = http://localhost:10334/foo
foo handler

In v8, resolve() seems to match routes based on the pathname (/foo). This strikes me as weird – with hash: true, shouldn't Navigo route based on the hash only?

mamacdon avatar Apr 22 '21 16:04 mamacdon

Hey,

if you have has: true then your browser journey should start with /#/foo. Also I like to think that the hash routing is more of a supportive feature. The main usage should not use hashes. That's because hashes (in general) have another purpose and also because history based routing is well supported.

krasimir avatar Apr 23 '21 07:04 krasimir

I have the same problem (at least i think)

const router = new Navigo("/", { hash: true });

router.on({
  "/about": function () {
    console.log("about");
  },
});

Clicking on a link to http://127.0.0.1:5500/#/about works fine, but when i reload the page, the callback function is not invoked.

behu-kea avatar Oct 22 '21 08:10 behu-kea

Ahh sorry i missed the resolve part

router
  .on({
    about: function () {
      console.log("about");
    },
  })
  .resolve();

Working now

behu-kea avatar Oct 22 '21 08:10 behu-kea