Skip to content

Commit 930c8e4

Browse files
fix: adjust fetch error message on the server (#8551)
* fix: adjust fetch error message on the server closes #8536 * make it possible to still use them in #await blocks * undo #await dance * put additional dev time check inside render.js instead * Create five-pumpkins-join.md * prevent corrupted state * Delete nice-mayflies-compete.md --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent eb94356 commit 930c8e4

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Diff for: .changeset/five-pumpkins-join.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
fix: provide helpful error/warning when calling `fetch` during render

Diff for: packages/kit/src/runtime/server/page/render.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,32 @@ export async function render_response({
113113
form: form_value
114114
};
115115

116-
rendered = options.root.render(props);
116+
if (__SVELTEKIT_DEV__) {
117+
const fetch = globalThis.fetch;
118+
let warned = false;
119+
globalThis.fetch = (info, init) => {
120+
if (typeof info === 'string' && !/^\w+:\/\//.test(info)) {
121+
throw new Error(
122+
`Cannot call \`fetch\` eagerly during server side rendering with relative URL (${info}) — put your \`fetch\` calls inside \`onMount\` or a \`load\` function instead`
123+
);
124+
} else if (!warned) {
125+
console.warn(
126+
`Avoid calling \`fetch\` eagerly during server side rendering — put your \`fetch\` calls inside \`onMount\` or a \`load\` function instead`
127+
);
128+
warned = true;
129+
}
130+
131+
return fetch(info, init);
132+
};
133+
134+
try {
135+
rendered = options.root.render(props);
136+
} finally {
137+
globalThis.fetch = fetch;
138+
}
139+
} else {
140+
rendered = options.root.render(props);
141+
}
117142

118143
for (const { node } of branch) {
119144
if (node.imports) {

0 commit comments

Comments
 (0)