Skip to content

Commit 79959b0

Browse files
fix: URL instance methods work in load (#5183)
* feat: Failing test * fix: More surgical approach to replacing url.hash * feat: Changeset * create LoadURL class * use LoadURL on the server as well Co-authored-by: Rich Harris <[email protected]>
1 parent f5b409f commit 79959b0

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

.changeset/popular-pets-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[fix] URL instance methods now work in `load`

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { onMount, tick } from 'svelte';
22
import { writable } from 'svelte/store';
33
import { coalesce_to_error } from '../../utils/error.js';
44
import { normalize } from '../load.js';
5-
import { normalize_path } from '../../utils/url.js';
5+
import { LoadURL, normalize_path } from '../../utils/url.js';
66
import {
77
create_updated_store,
88
find_anchor,
@@ -527,6 +527,7 @@ export function create_client({ target, session, base, trailing_slash }) {
527527
}
528528

529529
const session = $session;
530+
const load_url = new LoadURL(url);
530531

531532
if (module.load) {
532533
/** @type {import('types').LoadEvent} */
@@ -536,18 +537,7 @@ export function create_client({ target, session, base, trailing_slash }) {
536537
props: props || {},
537538
get url() {
538539
node.uses.url = true;
539-
540-
return new Proxy(url, {
541-
get: (target, property) => {
542-
if (property === 'hash') {
543-
throw new Error(
544-
'url.hash is inaccessible from load. Consider accessing hash from the page store within the script tag of your component.'
545-
);
546-
}
547-
548-
return Reflect.get(target, property, target);
549-
}
550-
});
540+
return load_url;
551541
},
552542
get session() {
553543
node.uses.session = true;

packages/kit/src/runtime/server/page/load_node.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { normalize } from '../../load.js';
44
import { respond } from '../index.js';
55
import { is_root_relative, resolve } from '../../../utils/url.js';
66
import { create_prerendering_url_proxy } from './utils.js';
7+
import { LoadURL } from '../../../utils/url.js';
78
import { is_pojo, lowercase_keys, normalize_request_method } from '../utils.js';
89
import { coalesce_to_error } from '../../../utils/error.js';
910
import { domain_matches, path_matches } from './cookie.js';
@@ -84,7 +85,7 @@ export async function load_node({
8485
} else if (module.load) {
8586
/** @type {import('types').LoadEvent} */
8687
const load_input = {
87-
url: state.prerendering ? create_prerendering_url_proxy(event.url) : event.url,
88+
url: state.prerendering ? create_prerendering_url_proxy(event.url) : new LoadURL(event.url),
8889
params: event.params,
8990
props: shadow.body || {},
9091
routeId: event.routeId,

packages/kit/src/utils/url.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,12 @@ export function normalize_path(path, trailing_slash) {
5252

5353
return path;
5454
}
55+
56+
export class LoadURL extends URL {
57+
/** @returns {string} */
58+
get hash() {
59+
throw new Error(
60+
'url.hash is inaccessible from load. Consider accessing hash from the page store within the script tag of your component.'
61+
);
62+
}
63+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script context="module">
2+
export const load = ({ url }) => {
3+
url.toString();
4+
return {};
5+
};
6+
</script>
7+
8+
<h1>I didn't break!</h1>

packages/kit/test/apps/basics/test/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,13 @@ test.describe.parallel('Load', () => {
15161516
}
15171517
});
15181518

1519+
test('url instance methods work in load', async ({ page, javaScriptEnabled }) => {
1520+
if (javaScriptEnabled) {
1521+
await page.goto('/load/url-to-string');
1522+
expect(await page.textContent('h1')).toBe("I didn't break!");
1523+
}
1524+
});
1525+
15191526
test('using window.fetch causes a warning', async ({ page, javaScriptEnabled }) => {
15201527
if (javaScriptEnabled && process.env.DEV) {
15211528
const warnings = [];

0 commit comments

Comments
 (0)