Skip to content
Open
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
35 changes: 35 additions & 0 deletions packages/react-router/__tests__/server-runtime/headers-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @jest-environment node
*/

import { getDocumentHeadersImpl } from "../../lib/server-runtime/headers";
import type { StaticHandlerContext } from "../../lib/router/router";

describe("getDocumentHeadersImpl", () => {
it("uses own-property checks for errors, actionData, and loaderData", () => {
let errorHeaders = new Headers({ "X-Error": "1" });
let nullProtoActionData = Object.create(null);
let nullProtoLoaderData = Object.create(null);

let context = {
matches: [
{ route: { id: "toString" } },
{ route: { id: "child" } },
],
errors: { child: new Error("boom") },
actionHeaders: { child: errorHeaders },
actionData: nullProtoActionData,
loaderHeaders: {},
loaderData: nullProtoLoaderData,
} as unknown as StaticHandlerContext;

let headers = getDocumentHeadersImpl(context, (match) => {
if (match.route.id === "child") {
return ({ errorHeaders: eh }) => eh || new Headers();
}
return undefined;
});

expect(headers.get("X-Error")).toBe("1");
});
});
25 changes: 20 additions & 5 deletions packages/react-router/lib/server-runtime/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export function getDocumentHeadersImpl(
_defaultHeaders?: Headers,
): Headers {
let boundaryIdx = context.errors
? context.matches.findIndex((m) => context.errors![m.route.id])
? context.matches.findIndex(
(m) =>
Object.prototype.hasOwnProperty.call(context.errors, m.route.id) &&
context.errors![m.route.id],
)
: -1;
let matches =
boundaryIdx >= 0
Expand All @@ -40,11 +44,16 @@ export function getDocumentHeadersImpl(
context.matches.slice(boundaryIdx).some((match) => {
let id = match.route.id;
if (
Object.prototype.hasOwnProperty.call(actionHeaders, id) &&
actionHeaders[id] &&
(!actionData || !actionData.hasOwnProperty(id))
(!actionData || !Object.prototype.hasOwnProperty.call(actionData, id))
) {
errorHeaders = actionHeaders[id];
} else if (loaderHeaders[id] && !loaderData.hasOwnProperty(id)) {
} else if (
Object.prototype.hasOwnProperty.call(loaderHeaders, id) &&
loaderHeaders[id] &&
(!loaderData || !Object.prototype.hasOwnProperty.call(loaderData, id))
) {
errorHeaders = loaderHeaders[id];
}
return errorHeaders != null;
Expand All @@ -55,8 +64,14 @@ export function getDocumentHeadersImpl(

return matches.reduce((parentHeaders, match, idx) => {
let { id } = match.route;
let loaderHeaders = context.loaderHeaders[id] || new Headers();
let actionHeaders = context.actionHeaders[id] || new Headers();
let loaderHeaders =
(Object.prototype.hasOwnProperty.call(context.loaderHeaders, id) &&
context.loaderHeaders[id]) ||
new Headers();
let actionHeaders =
(Object.prototype.hasOwnProperty.call(context.actionHeaders, id) &&
context.actionHeaders[id]) ||
new Headers();

// Only expose errorHeaders to the leaf headers() function to
// avoid duplication via parentHeaders
Expand Down
Loading