0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

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

This commit is contained in:
Adriaan van der Bergh 2024-12-02 15:35:11 +01:00 committed by GitHub
parent 74ee2e45ec
commit 15f000c3e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 3 deletions

View file

@ -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.

View file

@ -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