Skip to content

Commit a1371f4

Browse files
authored
fix handleError params values on dynamic param data calls (#13481)
1 parent 55a05c8 commit a1371f4

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

.changeset/pink-candles-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Fix `handleError` `params` values on `.data` requests for routes with a dynamic param as the last URL segment

packages/react-router/__tests__/server-runtime/handle-error-test.ts

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe("handleError", () => {
112112
throw error;
113113
},
114114
});
115-
let request = new Request("http://example.com/?_data=root");
115+
let request = new Request("http://example.com/_root.data");
116116
await handler(request);
117117
expect(handleErrorSpy).toHaveBeenCalledWith(error, {
118118
request,
@@ -123,7 +123,7 @@ describe("handleError", () => {
123123

124124
it("provides router-thrown ErrorResponse", async () => {
125125
let { handler, handleErrorSpy } = getHandler({});
126-
let request = new Request("http://example.com/?_data=root", {
126+
let request = new Request("http://example.com/_root.data", {
127127
method: "post",
128128
});
129129
await handler(request);
@@ -153,10 +153,65 @@ describe("handleError", () => {
153153
);
154154
},
155155
});
156-
let request = new Request("http://example.com/?_data=root");
156+
let request = new Request("http://example.com/_root.data");
157157
await handler(request);
158158
expect(handleErrorSpy).not.toHaveBeenCalled();
159159
});
160+
161+
it("provides proper params to handleError", async () => {
162+
let error = new Error("💥");
163+
164+
let handleErrorSpy = jest.fn();
165+
let build: ServerBuild = {
166+
routes: {
167+
param: {
168+
id: "param",
169+
path: "/:param",
170+
module: {
171+
default() {
172+
return null;
173+
},
174+
loader() {
175+
throw error;
176+
},
177+
},
178+
},
179+
},
180+
entry: {
181+
module: {
182+
handleError: handleErrorSpy,
183+
default() {
184+
return new Response("<html><body>Dummy document</body></html>");
185+
},
186+
},
187+
},
188+
future: {
189+
// Fill in the required values
190+
unstable_middleware: false,
191+
unstable_subResourceIntegrity: false,
192+
},
193+
prerender: [],
194+
assets: {
195+
entry: { imports: [], module: "" },
196+
routes: {},
197+
url: "",
198+
version: "",
199+
},
200+
assetsBuildDirectory: "",
201+
publicPath: "/",
202+
ssr: true,
203+
isSpaMode: false,
204+
};
205+
206+
let handler = createRequestHandler(build);
207+
let request = new Request("http://example.com/a.data");
208+
await handler(request);
209+
expect(handleErrorSpy).toHaveBeenCalledWith(error, {
210+
request,
211+
params: { param: "a" },
212+
context: {},
213+
});
214+
});
160215
});
161216

162217
describe("resource request", () => {

packages/react-router/lib/server-runtime/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
216216
}
217217
}
218218

219-
let matches = matchServerRoutes(routes, url.pathname, _build.basename);
219+
let matches = matchServerRoutes(routes, normalizedPath, _build.basename);
220220
if (matches && matches.length > 0) {
221221
Object.assign(params, matches[0].params);
222222
}

0 commit comments

Comments
 (0)