Skip to content

Commit

Permalink
put additional dev time check inside render.js instead
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Jan 26, 2023
1 parent f0cd825 commit b18c839
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function dev(vite, vite_config, svelte_config) {
globalThis.fetch = (info, init) => {
if (typeof info === 'string' && !/^\w+:\/\//.test(info)) {
throw new Error(
`Cannot use relative URL (${info}) with global fetch on the server. If this is called inside a \`load\` function, use \`event.fetch\` instead: https://kit.svelte.dev/docs/web-standards#fetch-apis`
`Cannot use relative URL (${info}) with global fetch use \`event.fetch\` instead: https://kit.svelte.dev/docs/web-standards#fetch-apis`
);
}

Expand Down
25 changes: 24 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,30 @@ export async function render_response({
form: form_value
};

rendered = options.root.render(props);
if (__SVELTEKIT_DEV__) {
const fetch = globalThis.fetch;
let warned = false;
globalThis.fetch = (info, init) => {
if (typeof info === 'string' && !/^\w+:\/\//.test(info)) {
throw new Error(
`Cannot call \`fetch\` eagerly during server side rendering with relative URL (${info}) — put your \`fetch\` calls inside \`onMount\` or a \`load\` function instead`
);
} else if (!warned) {
console.warn(
`Avoid calling \`fetch\` eagerly during server side rendering — put your \`fetch\` calls inside \`onMount\` or a \`load\` function instead`
);
warned = true;
}

return fetch(info, init);
};

rendered = options.root.render(props);

globalThis.fetch = fetch;
} else {
rendered = options.root.render(props);
}

for (const { node } of branch) {
if (node.imports) {
Expand Down

0 comments on commit b18c839

Please sign in to comment.