{"record":{"id":"f7409b32425ccaf8","repo":"sveltejs/kit","slug":"cannot-access-event-url-hash-consider-using-page","errorCode":null,"errorMessage":"Cannot access event.url.hash. Consider using `page.url.hash` inside a component instead","messagePattern":"Cannot access event\\.url\\.hash\\. Consider using `page\\.url\\.hash` inside a component instead","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/kit/src/utils/url.js","lineNumber":187,"sourceCode":"\t}\n\n\tif ((DEV || !BROWSER) && !allow_hash) {\n\t\tdisable_hash(tracked);\n\t}\n\n\treturn tracked;\n}\n\n/**\n * Disallow access to `url.hash` on the server and in `load`\n * @param {URL} url\n */\nfunction disable_hash(url) {\n\tallow_nodejs_console_log(url);\n\n\tObject.defineProperty(url, 'hash', {\n\t\tget() {\n\t\t\tthrow new Error(\n\t\t\t\t'Cannot access event.url.hash. Consider using `page.url.hash` inside a component instead'\n\t\t\t);\n\t\t}\n\t});\n}\n\n/**\n * Disallow access to `url.search` and `url.searchParams` during prerendering\n * @param {URL} url\n */\nexport function disable_search(url) {\n\tallow_nodejs_console_log(url);\n\n\tfor (const property of ['search', 'searchParams']) {\n\t\tObject.defineProperty(url, property, {\n\t\t\tget() {\n\t\t\t\tthrow new Error(`Cannot access url.${property} on a page with prerendering enabled`);\n\t\t\t}","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/utils/url.js#L169-L205","documentation":"Server-side `event.url` in SvelteKit is a URL whose `hash` property is redefined to throw, because the URL fragment is never sent to the server — the server simply cannot know it. Accessing `event.url.hash` in server code (load functions, hooks, endpoints) therefore throws immediately, with a message pointing to `page.url.hash` in components where the client-side value is available.","triggerScenarios":"Reading `event.url.hash` (directly or via destructuring/logging) inside `+page.server.ts`/`+server.ts` load functions, server hooks (`handle`, `locals`), or `getRequestEvent` context on the server.","commonSituations":"Logging the full URL server-side (`console.log(event.url.href)` still works, but explicit `.hash` access throws); porting client code that reads `page.url.hash` into a server module; building canonical URLs and including hash defensively; generic URL-manipulation helpers shared between server and client.","solutions":["Move the hash-dependent logic into a component/universal load using `page.url.hash` from `$app/stores`/`$app/state`","Drop the hash on the server — it is always absent from requests, so use `event.url.pathname + event.url.search`","Guard with `browser` (from `$app/environment`) when code runs in both environments"],"exampleFix":"// before (+page.server.ts)\nconst full = event.url.href; // or event.url.hash\n// after (+page.svelte.ts / component)\nimport { page } from '$app/state';\nconst hash = page.url.hash; // client only; server never sees the fragment","handlingStrategy":"try-catch","validationCode":"import { browser } from '$app/environment';\nif (!browser) {\n\t// never read .hash on server-side event.url\n\tconsole.assert(!('hashAccess' in plan), 'logic must not rely on url.hash server-side');\n}","typeGuard":"function canReadHash(url) {\n\ttry {\n\t\tvoid url.hash;\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n}","tryCatchPattern":"let hash = '';\ntry {\n\thash = event.url.hash;\n} catch {\n\t// fragment never reaches the server; fall back to pathname + search\n\tconst url = event.url.pathname + event.url.search;\n}","preventionTips":["Never read event.url.hash in server modules — the fragment is not sent in HTTP requests","Use page.url.hash in components/universal code for the client-side fragment","For shared helpers, branch on `browser` from $app/environment before touching .hash","When logging URLs server-side, build from pathname + search, not href with hash access"],"tags":["url","ssr","server","hash"],"backgroundTag":"url-hash-not-available-on-server","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}