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
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@
- souzasmatheus
- SovietGhost
- soxtoby
- spokodev
- srmagura
- SsongQ-92
- stasundr
Expand Down
10 changes: 10 additions & 0 deletions packages/create-react-router/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"types": ["node", "jest"],
"rootDir": "..",
"noEmit": true
}
}
10 changes: 10 additions & 0 deletions packages/react-router-architect/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"types": ["node", "jest"],
"rootDir": "..",
"noEmit": true
}
}
10 changes: 10 additions & 0 deletions packages/react-router-dev/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"types": ["node", "vite/client", "@vitejs/plugin-rsc/types", "jest"],
"rootDir": "..",
"noEmit": true
}
}
10 changes: 10 additions & 0 deletions packages/react-router-express/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"types": ["node", "jest"],
"rootDir": "..",
"noEmit": true
}
}
10 changes: 10 additions & 0 deletions packages/react-router-fs-routes/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"types": ["node", "jest"],
"rootDir": "..",
"noEmit": true
}
}
10 changes: 10 additions & 0 deletions packages/react-router-node/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"types": ["node", "jest"],
"rootDir": "..",
"noEmit": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"types": ["node", "jest"],
"rootDir": "..",
"noEmit": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix route ranking/scoring bug with dynamic parameters containing static extension suffixes (i.e., `/:name.xml`)

- These were not being detected as dynamic param segments and instead got incorrectly scored higher as a static segment
- This meant they could potentially tie truly static routes like `/sitemap.xml` and outrank them based on definition order
- These are now correctly identified as dynamic parameter segments and scored correctly
79 changes: 79 additions & 0 deletions packages/react-router/__tests__/path-matching-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,85 @@ describe("path matching", () => {
).toStrictEqual({ lang: "en" });
expect(matchPath("/sitemap/:lang?.xml", "/sitemap.xml")).toBeNull();
});

it("ranks a static routes above dynamic params with a static suffix", () => {
// Dynamic before static
expect(
pickPathsAndParams(
[{ path: "/:lang.xml" }, { path: "/sitemap.xml" }],
"/sitemap.xml",
),
).toEqual([
{
path: "/sitemap.xml",
params: {},
},
]);

// Static before dynamic
expect(
pickPathsAndParams(
[{ path: "/sitemap.xml" }, { path: "/:lang.xml" }],
"/sitemap.xml",
),
).toEqual([
{
path: "/sitemap.xml",
params: {},
},
]);

// Params still get extracted for dynamic-matching paths
expect(
pickPathsAndParams(
[{ path: "/sitemap.xml" }, { path: "/:lang.xml" }],
"/fr.xml",
),
).toEqual([
{
path: "/:lang.xml",
params: { lang: "fr" },
},
]);
});

it("ranks dynamic params with a suffix above dynamic params", () => {
// Dynamic+suffix before dynamic
expect(
pickPathsAndParams(
[{ path: "/:a.json" }, { path: "/:b" }],
"/thing.json",
),
).toEqual([
{
path: "/:a.json",
params: { a: "thing" },
},
]);

// Dynamic before dynamic+suffix
expect(
pickPathsAndParams(
[{ path: "/:b" }, { path: "/:a.json" }],
"/thing.json",
),
).toEqual([
{
path: "/:a.json",
params: { a: "thing" },
},
]);

// Non-suffix still matches dynamic param
expect(
pickPathsAndParams([{ path: "/:a.json" }, { path: "/:b" }], "/thing"),
).toEqual([
{
path: "/:b",
params: { b: "thing" },
},
]);
});
});

describe("path matching with a basename", () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/react-router/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"types": ["node", "jest"],
"rootDir": "..",
"noEmit": true
}
}
15 changes: 9 additions & 6 deletions packages/react-router/lib/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,8 @@ function rankRouteBranches(branches: RouteBranch[]): void {
}

const paramRe = /^:[\w-]+$/;
const partialParamRe = /^:[\w-]+/;
const partialDynamicSegmentValue = 3.5;
const dynamicSegmentValue = 3;
const indexRouteValue = 2;
const emptySegmentValue = 1;
Expand All @@ -1374,12 +1376,13 @@ function computeScore(path: string, index: boolean | undefined): number {
.filter((s) => !isSplat(s))
.reduce(
(score, segment) =>
score +
(paramRe.test(segment)
? dynamicSegmentValue
: segment === ""
? emptySegmentValue
: staticSegmentValue),
// prettier-ignore
score + (
paramRe.test(segment) ? dynamicSegmentValue :
partialParamRe.test(segment) ? partialDynamicSegmentValue :
segment === "" ? emptySegmentValue :
staticSegmentValue
),
initialScore,
);
}
Expand Down