Skip to content

Commit ca583af

Browse files
author
rtrampox
committed
fix(#26): create a new URLSearchParams object on searchParams and getSearchParams options of the SvelteKit adapter
1 parent 6dcc899 commit ca583af

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

packages/nuqs-svelte/src/lib/adapters/svelte-kit/adapter.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
const { history, scroll, shallow } = options;
1919
2020
if (browser) {
21-
const url = new URL(location.href);
21+
const url = new URL(page.url);
2222
url.search = renderQueryString(search);
2323
2424
if (shallow) {
@@ -41,8 +41,8 @@
4141
}
4242
},
4343
44-
searchParams: () => page.url.searchParams,
45-
getSearchParamsSnapshot: () => page.url.searchParams,
44+
searchParams: () => new URLSearchParams(page.url.searchParams),
45+
getSearchParamsSnapshot: () => new URLSearchParams(page.url.searchParams),
4646
};
4747
</script>
4848

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const load = ({ url }) => {
2+
const switchValue = url.searchParams.get("switch");
3+
const query = url.searchParams.get("q");
4+
5+
return {
6+
query,
7+
switch: switchValue,
8+
time: new Date(),
9+
};
10+
};
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<script lang="ts">
2-
import { goto } from "$app/navigation";
32
import { parseAsBoolean, parseAsString } from "$lib/parsers";
43
import { useQueryState } from "$lib/use-query-state.svelte";
54
5+
let { data } = $props();
6+
67
const query = useQueryState("q", parseAsString.withDefault("string-value"));
78
8-
const switchState = useQueryState("switch", parseAsBoolean.withDefault(false));
9+
const switchState = useQueryState(
10+
"switch",
11+
parseAsBoolean.withDefault(false).withOptions({ shallow: false }),
12+
);
913
</script>
1014

1115
<div class="flex flex-col gap-2">
@@ -15,16 +19,6 @@
1519
placeholder="Search..."
1620
class="w-full rounded-md bg-neutral-900 p-2 text-white ring ring-neutral-600"
1721
/>
18-
19-
<button
20-
type="button"
21-
onclick={() => goto(`?switch=${!switchState.current}`)}
22-
class="rounded-md p-2 text-white ring ring-neutral-600
23-
{switchState.current ? 'bg-green-500' : 'bg-neutral-900'}"
24-
>
25-
Switch with goto
26-
</button>
27-
2822
<button
2923
type="button"
3024
class="rounded-md p-2 text-white ring ring-neutral-600
@@ -33,4 +27,8 @@
3327
>
3428
Toggle Switch
3529
</button>
30+
31+
<p>Loader last updated: {data.time.toLocaleTimeString()}.</p>
32+
<p>Query value from loader: {data.query}</p>
33+
<p>Switch value from loader: {data.switch}</p>
3634
</div>

0 commit comments

Comments
 (0)