Skip to content

Commit

Permalink
fix: remove value and writable properties from headers descriptor (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
avanderbergh authored Dec 2, 2024
1 parent 74ee2e4 commit 15f000c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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

0 comments on commit 15f000c

Please sign in to comment.