Skip to content

Commit 5cfde28

Browse files
authored
fix: expose leader/action headers on error response sin static handler (#9140)
1 parent c17512d commit 5cfde28

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

packages/router/__tests__/router-test.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9914,7 +9914,37 @@ describe("a router", () => {
99149914
});
99159915

99169916
describe("headers", () => {
9917-
it("should expose headers from loader responses", async () => {
9917+
it("should expose headers from action/loader responses", async () => {
9918+
let { query } = createStaticHandler([
9919+
{
9920+
id: "root",
9921+
path: "/",
9922+
loader: () => new Response(null, { headers: { two: "2" } }),
9923+
children: [
9924+
{
9925+
id: "child",
9926+
index: true,
9927+
action: () => new Response(null, { headers: { one: "1" } }),
9928+
loader: () => new Response(null, { headers: { three: "3" } }),
9929+
},
9930+
],
9931+
},
9932+
]);
9933+
let context = (await query(
9934+
createSubmitRequest("/?index")
9935+
)) as StaticHandlerContext;
9936+
expect(Array.from(context.actionHeaders.child.entries())).toEqual([
9937+
["one", "1"],
9938+
]);
9939+
expect(Array.from(context.loaderHeaders.root.entries())).toEqual([
9940+
["two", "2"],
9941+
]);
9942+
expect(Array.from(context.loaderHeaders.child.entries())).toEqual([
9943+
["three", "3"],
9944+
]);
9945+
});
9946+
9947+
it("should expose headers from loader error responses", async () => {
99189948
let { query } = createStaticHandler([
99199949
{
99209950
id: "root",
@@ -9924,7 +9954,9 @@ describe("a router", () => {
99249954
{
99259955
id: "child",
99269956
index: true,
9927-
loader: () => new Response(null, { headers: { two: "2" } }),
9957+
loader: () => {
9958+
throw new Response(null, { headers: { two: "2" } });
9959+
},
99289960
},
99299961
],
99309962
},
@@ -9940,18 +9972,18 @@ describe("a router", () => {
99409972
]);
99419973
});
99429974

9943-
it("should expose headers from action responses", async () => {
9975+
it("should expose headers from action error responses", async () => {
99449976
let { query } = createStaticHandler([
99459977
{
99469978
id: "root",
99479979
path: "/",
9948-
loader: () => new Response(null, { headers: { two: "2" } }),
99499980
children: [
99509981
{
99519982
id: "child",
99529983
index: true,
9953-
action: () => new Response(null, { headers: { one: "1" } }),
9954-
loader: () => new Response(null, { headers: { three: "3" } }),
9984+
action: () => {
9985+
throw new Response(null, { headers: { one: "1" } });
9986+
},
99559987
},
99569988
],
99579989
},
@@ -9962,12 +9994,6 @@ describe("a router", () => {
99629994
expect(Array.from(context.actionHeaders.child.entries())).toEqual([
99639995
["one", "1"],
99649996
]);
9965-
expect(Array.from(context.loaderHeaders.root.entries())).toEqual([
9966-
["two", "2"],
9967-
]);
9968-
expect(Array.from(context.loaderHeaders.child.entries())).toEqual([
9969-
["three", "3"],
9970-
]);
99719997
});
99729998
});
99739999
});

packages/router/router.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,8 +1852,8 @@ export function unstable_createStaticHandler(
18521852
errors: {
18531853
[boundaryMatch.route.id]: result.error,
18541854
},
1855-
// Note: This is unused in queryRoute as we will return the raw error,
1856-
// or construct a response from the ErrorResponse
1855+
// Note: statusCode + headers are unused here since queryRoute will
1856+
// return the raw Response or value
18571857
statusCode: 500,
18581858
loaderHeaders: {},
18591859
actionHeaders: {},
@@ -1865,13 +1865,11 @@ export function unstable_createStaticHandler(
18651865
loaderData: {},
18661866
actionData: { [actionMatch.route.id]: result.data },
18671867
errors: null,
1868-
// Note: This is unused in queryRoute as we will return the raw
1869-
// Response or value
1868+
// Note: statusCode + headers are unused here since queryRoute will
1869+
// return the raw Response or value
18701870
statusCode: 200,
18711871
loaderHeaders: {},
1872-
actionHeaders: {
1873-
...(result.headers ? { [actionMatch.route.id]: result.headers } : {}),
1874-
},
1872+
actionHeaders: {},
18751873
};
18761874
}
18771875

@@ -1890,7 +1888,9 @@ export function unstable_createStaticHandler(
18901888
? result.error.status
18911889
: 500,
18921890
actionData: null,
1893-
actionHeaders: {},
1891+
actionHeaders: {
1892+
...(result.headers ? { [actionMatch.route.id]: result.headers } : {}),
1893+
},
18941894
};
18951895
}
18961896

@@ -2342,6 +2342,7 @@ async function callLoaderOrAction(
23422342
return {
23432343
type: resultType,
23442344
error: new ErrorResponse(status, result.statusText, data),
2345+
headers: result.headers,
23452346
};
23462347
}
23472348

@@ -2449,9 +2450,13 @@ function processRouteLoaderData(
24492450
? result.error.status
24502451
: 500;
24512452
}
2453+
if (result.headers) {
2454+
loaderHeaders[id] = result.headers;
2455+
}
24522456
} else if (isDeferredResult(result)) {
24532457
activeDeferreds?.set(id, result.deferredData);
24542458
loaderData[id] = result.deferredData.data;
2459+
// TODO: Add statusCode/headers once we wire up streaming in Remix
24552460
} else {
24562461
loaderData[id] = result.data;
24572462
// Error status codes always override success status codes, but if all

packages/router/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface RedirectResult {
4949
export interface ErrorResult {
5050
type: ResultType.error;
5151
error: any;
52+
headers?: Headers;
5253
}
5354

5455
/**

0 commit comments

Comments
 (0)