krasimir / navigo

A simple vanilla JavaScript router.
MIT License
2.74k stars 249 forks source link

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

Open sugacube opened 9 months ago

sugacube commented 9 months ago

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:

What really happened:

Workaround:

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()".