mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -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:
parent
22e405a044
commit
ff18b9c185
4 changed files with 11 additions and 1 deletions
5
.changeset/fuzzy-pans-cough.md
Normal file
5
.changeset/fuzzy-pans-cough.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Provides more information when logging a warning for accessing `Astro.request.headers` in prerendered pages
|
|
@ -437,6 +437,7 @@ async function generatePath(
|
||||||
headers: new Headers(),
|
headers: new Headers(),
|
||||||
logger,
|
logger,
|
||||||
isPrerendered: true,
|
isPrerendered: true,
|
||||||
|
routePattern: route.component
|
||||||
});
|
});
|
||||||
const renderContext = await RenderContext.create({
|
const renderContext = await RenderContext.create({
|
||||||
pipeline,
|
pipeline,
|
||||||
|
|
|
@ -20,6 +20,8 @@ export interface CreateRequestOptions {
|
||||||
* @default false
|
* @default false
|
||||||
*/
|
*/
|
||||||
isPrerendered?: boolean;
|
isPrerendered?: boolean;
|
||||||
|
|
||||||
|
routePattern: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const clientAddressSymbol = Symbol.for('astro.clientAddress');
|
const clientAddressSymbol = Symbol.for('astro.clientAddress');
|
||||||
|
@ -41,6 +43,7 @@ export function createRequest({
|
||||||
logger,
|
logger,
|
||||||
locals,
|
locals,
|
||||||
isPrerendered = false,
|
isPrerendered = false,
|
||||||
|
routePattern
|
||||||
}: CreateRequestOptions): Request {
|
}: CreateRequestOptions): Request {
|
||||||
// headers are made available on the created request only if the request is for a page that will be on-demand rendered
|
// 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
|
const headersObj = isPrerendered
|
||||||
|
@ -82,7 +85,7 @@ export function createRequest({
|
||||||
get() {
|
get() {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
null,
|
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;
|
return _headers;
|
||||||
},
|
},
|
||||||
|
|
|
@ -174,6 +174,7 @@ export async function handleRoute({
|
||||||
logger,
|
logger,
|
||||||
clientAddress: incomingRequest.socket.remoteAddress,
|
clientAddress: incomingRequest.socket.remoteAddress,
|
||||||
isPrerendered: route.prerender,
|
isPrerendered: route.prerender,
|
||||||
|
routePattern: route.component
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set user specified headers to response object.
|
// Set user specified headers to response object.
|
||||||
|
|
Loading…
Reference in a new issue