Skip to content
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

fix: remove value and writable properties from headers descriptor #12552

Merged
merged 3 commits into from
Dec 2, 2024
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/tame-hats-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixed an issue where modifying the `Request.headers` prototype during prerendering caused a build error. Removed conflicting value and writable properties from the `headers` descriptor to prevent `Invalid property descriptor` errors.
13 changes: 10 additions & 3 deletions packages/astro/src/core/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ export function createRequest({
});

if (isPrerendered) {
// Warn when accessing headers in prerendered pages
const _headers = request.headers;
const headersDesc = Object.getOwnPropertyDescriptor(request, 'headers') || {};
// Warn when accessing headers in SSG mode
let _headers = request.headers;

// We need to remove descriptor's value and writable properties because we're adding getters and setters.
const { value, writable, ...headersDesc } =
Object.getOwnPropertyDescriptor(request, 'headers') || {};

Object.defineProperty(request, 'headers', {
...headersDesc,
get() {
Expand All @@ -82,6 +86,9 @@ export function createRequest({
);
return _headers;
},
set(newHeaders: Headers) {
_headers = newHeaders;
},
});
} else if (clientAddress) {
// clientAddress is stored to be read by RenderContext, only if the request is for a page that will be on-demand rendered
Expand Down
Loading