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
6 changes: 6 additions & 0 deletions .changeset/quiet-topology-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"rsbuild-plugin-react-router": patch
---

Preserve route topology declaration order during development so reordering route
entries is detected as a topology change.
27 changes: 14 additions & 13 deletions src/route-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,20 @@ export const createRouteManifestSnapshot = (
routes: Record<string, RouteManifestSnapshotEntry>
): Set<string> =>
new Set(
Object.entries(routes)
.sort(([left], [right]) => left.localeCompare(right))
.map(([routeId, route]) =>
JSON.stringify([
routeId,
route.id,
route.parentId ?? null,
route.path ?? null,
route.index ?? null,
route.caseSensitive ?? null,
route.file,
])
)
// React Router uses sibling declaration order as a match tiebreaker, so the
// snapshot must preserve route-manifest insertion order.
Object.entries(routes).map(([routeId, route], order) =>
JSON.stringify([
order,
routeId,
route.id,
route.parentId ?? null,
route.path ?? null,
route.index ?? null,
route.caseSensitive ?? null,
route.file,
])
)
);

export const ensureDevRestartMarker = async (
Expand Down
34 changes: 23 additions & 11 deletions tests/route-watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,39 @@ describe('route watch topology snapshot', () => {
);
});

it('is stable for equivalent route manifests with different object insertion order', () => {
it('changes when sibling declaration order changes', () => {
const first = createRouteManifestSnapshot({
root: { id: 'root', path: '', file: 'root.tsx' },
'routes/demo': {
id: 'routes/demo',
'routes/a': {
id: 'routes/a',
parentId: 'root',
path: 'demo',
file: 'routes/demo.tsx',
path: ':value',
file: 'routes/a.tsx',
},
'routes/b': {
id: 'routes/b',
parentId: 'root',
path: ':value',
file: 'routes/b.tsx',
},
});

const second = createRouteManifestSnapshot({
'routes/demo': {
id: 'routes/demo',
root: { id: 'root', path: '', file: 'root.tsx' },
'routes/b': {
id: 'routes/b',
parentId: 'root',
path: 'demo',
file: 'routes/demo.tsx',
path: ':value',
file: 'routes/b.tsx',
},
'routes/a': {
id: 'routes/a',
parentId: 'root',
path: ':value',
file: 'routes/a.tsx',
},
root: { id: 'root', path: '', file: 'root.tsx' },
});

expect(second).toEqual(first);
expect(second).not.toEqual(first);
});
});