Skip to content

fix(useSearchParams): avoid state_unsafe_mutation on first read inside async $derived - #435

Open
filiabel wants to merge 1 commit into
svecosystem:mainfrom
filiabel:fix/use-search-params-async-derived
Open

filiabel wants to merge 1 commit into
svecosystem:mainfrom
filiabel:fix/use-search-params-async-derived

Conversation

@filiabel

@filiabel filiabel commented Nov 9, 2026

Copy link
Copy Markdown

With experimental.async, reading useSearchParams for the first time inside an async $derived throws state_unsafe_mutation when the URL has search params:

<script>
	const params = useSearchParams(schema);
	const data = $derived(await load(params.filter));
</script>

#initializeCacheFromURL fills a new SvelteURLSearchParams with .set(), which writes state. Svelte allows writes to state created inside a plain $derived, but not inside an async one. #361 only untracked the assignment.

The fix fills a plain URLSearchParams and wraps it afterwards. Behavior is unchanged (with duplicate keys, the last value still wins).

Verified with Playwright in a fresh SvelteKit 3 app with async enabled: direct load and client-side navigation with params both threw before and work after. Existing useSearchParams tests pass. Happy to add an async test app (like kit's test/apps/async) if you want coverage for this.

Related: #360, #361

@changeset-bot

changeset-bot Bot commented Nov 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d991ce5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
runed Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant