Skip to content

Commit 24468eb

Browse files
committed
fix: match mid-path catch-alls after last-static indexing
A splat before the last static token can span slashes, so that token's pathname index is not fixed. Leave those routes on the remainder scan.
1 parent d4a6e8b commit 24468eb

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎packages/app-core/src/server/router.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ function compilePath(path) {
7272
/**
7373
* Last static segment of a dynamic route pattern. Positions skip empty
7474
* segments so they line up with a leading-slash pathname split.
75+
* Returns null when a catch-all precedes that token: `*` can span slashes,
76+
* so the token's pathname index is not fixed and the route is scanned.
7577
*
7678
* @param {string} path
7779
* @returns {{ pos: number, value: string } | null}
@@ -81,17 +83,22 @@ function lastStaticSegment(path) {
8183
let pos = -1;
8284
let value = '';
8385
let index = 0;
86+
let sawCatchAll = false;
87+
let catchAllBefore = false;
8488
for (let i = 0; i < raw.length; i++) {
8589
const segment = raw[i];
8690
if (!segment) continue;
8791
const first = segment.charCodeAt(0);
88-
if (first !== 58 /* : */ && first !== 42 /* * */) {
92+
if (first === 42 /* * */) {
93+
sawCatchAll = true;
94+
} else if (first !== 58 /* : */) {
8995
pos = index;
9096
value = segment;
97+
catchAllBefore = sawCatchAll;
9198
}
9299
index++;
93100
}
94-
if (pos === -1) return null;
101+
if (pos === -1 || catchAllBefore) return null;
95102
return { pos, value };
96103
}
97104

‎packages/app-core/tests/router.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,19 @@ describe('app-core request router', () => {
143143
params: { slug: 'guides/setup/' },
144144
});
145145
});
146+
147+
it('matches a catch-all that sits before a later static segment', function () {
148+
const raw = new RenderRoute({ path: '/files/*path/raw', entry: '/src/raw.tsrx' });
149+
const router = createRouter([raw]);
150+
151+
expect(router.match('GET', '/files/a/b/raw')).toEqual({
152+
route: raw,
153+
params: { path: 'a/b' },
154+
});
155+
expect(router.match('GET', '/files/a/raw')).toEqual({
156+
route: raw,
157+
params: { path: 'a' },
158+
});
159+
expect(router.match('GET', '/files/raw')).toBeNull();
160+
});
146161
});

0 commit comments

Comments
 (0)