Skip to content

Commit

Permalink
chore: better warning (withastro#12642)
Browse files Browse the repository at this point in the history
* chore: better warning

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <[email protected]>

---------

Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
ematipico and sarah11918 authored Dec 5, 2024
1 parent 22e405a commit ff18b9c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-pans-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Provides more information when logging a warning for accessing `Astro.request.headers` in prerendered pages
1 change: 1 addition & 0 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ async function generatePath(
headers: new Headers(),
logger,
isPrerendered: true,
routePattern: route.component
});
const renderContext = await RenderContext.create({
pipeline,
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/core/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface CreateRequestOptions {
* @default false
*/
isPrerendered?: boolean;

routePattern: string
}

const clientAddressSymbol = Symbol.for('astro.clientAddress');
Expand All @@ -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
Expand Down Expand Up @@ -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;
},
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ff18b9c

Please sign in to comment.