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

Build fails with Invalid property descriptor when using Cloudflare adapter with modified Request headers prototype #12548

Closed
1 task done
avanderbergh opened this issue Nov 28, 2024 · 0 comments · Fixed by #12552
Labels
needs triage Issue needs to be triaged

Comments

@avanderbergh
Copy link
Contributor

Astro Info

Astro                    v4.16.16
Node                     v20.18.0
System                   Linux (x64)
Package Manager          pnpm
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When using the Cloudflare adapter in an Astro project, importing certain plugins (such as better-auth/plugins) results in an unexpected modification of the Request prototype.

Specifically, the Request.headers object is extended with a map property. This modification causes a build error during prerendering (astro build with export const prerender = true), resulting in the following error message:

Invalid property descriptor. Cannot both specify accessors and a value or writable attribute, #<Object>

The error is thrown in the /core/request file, when attempting to redefine the headers property of the Request object.

const request = new Request(url, {
	method: method,
	headers: headersObj,
	// body is made available only if the request is for a page that will be on-demand rendered
	body: staticLike ? null : body,
});

if (staticLike) {
	// Warn when accessing headers in SSG mode
	const _headers = request.headers;
	const headersDesc = Object.getOwnPropertyDescriptor(request, 'headers') || {};
	Object.defineProperty(request, 'headers', {
		...headersDesc,
		get() {
			logger.warn(
				null,
				`\`Astro.request.headers\` is unavailable in "static" output mode, and in prerendered pages within "hybrid" and "server" output modes. If you need access to request headers, make sure that \`output\` is configured as either \`"server"\` or \`output: "hybrid"\` in your config file, and that the page accessing the headers is rendered on-demand.`,
			);
			return _headers;
		},
	});
} 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
	Reflect.set(request, clientAddressSymbol, clientAddress);
}

The issue arises because the headers property already has a value, and the build logic tries to add a getter to it in order to show a warning when accessing the headers. This combination of both having a value and adding a getter causes the Invalid property descriptor error, resulting in the build failing.

For additional details, please refer to the related issue I created in the better-auth repository: better-auth issue #681.

What's the expected result?

The build completes without errors.

Link to Minimal Reproducible Example

https://github.com/avanderbergh/better-auth-astro-issue

Participation

  • I am willing to submit a pull request for this issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant