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

chore: better warning #12642

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading