Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow calling fetch for any scheme #10699

Merged
merged 7 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/kit/src/exports/vite/dev/index.js
Expand Up @@ -15,6 +15,7 @@ import * as sync from '../../../core/sync/sync.js';
import { get_mime_lookup, runtime_base } from '../../../core/utils.js';
import { compact } from '../../../utils/array.js';
import { not_found } from '../utils.js';
import { SCHEME } from '../../../utils/url.js';

const cwd = process.cwd();

Expand All @@ -31,7 +32,7 @@ export async function dev(vite, vite_config, svelte_config) {

const fetch = globalThis.fetch;
globalThis.fetch = (info, init) => {
if (typeof info === 'string' && !/^\w+:\/\//.test(info)) {
if (typeof info === 'string' && !SCHEME.test(info)) {
throw new Error(
`Cannot use relative URL (${info}) with global fetch — use \`event.fetch\` instead: https://kit.svelte.dev/docs/web-standards#fetch-apis`
);
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Expand Up @@ -12,6 +12,7 @@ import { public_env } from '../../shared-server.js';
import { text } from '../../../exports/index.js';
import { create_async_iterator } from '../../../utils/streaming.js';
import { SVELTE_KIT_ASSETS } from '../../../constants.js';
import { SCHEME } from '../../../utils/url.js';

// TODO rename this function/module

Expand Down Expand Up @@ -151,7 +152,7 @@ export async function render_response({
const fetch = globalThis.fetch;
let warned = false;
globalThis.fetch = (info, init) => {
if (typeof info === 'string' && !/^\w+:\/\//.test(info)) {
if (typeof info === 'string' && !SCHEME.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`
);
Expand Down
9 changes: 7 additions & 2 deletions packages/kit/src/utils/url.js
@@ -1,14 +1,19 @@
import { BROWSER } from 'esm-env';

/**
* Matches a URI scheme. See https://www.rfc-editor.org/rfc/rfc3986#section-3.1
* @type {RegExp}
*/
export const SCHEME = /^[a-z][a-z\d+\-.]+:/i;

const absolute = /^([a-z]+:)?\/?\//;
const scheme = /^[a-z]+:/;

/**
* @param {string} base
* @param {string} path
*/
export function resolve(base, path) {
if (scheme.test(path)) return path;
if (SCHEME.test(path)) return path;
if (path[0] === '#') return base + path;

const base_match = absolute.exec(base);
Expand Down