Skip to content

Commit f451f6c

Browse files
authored
fix: use window.fetch for server load fetch requests (#13315)
This PR ensures that this server-load data request is also patchable by basically using the same mechanism introduced in #10009
1 parent 00e1a76 commit f451f6c

File tree

8 files changed

+62
-10
lines changed

8 files changed

+62
-10
lines changed

Diff for: .changeset/funny-moles-scream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: use current `window.fetch` for server load fetch requests

Diff for: packages/kit/src/runtime/client/client.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ import {
88
make_trackable,
99
normalize_path
1010
} from '../../utils/url.js';
11-
import {
12-
initial_fetch,
13-
lock_fetch,
14-
native_fetch,
15-
subsequent_fetch,
16-
unlock_fetch
17-
} from './fetcher.js';
11+
import { dev_fetch, initial_fetch, lock_fetch, subsequent_fetch, unlock_fetch } from './fetcher.js';
1812
import { parse } from './parse.js';
1913
import * as storage from './session-storage.js';
2014
import {
@@ -2548,7 +2542,9 @@ async function load_data(url, invalid) {
25482542
}
25492543
data_url.searchParams.append(INVALIDATED_PARAM, invalid.map((i) => (i ? '1' : '0')).join(''));
25502544

2551-
const res = await native_fetch(data_url.href);
2545+
// use window.fetch directly to allow using a 3rd party-patched fetch implementation
2546+
const fetcher = DEV ? dev_fetch : window.fetch;
2547+
const res = await fetcher(data_url.href, {});
25522548

25532549
if (!res.ok) {
25542550
// error message is a JSON-stringified string which devalue can't handle at the top level

Diff for: packages/kit/src/runtime/client/fetcher.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { b64_decode } from '../utils.js';
55
let loading = 0;
66

77
/** @type {typeof fetch} */
8-
export const native_fetch = BROWSER ? window.fetch : /** @type {any} */ (() => {});
8+
const native_fetch = BROWSER ? window.fetch : /** @type {any} */ (() => {});
99

1010
export function lock_fetch() {
1111
loading += 1;
@@ -137,7 +137,7 @@ export function subsequent_fetch(resource, resolved, opts) {
137137
* @param {RequestInfo | URL} resource
138138
* @param {RequestInit & Record<string, any> | undefined} opts
139139
*/
140-
function dev_fetch(resource, opts) {
140+
export function dev_fetch(resource, opts) {
141141
const patched_opts = { ...opts };
142142
// This assigns the __sveltekit_fetch__ flag and makes it non-enumerable
143143
Object.defineProperty(patched_opts, '__sveltekit_fetch__', {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function load() {
2+
return { msg: 'server load data' };
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<p>
6+
This page makes a fetch request to the server to get the data from the server load function when
7+
users navigate to it.
8+
</p>
9+
10+
<h1>{data.msg}</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function load() {
2+
return { msg: 'server load data' };
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
import { browser } from '$app/environment';
3+
4+
if (browser) {
5+
const original_fetch = window.fetch;
6+
window.fetch = (input, init) => {
7+
console.log('Called a patched window.fetch for server load request');
8+
return original_fetch(input, init);
9+
};
10+
}
11+
</script>
12+
13+
<p>
14+
The sole purpose of this page is to apply a `window.fetch` patch before navigating to the next
15+
page. Click the link below to navigate to the next page with a server load function.
16+
</p>
17+
18+
<a href="./patching-server-load-ii">Go To Page with Server Load</a>

Diff for: packages/kit/test/apps/basics/test/client.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@ test.describe('Load', () => {
252252
expect(logs).toContain('Called a patched window.fetch');
253253
});
254254

255+
test('permits 3rd party patching of server load fetch requests', async ({ page }) => {
256+
const logs = [];
257+
page.on('console', (msg) => {
258+
if (msg.type() === 'log') {
259+
logs.push(msg.text());
260+
}
261+
});
262+
263+
await page.goto('/load/window-fetch/patching-server-load');
264+
265+
await page.getByText('Go To Page with Server Load').click();
266+
267+
expect(await page.textContent('h1')).toBe('server load data');
268+
269+
expect(logs).toContain('Called a patched window.fetch for server load request');
270+
});
271+
255272
test('does not repeat fetch on hydration when using Request object', async ({ page }) => {
256273
const requests = [];
257274
page.on('request', (request) => {

0 commit comments

Comments
 (0)