diff --git a/.changeset/tame-hats-fold.md b/.changeset/tame-hats-fold.md new file mode 100644 index 000000000000..e369aa8d4ca5 --- /dev/null +++ b/.changeset/tame-hats-fold.md @@ -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. diff --git a/packages/astro/src/core/request.ts b/packages/astro/src/core/request.ts index 3ace80ba8fc9..624bee879850 100644 --- a/packages/astro/src/core/request.ts +++ b/packages/astro/src/core/request.ts @@ -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() { @@ -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