Skip to content

Commit 0222125

Browse files
error in dev mode if global fetch is used with relative URL (#8370)
* error in dev mode if global fetch is used with relative URL - closes #7976 * Update packages/kit/src/exports/vite/dev/index.js Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Simon H <[email protected]>
1 parent 49cc9e9 commit 0222125

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

.changeset/cuddly-cows-vanish.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Error in dev mode if global `fetch` is used with relative URL

documentation/docs/10-getting-started/40-web-standards.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ In particular, you'll get comfortable with the following:
1212

1313
SvelteKit uses [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/fetch) for getting data from the network. It's available in [hooks](/docs/hooks) and [server routes](/docs/routing#server) as well as in the browser.
1414

15-
> A special version of `fetch` is available in [`load`](/docs/load) functions for invoking endpoints directly during server-side rendering, without making an HTTP call, while preserving credentials. (To make credentialled fetches in server-side code outside `load`, you must explicitly pass `cookie` and/or `authorization` headers.) It also allows you to make relative requests, whereas server-side `fetch` normally requires a fully qualified URL.
15+
> A special version of `fetch` is available in [`load`](/docs/load) functions, [server hooks](/docs/hooks#server-hooks) and [API routes](/docs/routing#server) for invoking endpoints directly during server-side rendering, without making an HTTP call, while preserving credentials. (To make credentialled fetches in server-side code outside `load`, you must explicitly pass `cookie` and/or `authorization` headers.) It also allows you to make relative requests, whereas server-side `fetch` normally requires a fully qualified URL.
1616
1717
Besides `fetch` itself, the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) includes the following interfaces:
1818

packages/kit/src/exports/vite/dev/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ export async function dev(vite, vite_config, svelte_config) {
2929
installPolyfills();
3030
}
3131

32+
const fetch = globalThis.fetch;
33+
globalThis.fetch = (info, init) => {
34+
if (typeof info === 'string' && !/^\w+:\/\//.test(info)) {
35+
throw new Error(
36+
`Cannot use relative URL (${info}) with global fetch — use \`event.fetch\` instead: https://kit.svelte.dev/docs/web-standards#fetch-apis`
37+
);
38+
}
39+
40+
return fetch(info, init);
41+
};
42+
3243
sync.init(svelte_config, vite_config.mode);
3344

3445
/** @type {import('types').Respond} */

0 commit comments

Comments
 (0)