Skip to content

fix handleError params values on dynamic param data calls #13481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2025
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
5 changes: 5 additions & 0 deletions .changeset/pink-candles-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Fix `handleError` `params` values on `.data` requests for routes with a dynamic param as the last URL segment
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe("handleError", () => {
throw error;
},
});
let request = new Request("http://example.com/?_data=root");
let request = new Request("http://example.com/_root.data");
await handler(request);
expect(handleErrorSpy).toHaveBeenCalledWith(error, {
request,
Expand All @@ -123,7 +123,7 @@ describe("handleError", () => {

it("provides router-thrown ErrorResponse", async () => {
let { handler, handleErrorSpy } = getHandler({});
let request = new Request("http://example.com/?_data=root", {
let request = new Request("http://example.com/_root.data", {
method: "post",
});
await handler(request);
Expand Down Expand Up @@ -153,10 +153,65 @@ describe("handleError", () => {
);
},
});
let request = new Request("http://example.com/?_data=root");
let request = new Request("http://example.com/_root.data");
await handler(request);
expect(handleErrorSpy).not.toHaveBeenCalled();
});

it("provides proper params to handleError", async () => {
let error = new Error("💥");

let handleErrorSpy = jest.fn();
let build: ServerBuild = {
routes: {
param: {
id: "param",
path: "/:param",
module: {
default() {
return null;
},
loader() {
throw error;
},
},
},
},
entry: {
module: {
handleError: handleErrorSpy,
default() {
return new Response("<html><body>Dummy document</body></html>");
},
},
},
future: {
// Fill in the required values
unstable_middleware: false,
unstable_subResourceIntegrity: false,
},
prerender: [],
assets: {
entry: { imports: [], module: "" },
routes: {},
url: "",
version: "",
},
assetsBuildDirectory: "",
publicPath: "/",
ssr: true,
isSpaMode: false,
};

let handler = createRequestHandler(build);
let request = new Request("http://example.com/a.data");
await handler(request);
expect(handleErrorSpy).toHaveBeenCalledWith(error, {
request,
params: { param: "a" },
context: {},
});
});
});

describe("resource request", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/lib/server-runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
}
}

let matches = matchServerRoutes(routes, url.pathname, _build.basename);
let matches = matchServerRoutes(routes, normalizedPath, _build.basename);
if (matches && matches.length > 0) {
Object.assign(params, matches[0].params);
}
Expand Down