0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

chore: better warning (#12642)

* chore: better warning

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
This commit is contained in:
Emanuele Stoppa 2024-12-05 12:25:01 +00:00 committed by GitHub
parent 22e405a044
commit ff18b9c185
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 11 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Provides more information when logging a warning for accessing `Astro.request.headers` in prerendered pages

View file

@ -437,6 +437,7 @@ async function generatePath(
headers: new Headers(),
logger,
isPrerendered: true,
routePattern: route.component
});
const renderContext = await RenderContext.create({
pipeline,

View file

@ -20,6 +20,8 @@ export interface CreateRequestOptions {
* @default false
*/
isPrerendered?: boolean;
routePattern: string
}
const clientAddressSymbol = Symbol.for('astro.clientAddress');
@ -41,6 +43,7 @@ export function createRequest({
logger,
locals,
isPrerendered = false,
routePattern
}: CreateRequestOptions): Request {
// headers are made available on the created request only if the request is for a page that will be on-demand rendered
const headersObj = isPrerendered
@ -82,7 +85,7 @@ export function createRequest({
get() {
logger.warn(
null,
`\`Astro.request.headers\` is not available on prerendered pages. If you need access to request headers, make sure that the page is server rendered using \`export const prerender = false;\` or by setting \`output\` to \`"server"\` in your Astro config to make all your pages server rendered.`,
`\`Astro.request.headers\` was used when rendering the route \`${routePattern}'\`. \`Astro.request.headers\` is not available on prerendered pages. If you need access to request headers, make sure that the page is server-rendered using \`export const prerender = false;\` or by setting \`output\` to \`"server"\` in your Astro config to make all your pages server-rendered by default.`,
);
return _headers;
},

View file

@ -174,6 +174,7 @@ export async function handleRoute({
logger,
clientAddress: incomingRequest.socket.remoteAddress,
isPrerendered: route.prerender,
routePattern: route.component
});
// Set user specified headers to response object.