sveltejs/kit · error

Cannot read clientAddress during prerendering

Error message

Cannot read clientAddress during prerendering

What it means

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.

Source

Thrown at packages/kit/src/core/postbuild/prerender.js:389

		const dependencies = new Map();

		if (progress) {
			progress.clear();
			progress.update(decoded);

			if (Date.now() - progress.updated > 50) {
				progress.updated = Date.now();

				// without this, the update will rarely be visible, and progress will appear stuck
				await new Promise((f) => setTimeout(f, 0));
			}
		}

		const request = new Request(prerender_origin + encoded);

		const response = await server.respond(request, {
			getClientAddress() {
				throw new Error('Cannot read clientAddress during prerendering');
			},
			prerendering: {
				dependencies,
				remote_responses,
				resolved_route_ids
			},
			read: (file) => {
				// stuff we just wrote
				const filepath = saved.get(file);
				if (filepath) return readFileSync(filepath);

				// Static assets emitted during build
				if (file.startsWith(config.appDir)) {
					return readFileSync(`${out}/server/${file}`);
				}

				// stuff in `static`
				return readFileSync(join(config.files.assets, file));

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Return early or use a placeholder when `event.prerendering` is truthy instead of calling getClientAddress
  2. Exclude the route from prerendering (`export const prerender = false` on that page/route) if the IP is essential
  3. Move IP-dependent logic into client-side code or a server endpoint that is not prerendered
  4. Gate the call behind an environment check so it only runs at request time, not build time

Example fix

// before
export const load = ({ getClientAddress }) => ({ ip: getClientAddress() });

// after
export const load = ({ prerendering }) => {
  if (prerendering) return { ip: null };
  return { ip: undefined }; // resolve at request time via an endpoint
};
Defensive patterns

Strategy: validation

Validate before calling

function safeClientAddress(event) {
  if (event.prerendering) return null;
  return event.getClientAddress();
}

Type guard

function isPrerenderingEvent(event) {
  return event.prerendering != null;
}

Try / catch

function getClientAddressSafe(event) {
  try {
    return event.getClientAddress();
  } catch (e) {
    if (String(e?.message).includes('during prerendering')) return null;
    throw e;
  }
}

Prevention

When it happens

Trigger: event.getClientAddress() reached from a load function, handle hook, or endpoint during `pnpm build` prerendering (prerender = true / entries / crawling).

Common situations: 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.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/5b60eb786d18d770. Report an issue: GitHub.