diff --git a/packages/router/src/index.ts b/packages/router/src/index.ts index a91bc23..51739f4 100644 --- a/packages/router/src/index.ts +++ b/packages/router/src/index.ts @@ -384,7 +384,7 @@ function expand(node: Node, pending: unknown[], start: number, whole: bool for (; index < parts.length; index++) { const part = parts[index]! - let segment: Segment = decode(part) + let segment: Segment = part.includes('%') ? decode(part) : part // Only a part starting with `{` once decoded can be a param. Past its // first part, a route waits before one, and before literal text like it, @@ -392,7 +392,9 @@ function expand(node: Node, pending: unknown[], start: number, whole: bool // goes after the routes waiting there. if (segment.charCodeAt(0) === 123) { if (!whole && index > start) { - literals = Math.min(literals, index) + if (index < literals) { + literals = index + } ;(child.pending ??= []).push(parts, data, params) continue next } @@ -408,7 +410,9 @@ function expand(node: Node, pending: unknown[], start: number, whole: bool continue } - literals = Math.min(literals, index) + if (index < literals) { + literals = index + } // An array literal holds just its items, where a push to `[]` reserves 17. if (params === undefined) { params = [segment] diff --git a/packages/router/tests/late-adds.test.ts b/packages/router/tests/late-adds.test.ts index 7ed74c7..e916785 100644 --- a/packages/router/tests/late-adds.test.ts +++ b/packages/router/tests/late-adds.test.ts @@ -15,6 +15,9 @@ describe('routes added after requests', () => { router.add('GET', '/d/{+r}/e', 'duplicate') router.add('GET', '/d/f', '/d/f') router.add('GET', '/d/{x}', '/d/{x}') + // shares its parts up to `t` with the route before it + router.add('GET', '/s/{x}/t/{y}', '/s/{x}/t/{y}') + router.add('GET', '/s/{x}/t/u', '/s/{x}/t/u') expectMatches(router, { '/a/1/b/2': ['/a/{x}/b/{y}', { x: '1', y: '2' }], @@ -26,6 +29,8 @@ describe('routes added after requests', () => { '/d/f/e': ['/d/{+p}/e', { p: 'f' }], '/%64/f': ['/d/f'], '/a/1/c': ['/a/{x}/c', { x: '1' }], + '/s/1/t/2': ['/s/{x}/t/{y}', { x: '1', y: '2' }], + '/s/1/t/u': ['/s/{x}/t/u', { x: '1' }], }) })