{"record":{"id":"30b52ea87965a0da","repo":"sveltejs/kit","slug":"cannot-call-fetch-eagerly-during-server-side-ren","errorCode":null,"errorMessage":"Cannot call `fetch` eagerly during server-side rendering with relative URL (${info}) — put your `fetch` calls inside `onMount` or a `load` function instead","messagePattern":"Cannot call `fetch` eagerly during server-side rendering with relative URL \\((.+?)\\) — put your `fetch` calls inside `onMount` or a `load` function instead","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/kit/src/runtime/server/page/render.js","lineNumber":231,"sourceCode":"\t\t\t\t\t\t}\n\n\t\t\t\t\t\terror = handled;\n\t\t\t\t\t\tprops.page.error = error;\n\t\t\t\t\t\tprops.page.status = status = error.status;\n\n\t\t\t\t\t\treturn error;\n\t\t\t\t\t}\n\t\t\t\t: undefined\n\t\t};\n\n\t\tconst fetch = globalThis.fetch;\n\n\t\ttry {\n\t\t\tif (DEV) {\n\t\t\t\tlet warned = false;\n\t\t\t\tglobalThis.fetch = (info, init) => {\n\t\t\t\t\tif (typeof info === 'string' && !SCHEME.test(info)) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`Cannot call \\`fetch\\` eagerly during server-side rendering with relative URL (${info}) — put your \\`fetch\\` calls inside \\`onMount\\` or a \\`load\\` function instead`\n\t\t\t\t\t\t);\n\t\t\t\t\t} else if (!warned && !try_get_request_store()?.state.is_in_remote_function) {\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t'Avoid calling `fetch` eagerly during server-side rendering — put your `fetch` calls inside `onMount` or a `load` function instead'\n\t\t\t\t\t\t);\n\t\t\t\t\t\twarned = true;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn fetch(info, init);\n\t\t\t\t};\n\t\t\t}\n\n\t\t\trendered = await with_request_store({ event, state: render_state }, async () => {\n\t\t\t\treturn render(Root, { ...render_opts, props });\n\t\t\t});\n\n\t\t\tif (rendered.hashes) {","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/runtime/server/page/render.js#L213-L249","documentation":"In dev, SvelteKit replaces `globalThis.fetch` during SSR with a wrapper that rejects relative-URL fetches executed eagerly at module top level (outside load/onMount), because there is no request context to resolve the URL against. A relative-URL call throws; even absolute-URL eager fetches produce a console warning.","triggerScenarios":"A module executes `fetch('/api/data')` at import time or module scope (not inside load, a remote function, or onMount), and the code path runs during server-side rendering in dev mode where `SCHEME.test(info)` fails for relative URLs.","commonSituations":"API clients instantiated at module level that prefetch data; libraries with side-effect fetches on import; moving fetch code out of a load function into a shared module without preserving call timing.","solutions":["Move the fetch call inside a `load` function or a remote function so it runs with request context","Move client-side fetches into `onMount` or an event handler","Use an absolute URL if the eager fetch is genuinely intentional on the server"],"exampleFix":"// before: module scope\nexport const data = fetch('/api/items').then((r) => r.json());\n// after\nexport async function load({ fetch }) {\n  const items = await fetch('/api/items').then((r) => r.json());\n  return { items };\n}","handlingStrategy":"validation","validationCode":"if (typeof window === 'undefined' && !isInLoadContext()) {\n  throw new Error('move this fetch into load or onMount');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call fetch at module top level in SSR-shared code","Use SvelteKit's load-provided fetch for server requests","Wrap client-only data fetching in onMount or event handlers"],"tags":["ssr","fetch","dev","relative-url"],"backgroundTag":"relative-url-fetch-during-ssr","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}