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: correctly serialize request url when using load fetch #8876

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-icons-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly serialize request url when using load `fetch`
11 changes: 8 additions & 3 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,17 @@ export function create_client({ target }) {
}

// we must fixup relative urls so they are resolved from the target page
const resolved = new URL(requested, url).href;
depends(resolved);
const resolved = new URL(requested, url);
depends(resolved.href);

// match ssr serialized data url, which is important to find cached responses
if (resolved.origin === url.origin) {
requested = resolved.href.slice(url.origin.length);
}

// prerendered pages may be served from any origin, so `initial_fetch` urls shouldn't be resolved
return started
? subsequent_fetch(requested, resolved, init)
? subsequent_fetch(requested, resolved.href, init)
: initial_fetch(requested, init);
},
setHeaders: () => {}, // noop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** @type {import('./$types').PageLoad} */
export async function load({ fetch, data }) {
export async function load({ fetch, data, url }) {
const { a } = data;

const res = await fetch('/load/serialization/fetched-from-shared.json');
const res = await fetch(new URL('/load/serialization/fetched-from-shared.json', url.origin));
const { b } = await res.json();

return { a, b, c: a + b };
Expand Down
4 changes: 3 additions & 1 deletion packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ test.describe('Load', () => {

if (!javaScriptEnabled) {
// by the time JS has run, hydration will have nuked these scripts
const script_contents = await page.innerHTML('script[data-sveltekit-fetched]');
const script_contents = await page.innerHTML(
'script[data-sveltekit-fetched][data-url="/load/serialization/fetched-from-shared.json"]'
);

const payload = '{"status":200,"statusText":"","headers":{},"body":"{\\"b\\":2}"}';

Expand Down