sveltejs/kit · error

Cannot serialize cookies until after the route is determined

Error message

Cannot serialize cookies until after the route is determined

What it means

Runtime guard in cookie serialize(): cookie paths are resolved relative to the matched route's normalized URL, but at the point serialize() was called the route (and thus normalized_url) has not yet been determined — typically because cookies are being serialized eagerly during initial setup, before routing completed. Serialization must wait until the route is known so relative paths can be expanded correctly.

Source

Thrown at packages/kit/src/runtime/server/cookie.js:177

			);
		},

		set(name, value, options) {
			set_internal(name, value, { ...defaults, ...options });
		},

		delete(name, options) {
			cookies.set(name, '', { ...options, maxAge: 0 });
		},

		parse: parseSetCookie,

		serialize(name, value, { encode, ...options }) {
			let path = options.path ?? '/';

			if (!options.domain || options.domain === url.hostname) {
				if (!normalized_url) {
					throw new Error('Cannot serialize cookies until after the route is determined');
				}
				path = resolve(normalized_url, path);
			}

			return stringifySetCookie({ name, value, ...defaults, ...options, path }, { encode });
		}
	};

	/**
	 * @param {URL} destination
	 * @param {string | null} header
	 */
	function get_cookie_header(destination, header) {
		/** @type {Record<string, string>} */
		const combined_cookies = {
			// cookies sent by the user agent have lowest precedence
			...initial_cookies
		};

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Defer cookie serialization until after the request has been routed (e.g. in a handle hook after resolve, or via event.cookies in an endpoint/action)
  2. Pass an absolute path or matching domain so route-relative resolution is not required
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/kit/src/runtime/server/cookie.js:177 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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