From ff18b9c18558dcfdae581cc1c603a9a53491c7c2 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 5 Dec 2024 12:25:01 +0000 Subject: [PATCH] chore: better warning (#12642) * chore: better warning * Apply suggestions from code review Co-authored-by: Sarah Rainsberger --------- Co-authored-by: Sarah Rainsberger --- .changeset/fuzzy-pans-cough.md | 5 +++++ packages/astro/src/core/build/generate.ts | 1 + packages/astro/src/core/request.ts | 5 ++++- packages/astro/src/vite-plugin-astro-server/route.ts | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .changeset/fuzzy-pans-cough.md diff --git a/.changeset/fuzzy-pans-cough.md b/.changeset/fuzzy-pans-cough.md new file mode 100644 index 0000000000..f813b1c356 --- /dev/null +++ b/.changeset/fuzzy-pans-cough.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Provides more information when logging a warning for accessing `Astro.request.headers` in prerendered pages diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 1cc25aaee7..84d344bfbb 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -437,6 +437,7 @@ async function generatePath( headers: new Headers(), logger, isPrerendered: true, + routePattern: route.component }); const renderContext = await RenderContext.create({ pipeline, diff --git a/packages/astro/src/core/request.ts b/packages/astro/src/core/request.ts index 624bee8798..98d4402c6d 100644 --- a/packages/astro/src/core/request.ts +++ b/packages/astro/src/core/request.ts @@ -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; }, diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index b4158b854d..978de421a8 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -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.