Skip to content

Conversation

@oskarrough
Copy link

On initial load when a query collection tries to start up, AND there's local data to hydrate from the queryPersister, it would throw https://svelte.dev/docs/svelte/runtime-errors#Client-errors-state_unsafe_mutation.

🎯 Changes

I don't particularly like adding await tick(), but when added here to the subscribeChanges() inside useLiveQuery() it does seem to patch the issue.

Claude said:

When subscribeChanges is called with includeInitialState: true, it fires the callback synchronously with the initial data - right there during the $effect setup. At that moment, Svelte might still be in the middle of a render cycle (evaluating $derived, rendering templates, etc.). Updating $state during render is forbidden - hence state_unsafe_mutation.
Without tick():
$effect runs → subscribeChanges → callback fires immediately → state mutation → ERROR (still rendering)
With tick():
$effect runs → subscribeChanges → callback fires → await tick() → [render completes] → state mutation → OK

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

On initial load when a query collection tries to start up, and there's local data to hydrate from the queryPersister, it would throw https://svelte.dev/docs/svelte/runtime-errors#Client-errors-state_unsafe_mutation.

I don't particularly like adding `await tick()`, but when added here to `useLiveQuery()` it does seem to solve the issue here.

Claude said


> When subscribeChanges is called with includeInitialState: true, it fires the callback synchronously with the initial data - right there during the $effect setup.

> At that moment, Svelte might still be in the middle of a render cycle (evaluating $derived, rendering templates, etc.). Updating $state during render is forbidden - hence
  state_unsafe_mutation.

> Without tick():
  $effect runs → subscribeChanges → callback fires immediately → state mutation → ERROR (still rendering)

> With tick():
  $effect runs → subscribeChanges → callback fires → await tick() → [render completes] → state mutation → OK
@changeset-bot
Copy link

changeset-bot bot commented Dec 7, 2025

⚠️ No Changeset found

Latest commit: 884a561

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

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.

2 participants