Skip to content

Commit 32f4c46

Browse files
committed
fix: ensure url of Request is correctly handled
1 parent 519d642 commit 32f4c46

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

packages/kit/src/runtime/client/fetcher.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,16 @@ export function create_fetch(app) {
8585
};
8686
}
8787

88-
window.fetch = async (input, init) => {
89-
const original_request = normalize_fetch_input(input, init);
90-
91-
return app.hooks.handleFetch({
92-
request: original_request,
93-
fetch: runtime_fetch
94-
});
95-
};
88+
if (BROWSER) {
89+
window.fetch = async (input, init) => {
90+
const original_request = normalize_fetch_input(input, init);
91+
92+
return app.hooks.handleFetch({
93+
request: original_request,
94+
fetch: runtime_fetch
95+
});
96+
};
97+
}
9698
}
9799

98100
const cache = new Map();
@@ -172,7 +174,7 @@ export function dev_fetch(resource, opts) {
172174
* @param {RequestInit} [opts]
173175
*/
174176
function build_selector(resource, opts) {
175-
const url = JSON.stringify(resource instanceof Request ? resource.url : resource);
177+
const url = get_selector_url(resource);
176178

177179
let selector = `script[data-sveltekit-fetched][data-url=${url}]`;
178180

@@ -194,6 +196,20 @@ function build_selector(resource, opts) {
194196
return selector;
195197
}
196198

199+
/**
200+
* Build the cache url for a given request
201+
* @param {URL | RequestInfo} resource
202+
*/
203+
function get_selector_url(resource) {
204+
if (resource instanceof Request) {
205+
resource = resource.url.startsWith(location.origin)
206+
? resource.url.slice(location.origin.length)
207+
: resource.url;
208+
}
209+
210+
return JSON.stringify(resource);
211+
}
212+
197213
/**
198214
* @param {RequestInfo | URL} info
199215
* @param {RequestInit | undefined} init
@@ -204,5 +220,5 @@ function normalize_fetch_input(info, init) {
204220
return info;
205221
}
206222

207-
return new Request(typeof info === 'string' ? new URL(info) : info, init);
223+
return new Request(typeof info === 'string' ? new URL(info, location.href) : info, init);
208224
}

0 commit comments

Comments
 (0)