Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,17 @@ function expand<T>(node: Node<T>, 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,
// which keeps the first of two that read the same. A route added whole
// 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
}
Expand All @@ -408,7 +410,9 @@ function expand<T>(node: Node<T>, 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]
Expand Down
5 changes: 5 additions & 0 deletions packages/router/tests/late-adds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }],
Expand All @@ -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' }],
})
})

Expand Down
Loading