{"record":{"id":"475e6e82dbeb844c","repo":"withastro/astro","slug":"astro-request-headers-was-used-when-rendering-th","errorCode":null,"errorMessage":"`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.","messagePattern":"`Astro\\.request\\.headers` was used when rendering the route `(.+?)'`\\. `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\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/astro/src/core/request.ts","lineNumber":84,"sourceCode":"\t\tmethod: method,\n\t\theaders: headersObj,\n\t\t// body is made available only if the request is for a page that will be on-demand rendered\n\t\tbody: isPrerendered ? null : body,\n\t\t...init,\n\t});\n\n\tif (isPrerendered) {\n\t\t// Warn when accessing headers in SSG mode\n\t\tlet _headers = request.headers;\n\n\t\t// We need to remove descriptor's value and writable properties because we're adding getters and setters.\n\t\tconst { value, writable, ...headersDesc } =\n\t\t\tObject.getOwnPropertyDescriptor(request, 'headers') || {};\n\n\t\tObject.defineProperty(request, 'headers', {\n\t\t\t...headersDesc,\n\t\t\tget() {\n\t\t\t\tlogger.warn(\n\t\t\t\t\tnull,\n\t\t\t\t\t`\\`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.`,\n\t\t\t\t);\n\t\t\t\treturn _headers;\n\t\t\t},\n\t\t\tset(newHeaders: Headers) {\n\t\t\t\t_headers = newHeaders;\n\t\t\t},\n\t\t});\n\t}\n\n\treturn request;\n}\n","sourceCodeStart":66,"sourceCodeEnd":98,"githubUrl":"https://github.com/withastro/astro/blob/52e6c34790cc8ac4e69e6135ace06049867e5c4a/packages/astro/src/core/request.ts#L66-L98","documentation":"During prerendering there is no incoming request, so request.headers only holds placeholder values. getRequest replaces the headers property with a getter that warns when a prerendered route reads it and returns the meaningless build-time headers — logic depending on real headers (auth, cookies, locale) silently misbehaves.","triggerScenarios":"A page with prerender enabled (default under output 'static', or export const prerender = true) reads Astro.request.headers or calls .get() on it in frontmatter or components.","commonSituations":"Copying header/cookie logic from an SSR page into a static one; switching output from 'server' to 'static' without flagging header-dependent pages; reading user-agent or cookie-based theming on a marketing/blog page.","solutions":["Opt that page into SSR: add `export const prerender = false;` at the top of the file","Set output: 'server' in astro.config when most pages need request data","Move the header-dependent logic out of the prerendered page (client-side script, middleware, or an API endpoint)"],"exampleFix":"// src/pages/dashboard.astro — before\nexport const prerender = true;\nconst token = Astro.request.headers.get('authorization');\n\n// after\nexport const prerender = false; // server-render so headers are real\nconst token = Astro.request.headers.get('authorization');","handlingStrategy":"validation","validationCode":"export const prerender = true;\nconst ua = prerender ? undefined : Astro.request.headers.get('user-agent');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Audit every Astro.request.headers/cookies access when flipping a page to prerendered","Group header-dependent routes under output 'server' or per-page prerender = false","Centralize header reads in a helper that takes the value as an explicit argument so static pages cannot call it"],"tags":["prerender","ssg","headers","request","hybrid-rendering"],"backgroundTag":"server-api-in-static-build","analyzedSha":"52e6c34790cc8ac4e69e6135ace06049867e5c4a","analyzedAt":"2026-08-18T18:48:03.901Z","contentChangedAt":"2026-08-18T18:48:03.901Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}