krasimir/navigo

Default strategy is not necessarily 'ONE', but is 'ALL' when specifying some options but no strategy.

sugacube opened this issue · 0 comments

v8+ documentation says "By default the strategy is equal to "ONE"."
But if some options are specified, but the strategy option is left out, it defaults to 'ALL'.

How to reproduce:

const r1 = new Navigo('/');
r1.on('/x', () => {}) 
r1.on('*', () => {}) 
r1.match('/x').length 

->  result = 1

const r2 = new Navigo('/', { hash: true});
r2.on('/x', () => {}) 
r2.on('*', () => {}) 
r2.match('/x').length 

->  result = 2

What I expected:

  • Only first route is matched, i.e. length of match array = 1

What really happened:

  • All matching routes are returned (length = 2)

Workaround:

  • explicit include "strategy: 'ONE'" in constructor.

    const r3 = new Navigo('/', { hash: true, strategy: 'ONE'});
    r3.on('/x', () => {})
    r3.on('*', () => {})
    r3.match('/x').length

Remark:
The culprit seems to be when options are provided, strategy remains unset if not explicitly provided.
In "matchPathToRegisteredRoutes.ts", line "if (context.resolveOptions.strategy === "ONE") {" will fail, effectively matching all routes.
At first sight, this initialisation seems unharmful for the other options, and linkselector is explicitly being checked for being unset in "findLinks()".