{"record":{"id":"5b60eb786d18d770","repo":"sveltejs/kit","slug":"cannot-read-clientaddress-during-prerendering-5b60eb","errorCode":null,"errorMessage":"Cannot read clientAddress during prerendering","messagePattern":"Cannot read clientAddress during prerendering","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/kit/src/core/postbuild/prerender.js","lineNumber":389,"sourceCode":"\t\tconst dependencies = new Map();\n\n\t\tif (progress) {\n\t\t\tprogress.clear();\n\t\t\tprogress.update(decoded);\n\n\t\t\tif (Date.now() - progress.updated > 50) {\n\t\t\t\tprogress.updated = Date.now();\n\n\t\t\t\t// without this, the update will rarely be visible, and progress will appear stuck\n\t\t\t\tawait new Promise((f) => setTimeout(f, 0));\n\t\t\t}\n\t\t}\n\n\t\tconst request = new Request(prerender_origin + encoded);\n\n\t\tconst response = await server.respond(request, {\n\t\t\tgetClientAddress() {\n\t\t\t\tthrow new Error('Cannot read clientAddress during prerendering');\n\t\t\t},\n\t\t\tprerendering: {\n\t\t\t\tdependencies,\n\t\t\t\tremote_responses,\n\t\t\t\tresolved_route_ids\n\t\t\t},\n\t\t\tread: (file) => {\n\t\t\t\t// stuff we just wrote\n\t\t\t\tconst filepath = saved.get(file);\n\t\t\t\tif (filepath) return readFileSync(filepath);\n\n\t\t\t\t// Static assets emitted during build\n\t\t\t\tif (file.startsWith(config.appDir)) {\n\t\t\t\t\treturn readFileSync(`${out}/server/${file}`);\n\t\t\t\t}\n\n\t\t\t\t// stuff in `static`\n\t\t\t\treturn readFileSync(join(config.files.assets, file));","sourceCodeStart":371,"sourceCodeEnd":407,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/core/postbuild/prerender.js#L371-L407","documentation":"While prerendering real routes, the Server instance is handed a getClientAddress implementation that throws. Any app code invoked during prerendering (hooks, load functions, +server.js) that reads the client IP triggers this error, because a prerendered page has no client.","triggerScenarios":"event.getClientAddress() reached from a load function, handle hook, or endpoint during `pnpm build` prerendering (prerender = true / entries / crawling).","commonSituations":"IP-based logging in hooks.server.js; header analysis (x-forwarded-for fallback) that falls back to getClientAddress; pages newly marked prerender that previously ran server-only at request time.","solutions":["Return early or use a placeholder when `event.prerendering` is truthy instead of calling getClientAddress","Exclude the route from prerendering (`export const prerender = false` on that page/route) if the IP is essential","Move IP-dependent logic into client-side code or a server endpoint that is not prerendered","Gate the call behind an environment check so it only runs at request time, not build time"],"exampleFix":"// before\nexport const load = ({ getClientAddress }) => ({ ip: getClientAddress() });\n\n// after\nexport const load = ({ prerendering }) => {\n  if (prerendering) return { ip: null };\n  return { ip: undefined }; // resolve at request time via an endpoint\n};","handlingStrategy":"validation","validationCode":"function safeClientAddress(event) {\n  if (event.prerendering) return null;\n  return event.getClientAddress();\n}","typeGuard":"function isPrerenderingEvent(event) {\n  return event.prerendering != null;\n}","tryCatchPattern":"function getClientAddressSafe(event) {\n  try {\n    return event.getClientAddress();\n  } catch (e) {\n    if (String(e?.message).includes('during prerendering')) return null;\n    throw e;\n  }\n}","preventionTips":["Centralize IP access in one helper that checks prerendering state","Mark IP-dependent routes with `export const prerender = false`","Run `pnpm build` locally with prerendering before CI to catch these early"],"tags":["prerender","client-address","build-time"],"backgroundTag":"client-address-during-prerendering","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}