{"record":{"id":"0894256b00bcd235","repo":"sveltejs/kit","slug":"cannot-use-relative-url-info-with-global-fetc","errorCode":null,"errorMessage":"Cannot use relative URL (${info}) with global fetch — use `event.fetch` instead: https://svelte.dev/docs/kit/web-standards#fetch-apis","messagePattern":"Cannot use relative URL \\((.+?)\\) with global fetch — use `event\\.fetch` instead: https://svelte\\.dev/docs/kit/web-standards#fetch-apis","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/kit/src/exports/vite/dev/index.js","lineNumber":67,"sourceCode":"\t/** @type {AsyncLocalStorage<{ event: RequestEvent, config: any, prerender: PrerenderOption }>} */\n\tconst async_local_storage = new AsyncLocalStorage();\n\n\tglobalThis.__SVELTEKIT_TRACK__ = (label) => {\n\t\tconst context = async_local_storage.getStore();\n\t\tif (!context || context.prerender === true) return;\n\n\t\tcheck_feature(\n\t\t\t/** @type {string} */ (context.event.route.id),\n\t\t\tcontext.config,\n\t\t\tlabel,\n\t\t\tsvelte_config.adapter\n\t\t);\n\t};\n\n\tconst fetch = globalThis.fetch;\n\tglobalThis.fetch = (info, init) => {\n\t\tif (typeof info === 'string' && !SCHEME.test(info)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Cannot use relative URL (${info}) with global fetch — use \\`event.fetch\\` instead: https://svelte.dev/docs/kit/web-standards#fetch-apis`\n\t\t\t);\n\t\t}\n\n\t\treturn fetch(info, init);\n\t};\n\n\twrite_tsconfig(svelte_config, root);\n\n\t/** @type {ManifestData} */\n\tlet manifest_data;\n\t/** @type {SSRManifest} */\n\tlet manifest;\n\n\t/** @type {Error | null} */\n\tlet manifest_error = null;\n\n\tconst runner = get_runner(vite, vite_dev_server);","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/exports/vite/dev/index.js#L49-L85","documentation":"In dev, SvelteKit patches `globalThis.fetch` to catch a common mistake: calling global `fetch` with a relative URL (e.g. `fetch('/api/data')`). Relative URLs are invalid for global fetch because there is no request context to resolve them against; inside server code you must use `event.fetch`, which resolves relative URLs against the current request.","triggerScenarios":"Any server-side code (load function without event.fetch, +server.ts handler, hooks, instrumentation) calling `fetch('/path')` with a string lacking a scheme during `vite dev`.","commonSituations":"Code copied from client components into server load; shared data-fetching modules that use global fetch; unit-tested fetch wrappers that worked with a mocked base URL.","solutions":["Use `event.fetch` (or `fetch` from the load/action/`RequestEvent` context) instead of global fetch","Prefix the URL with an absolute origin, e.g. `fetch(url.origin + '/api/data')` using `event.url`","Pass the event/fetch into shared helper functions so relative URLs resolve correctly","Restructure to call internal functions directly instead of fetching your own API"],"exampleFix":"// before\nexport async function load() {\n  const res = await fetch('/api/data');\n}\n// after\nexport async function load({ fetch }) {\n  const res = await fetch('/api/data');\n}","handlingStrategy":"type-guard","validationCode":"function assertAbsoluteUrl(url) {\n  if (/^\\/[^^]*|^\\?/.test(url)) {\n    throw new TypeError(`Use event.fetch for relative URLs: ${url}`);\n  }\n}","typeGuard":"function isAbsoluteUrl(url) {\n  return typeof url !== 'string' || !!SCHEME_LIKE.test(url) || URL.canParse(url) && new URL(url).origin !== 'null';\n}","tryCatchPattern":"try {\n  const res = await globalFetch('/api/data');\n} catch (err) {\n  if (/relative URL.*global fetch/.test(err.message)) {\n    console.error('Switch to event.fetch in server code');\n  }\n  throw err;\n}","preventionTips":["Always destructure `fetch` from load/action/event context in server code","Lint against bare `fetch(` in server-only directories","Pass fetch explicitly into shared helper modules","Use absolute origins when you must call an external service from server code"],"tags":["fetch","relative-url","dev","server"],"backgroundTag":"relative-url-global-fetch","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}