Skip to content

Commit 551584c

Browse files
authored
fix(router): literal route prioritized over slug (#1970)
fixes #1968
1 parent 32cd5d5 commit 551584c

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

packages/waku/src/router/create-pages.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ const routePriorityComparator = (
285285
return aPathLength > bPathLength ? -1 : 1;
286286
}
287287

288+
// If path lengths are equal, literal segments take priority over dynamic segments
289+
const minLength = Math.min(aPathLength, bPathLength);
290+
for (let i = 0; i < minLength; i++) {
291+
const aIsLiteral = aPath[i]?.type === 'literal';
292+
const bIsLiteral = bPath[i]?.type === 'literal';
293+
if (aIsLiteral !== bIsLiteral) {
294+
return aIsLiteral ? -1 : 1;
295+
}
296+
}
297+
288298
// If path lengths are equal, compare wildcard presence
289299
// sort the route without the wildcard higher, to check it earlier
290300
if (aHasWildcard !== bHasWildcard) {

packages/waku/tests/create-pages.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,43 @@ describe('createPages pages and layouts', () => {
11631163
);
11641164
});
11651165

1166+
it('literal route takes priority over dynamic slug route at same depth', async () => {
1167+
const AboutPage = vi.fn();
1168+
const SlugPage = vi.fn();
1169+
createPages(async ({ createPage }) => [
1170+
createPage({
1171+
render: 'dynamic',
1172+
path: '/[slug]',
1173+
component: SlugPage,
1174+
}),
1175+
createPage({
1176+
render: 'dynamic',
1177+
path: '/about',
1178+
component: AboutPage,
1179+
}),
1180+
]);
1181+
const { getConfigs } = injectedFunctions();
1182+
const configs = await getConfigs();
1183+
expect(configs).toHaveLength(2);
1184+
const literalRoute = Array.from(configs).find(
1185+
(config) =>
1186+
config.type === 'route' &&
1187+
config.path.length === 1 &&
1188+
config.path[0]?.type === 'literal',
1189+
);
1190+
const slugRoute = Array.from(configs).find(
1191+
(config) =>
1192+
config.type === 'route' &&
1193+
config.path.length === 1 &&
1194+
config.path[0]?.type === 'group',
1195+
);
1196+
expect(literalRoute).toBeDefined();
1197+
expect(slugRoute).toBeDefined();
1198+
expect(Array.from(configs).indexOf(literalRoute!)).toBeLessThan(
1199+
Array.from(configs).indexOf(slugRoute!),
1200+
);
1201+
});
1202+
11661203
it('fails if static paths do not match the slug pattern', async () => {
11671204
createPages(async ({ createPage }) => [
11681205
createPage({

0 commit comments

Comments
 (0)