sveltejs/kit · error · Error

Cannot use prerendering if page template contains %sveltekit

Error message

Cannot use prerendering if page template contains %sveltekit.nonce%

What it means

The prerendered app template must not contain the literal `%sveltekit.nonce%` placeholder, because static HTML cannot substitute a per-request nonce. SvelteKit detects that the template includes the nonce placeholder while prerendering and throws.

Source

Thrown at packages/kit/src/runtime/server/page/render.js:69

	branch,
	fetched,
	page_config,
	status,
	error = null,
	event,
	state,
	resolve_opts,
	action_result,
	data_serializer,
	error_components
}) {
	if (state.prerendering || state.prerender_default === true) {
		if (options.csp.mode === 'nonce') {
			throw new Error('Cannot use prerendering if config.csp.mode === "nonce"');
		}

		if (options.app_template_contains_nonce) {
			throw new Error('Cannot use prerendering if page template contains %sveltekit.nonce%');
		}
	}

	const client = manifest.client;

	const modulepreloads = new Set(client?.imports);
	const stylesheets = new Set(client?.stylesheets);

	/** @type {Map<string, import('types').FontDependency>} */
	const fonts = new Map(client?.fonts.map((font) => [font.file, font]));

	/**
	 * The value of the Link header that is added to the response when not prerendering
	 * @type {Set<string>}
	 */
	const link_headers = new Set();

	/** @type {Map<string, string>} */

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Remove `%sveltekit.nonce%` from src/app.html and let SvelteKit inject CSP directives automatically
  2. Disable prerendering on pages that need the nonce placeholder
  3. Switch CSP mode to 'hash' so a nonce placeholder is unnecessary

Example fix

<!-- before: src/app.html -->
<script nonce="%sveltekit.nonce%">...</script>
<!-- after -->
%%sveltekit.head%%
Defensive patterns

Strategy: validation

Validate before calling

import { readFileSync } from 'node:fs';
const ok = !readFileSync('src/app.html', 'utf8').includes('%sveltekit.nonce%');

Prevention

When it happens

Trigger: Prerendering is active (state.prerendering or prerender_default === true) and `options.app_template_contains_nonce` is true, i.e. `src/app.html` contains the string `%sveltekit.nonce%`.

Common situations: Manually adding `%sveltekit.nonce%` to app.html for custom CSP handling while some pages are prerendered; upgrading SvelteKit where the template placeholder requirement changed and an old custom app.html still contains it.

Related errors


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