Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions .changeset/stale-books-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@cloudflare/pages-shared": patch
---

fix: omit headers rules on internal error

The Pages asset handler will no longer apply headers rules on 500 responses caused by some internal error.
This prevents transient errors from being cached when caching headers are being set by headers rules.
30 changes: 30 additions & 0 deletions packages/pages-shared/__tests__/asset-server/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,36 @@ describe("asset-server handler", () => {
expect(isPreservationCacheResponseExpiring(res)).toBe(true);
});
});

test("internal asset error doesn't set headers", async () => {
Comment thread
WalshyDev marked this conversation as resolved.
Outdated
const metadata = createMetadataObject({
deploymentId: "mock-deployment-id",
headers: {
invalid: [],
rules: [
{
path: "/*",
headers: {"x-unwanted-header": "foo"},
unsetHeaders: [],
},
],
}
}) as Metadata;

const { response } = await getTestResponse({
request: "https://foo.com/",
metadata,
fetchAsset: async (...args) => {
throw "uh oh";
},
findAssetEntryForPath: async (path: string) => {
return "some page";
},
});

expect(response.status).toBe(500);
expect(Object.fromEntries(response.headers)).not.toHaveProperty("x-unwanted-header");
});
});

interface HandlerSpies {
Expand Down
6 changes: 5 additions & 1 deletion packages/pages-shared/asset-server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,11 @@ export async function generateHandler<
};
}
);
const matches = headersMatcher({ request });

// If the response was an internal error from the asset handler, skip applying header rules.
Comment thread
aaronlisman marked this conversation as resolved.
Outdated
// This avoids a case where we can cache 500s for a long time, depending on headers set by a user.
Comment thread
aaronlisman marked this conversation as resolved.
Outdated
const assetError = response.status >= 400 && response.status <= 599
const matches = assetError ? [] : headersMatcher({ request });

// This keeps track of every header that we've set from _headers
// because we want to combine user declared headers but overwrite
Expand Down