{"record":{"id":"c8689036ab6239a3","repo":"sveltejs/kit","slug":"cannot-serialize-cookies-until-after-the-route-is","errorCode":null,"errorMessage":"Cannot serialize cookies until after the route is determined","messagePattern":"Cannot serialize cookies until after the route is determined","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/kit/src/runtime/server/cookie.js","lineNumber":177,"sourceCode":"\t\t\t);\n\t\t},\n\n\t\tset(name, value, options) {\n\t\t\tset_internal(name, value, { ...defaults, ...options });\n\t\t},\n\n\t\tdelete(name, options) {\n\t\t\tcookies.set(name, '', { ...options, maxAge: 0 });\n\t\t},\n\n\t\tparse: parseSetCookie,\n\n\t\tserialize(name, value, { encode, ...options }) {\n\t\t\tlet path = options.path ?? '/';\n\n\t\t\tif (!options.domain || options.domain === url.hostname) {\n\t\t\t\tif (!normalized_url) {\n\t\t\t\t\tthrow new Error('Cannot serialize cookies until after the route is determined');\n\t\t\t\t}\n\t\t\t\tpath = resolve(normalized_url, path);\n\t\t\t}\n\n\t\t\treturn stringifySetCookie({ name, value, ...defaults, ...options, path }, { encode });\n\t\t}\n\t};\n\n\t/**\n\t * @param {URL} destination\n\t * @param {string | null} header\n\t */\n\tfunction get_cookie_header(destination, header) {\n\t\t/** @type {Record<string, string>} */\n\t\tconst combined_cookies = {\n\t\t\t// cookies sent by the user agent have lowest precedence\n\t\t\t...initial_cookies\n\t\t};","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/runtime/server/cookie.js#L159-L195","documentation":"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.","triggerScenarios":"Thrown at packages/kit/src/runtime/server/cookie.js:177 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["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)","Pass an absolute path or matching domain so route-relative resolution is not required"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}