-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat!(svelte-query): rewrite Svelte adapter to use runes #9694
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
Merged
+4,860
−2,018
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ee9346a
WIP: Svelte 5 adapter (#6981)
zhihengGet b5f5fd4
feat(svelte-query): Improve svelte runes API (#8852)
elliott-with-the-longest-name-on-github 594e0bf
Merge branch 'main' into svelte-5-adapter
lachlancollins e3f7484
Use sleep from query-test-utils
lachlancollins 859d9b7
Simplify test reset logic
lachlancollins 746b9c5
Merge branch 'main' into svelte-5-adapter
lachlancollins 6e65a4a
Merge branch 'main' into svelte-5-adapter
lachlancollins 540ce06
Fix some merge conflicts
lachlancollins f0a9ebe
More fixes
lachlancollins cc5a571
A few more fixes
lachlancollins d484552
Merge branch 'main' into svelte-5-adapter
lachlancollins 686db47
Fix useMutationState
lachlancollins e3bba4a
Add changeset
lachlancollins 373e24b
Add migration docs
lachlancollins d0beb44
Merge branch 'main' into svelte-5-adapter
lachlancollins bd8737f
Replace Set with SvelteSet
lachlancollins e07e895
Update minimum svelte version
lachlancollins 9f5c7e8
Bump svelte-eslint-parser
lachlancollins eea078e
Unwrap createQuery test
lachlancollins 968d6a8
fix(svelte-query): `state_unsafe_mutation` error with `useIs...` (#9493)
hmnd 62be7d5
chore(svelte-query): fix eslint config (#9699)
lachlancollins c1e67ed
Merge branch 'main' into svelte-5-adapter
lachlancollins c6d0b92
Merge branch 'main' into svelte-5-adapter
lachlancollins 05f0acd
ci: apply automated fixes
autofix-ci[bot] 7049e4b
Fix sherif
lachlancollins 0c44073
Update docs and changeset
lachlancollins f3fc6a2
Update keywords
lachlancollins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@tanstack/svelte-query-persist-client': major | ||
'@tanstack/svelte-query-devtools': major | ||
'@tanstack/svelte-query': major | ||
--- | ||
|
||
BREAKING: Migrate to svelte runes (signals). Requires [Svelte v5.25.0](https://github.com/sveltejs/svelte/releases/tag/svelte%405.25.0) or newer. Please see the [migration guide](https://tanstack.com/query/latest/docs/framework/svelte/migrate-from-v5-to-v6). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
## Overview | ||
|
||
While Svelte v5 has legacy compatibility with the stores syntax from Svelte v3/v4, it has been somewhat buggy and unreliable for this adapter. The `@tanstack/svelte-query` v6 adapter fully migrates to the runes syntax, which relies on signals. This rewrite should also simplify the code required to ensure your query inputs remain reactive. | ||
|
||
## Installation | ||
|
||
Please ensure your project has [Svelte v5.25.0](https://github.com/sveltejs/svelte/releases/tag/svelte%405.25.0) or newer. | ||
|
||
Run `pnpm add @tanstack/svelte-query@latest` (or your package manager's equivalent). | ||
|
||
> Note that `@tanstack/svelte-query` v6 depends on `@tanstack/query-core` v5. | ||
|
||
## Thunks | ||
|
||
Like the Solid adapter, most functions for the Svelte adapter now require options to be provided as a "thunk" (`() => options`) to provide reactivity. | ||
|
||
```diff | ||
-const query = createQuery({ | ||
+const query = createQuery(() => ({ | ||
queryKey: ['todos'], | ||
queryFn: () => fetchTodos(), | ||
-}) | ||
+})) | ||
``` | ||
|
||
## Accessing Properties | ||
|
||
Given the adapter no longer uses stores, it is no longer necessary to prefix with `$`. | ||
|
||
```diff | ||
-{#if $todos.isSuccess} | ||
+{#if todos.isSuccess} | ||
<ul> | ||
- {#each $todos.data.items as item} | ||
+ {#each todos.data.items as item} | ||
<li>{item}</li> | ||
{/each} | ||
</ul> | ||
{/if} | ||
``` | ||
|
||
## Reactivity | ||
|
||
You previously needed to do some funky things with stores to achieve reactivity for inputs. This is no longer the case! You don't even need to wrap your query in a `$derived`. | ||
|
||
```diff | ||
-const intervalMs = writable(1000) | ||
+let intervalMs = $state(1000) | ||
|
||
-const query = createQuery(derived(intervalMs, ($intervalMs) => ({ | ||
+const query = createQuery(() => ({ | ||
queryKey: ['refetch'], | ||
queryFn: async () => await fetch('/api/data').then((r) => r.json()), | ||
refetchInterval: $intervalMs, | ||
-}))) | ||
+})) | ||
``` | ||
|
||
## Disabling Legacy Mode | ||
|
||
If your component has any stores, it might not properly switch to runes mode. You can ensure your application is using runes in two ways: | ||
|
||
### On a per-file basis | ||
|
||
In each `.svelte` file, once you have migrated to runes, add `<svelte:options runes={true} />`. This is better for large applications requiring gradual migration. | ||
|
||
### On an project-wide basis | ||
|
||
In your `svelte.config.js`, add the following to config: | ||
|
||
```json | ||
compilerOptions: { | ||
runes: true, | ||
}, | ||
``` | ||
|
||
This can be added once you've 100% eradicated stores syntax from your app. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ const config = { | |
kit: { | ||
adapter: adapter(), | ||
}, | ||
compilerOptions: { | ||
runes: true, | ||
}, | ||
} | ||
|
||
export default config |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
🧩 Analysis chain
Confirm Svelte minimum version and align peerDeps; consider adding a brief TL;DR migration snippet.
Example tweak (optional):
Run this script to check peerDependency alignment across the repo:
🏁 Script executed:
Length of output: 11450
🏁 Script executed:
Length of output: 381
PeerDeps aligned and release tag valid; migration guide link broken
packages/svelte-query*
declarepeerDependencies.svelte
as^5.25.0
.https://tanstack.com/query/latest/docs/framework/svelte/migrate-from-v5-to-v6
link to its correct path.🤖 Prompt for AI Agents