sveltejs/kit · error · Error

Cannot access url.${property} on a page with prerendering en

Error message

Cannot access url.${property} on a page with prerendering enabled

What it means

@sveltejs/kit's `disable_search` replaces the `search` and `searchParams` accessors on the request URL with getters that always throw, so code cannot read the query string during prerendering. Prerendered pages must be identical for every visitor, so the query string is meaningless; the library throws to surface accidental reliance on it instead of silently producing wrong output.

Source

Thrown at packages/kit/src/utils/url.js:204

		get() {
			throw new Error(
				'Cannot access event.url.hash. Consider using `page.url.hash` inside a component instead'
			);
		}
	});
}

/**
 * Disallow access to `url.search` and `url.searchParams` during prerendering
 * @param {URL} url
 */
export function disable_search(url) {
	allow_nodejs_console_log(url);

	for (const property of ['search', 'searchParams']) {
		Object.defineProperty(url, property, {
			get() {
				throw new Error(`Cannot access url.${property} on a page with prerendering enabled`);
			}
		});
	}
}

/**
 * Allow URL to be console logged, bypassing disabled properties.
 * @param {URL} url
 */
function allow_nodejs_console_log(url) {
	if (!BROWSER) {
		// @ts-ignore
		url[Symbol.for('nodejs.util.inspect.custom')] = (_depth, opts, inspect) => {
			return inspect(new URL(url), opts);
		};
	}
}

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Remove any read of `url.search`/`url.searchParams` from code paths that run during prerendering.
  2. Disable prerendering for routes that legitimately need the query string (`export const prerender = false`).
  3. Use page.url only for non-query parts (origin, pathname) in prerendered pages.
  4. Move query-string-dependent logic to client-side code or a server endpoint instead of the prerendered load.

Example fix

// before (prerendered route)
export function load({ url }) {
  const token = url.searchParams.get('token');
  return { token };
}

// after: make the route dynamic
export const prerender = false;
export function load({ url }) {
  const token = url.searchParams.get('token');
  return { token };
}
Defensive patterns

Strategy: validation

Validate before calling

if (import.meta.env.SSR && should_prerender && (url.search || url.searchParams.size)) {
  throw new Error('query string used in prerendered route');
}

Type guard

function has_query(url) { return url.search.length > 0; }

Try / catch

try {
  const q = url.searchParams.get('page');
} catch {
  const q = null; // prerendering: fall back to default
}

Prevention

When it happens

Trigger: Accessing `url.search` or `url.searchParams` (directly or via helpers like `new URLSearchParams(url.search)`) inside load functions, page data, or handlers on a route being prerendered (`export const prerender = true` or global prerender config).

Common situations: Reading `?page=` or `?token=` from a URL in a prerendered page; calling `fetch(url)` with a URL object whose searchParams are read for cache-busting; sharing a load function between prerendered and dynamic routes without guarding query-string access.

Related errors


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