Skip to content

Commit b5f5fd4

Browse files
elliott-with-the-longest-name-on-githubautofix-ci[bot]lachlancollins
authored
feat(svelte-query): Improve svelte runes API (#8852)
* feat: Draft proposal * chore: Improve reactive containers * ci: apply automated fixes * oops * fix: Update API, add a bunch of tests * merge main * fix: use const * more tests * feat: More tests, back to thunks, fixed svelte-query-persist-client * feat: More tests and examples! * lockfile * fixes * Fix current CI errors * More small fixes/tweaks * Remove test.only * ci: apply automated fixes * Fix pnpm-lock, fix import order * update main docs * feat: More tests * ci: apply automated fixes * add back old tests * Cleanup * Fix persist client * Fix useMutationState --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Lachlan Collins <[email protected]>
1 parent ee9346a commit b5f5fd4

File tree

81 files changed

+3686
-1308
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+3686
-1308
lines changed

docs/config.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@
127127
{
128128
"label": "SSR & SvelteKit",
129129
"to": "framework/svelte/ssr"
130-
},
131-
{
132-
"label": "Reactivity",
133-
"to": "framework/svelte/reactivity"
134130
}
135131
]
136132
},

docs/framework/svelte/devtools.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Place the following code as high in your Svelte app as you can. The closer it is
5555

5656
### Options
5757

58-
- `initialIsOpen: Boolean`
58+
- `initialIsOpen: boolean`
5959
- Set this `true` if you want the dev tools to default to being open
6060
- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
6161
- Defaults to `bottom-right`

docs/framework/svelte/installation.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ title: Installation
55

66
You can install Svelte Query via [NPM](https://npmjs.com).
77

8-
> v5 is currently available as a release-candidate. We don't anticipate any major API changes from here on out. We encourage you to try it out and report any issues you find.
9-
108
### NPM
119

1210
```bash

docs/framework/svelte/overview.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ Then call any function (e.g. createQuery) from any component:
2828
<script lang="ts">
2929
import { createQuery } from '@tanstack/svelte-query'
3030
31-
const query = createQuery({
31+
const query = createQuery(() => ({
3232
queryKey: ['todos'],
3333
queryFn: () => fetchTodos(),
34-
})
34+
}))
3535
</script>
3636
3737
<div>
38-
{#if $query.isLoading}
38+
{#if query.isLoading}
3939
<p>Loading...</p>
40-
{:else if $query.isError}
41-
<p>Error: {$query.error.message}</p>
42-
{:else if $query.isSuccess}
43-
{#each $query.data as todo}
40+
{:else if query.isError}
41+
<p>Error: {query.error.message}</p>
42+
{:else if query.isSuccess}
43+
{#each query.data as todo}
4444
<p>{todo.title}</p>
4545
{/each}
4646
{/if}
@@ -62,6 +62,8 @@ Svelte Query offers useful functions and components that will make managing serv
6262
- `useQueryClient`
6363
- `useIsFetching`
6464
- `useIsMutating`
65+
- `useMutationState`
66+
- `useIsRestoring`
6567
- `useHydrate`
6668
- `<QueryClientProvider>`
6769
- `<HydrationBoundary>`
@@ -70,5 +72,4 @@ Svelte Query offers useful functions and components that will make managing serv
7072

7173
Svelte Query offers an API similar to React Query, but there are some key differences to be mindful of.
7274

73-
- Many of the functions in Svelte Query return a Svelte store. To access values on these stores reactively, you need to prefix the store with a `$`. You can learn more about Svelte stores [here](https://learn.svelte.dev/tutorial/writable-stores).
74-
- If your query or mutation depends on variables, you must use a store for the options. You can read more about this [here](../reactivity).
75+
- The arguments to the `create*` functions must be wrapped in a function to preserve reactivity.

docs/framework/svelte/reactivity.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

docs/framework/svelte/ssr.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ export async function load() {
5858
5959
export let data: PageData
6060
61-
const query = createQuery({
61+
const query = createQuery(() => ({
6262
queryKey: ['posts'],
6363
queryFn: getPosts,
6464
initialData: data.posts,
65-
})
65+
}))
6666
</script>
6767
```
6868

@@ -136,10 +136,10 @@ export async function load({ parent, fetch }) {
136136
import { createQuery } from '@tanstack/svelte-query'
137137
138138
// This data is cached by prefetchQuery in +page.ts so no fetch actually happens here
139-
const query = createQuery({
139+
const query = createQuery(() => ({
140140
queryKey: ['posts'],
141141
queryFn: async () => (await fetch('/api/posts')).json(),
142-
})
142+
}))
143143
</script>
144144
```
145145

examples/svelte/auto-refetching/src/routes/+page.svelte

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
const client = useQueryClient()
1212
13-
const endpoint = 'http://localhost:5173/api/data'
13+
const endpoint = '/api/data'
1414
1515
const todos = createQuery<{ items: string[] }>(() => ({
1616
queryKey: ['refetch'],
@@ -21,7 +21,9 @@
2121
2222
const addMutation = createMutation(() => ({
2323
mutationFn: (value: string) =>
24-
fetch(`${endpoint}?add=${value}`).then((r) => r.json()),
24+
fetch(`${endpoint}?add=${encodeURIComponent(value)}`).then((r) =>
25+
r.json(),
26+
),
2527
onSuccess: () => client.invalidateQueries({ queryKey: ['refetch'] }),
2628
}))
2729
@@ -31,7 +33,7 @@
3133
}))
3234
</script>
3335

34-
<h1>Auto Refetch with stale-time set to 1s</h1>
36+
<h1>Auto Refetch with stale-time set to {(intervalMs / 1000).toFixed(2)}s</h1>
3537

3638
<p>
3739
This example is best experienced on your own machine, where you can open
@@ -86,14 +88,22 @@
8688
<button onclick={() => clearMutation.mutate(undefined)}> Clear All </button>
8789
</div>
8890
{/if}
89-
{#if todos.isFetching}
90-
<div style="color:darkgreen; font-weight:700">
91-
'Background Updating...' : ' '
92-
</div>
93-
{/if}
91+
92+
<pre
93+
class={['updating-text', todos.isFetching && 'on']}
94+
style="font-weight:700">Background Updating...</pre>
9495

9596
<style>
9697
li {
9798
text-align: left;
9899
}
100+
101+
.updating-text {
102+
color: transparent;
103+
transition: all 0.3s ease;
104+
}
105+
.updating-text.on {
106+
color: green;
107+
transition: none;
108+
}
99109
</style>

examples/svelte/basic/src/lib/Posts.svelte

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838
</article>
3939
{/each}
4040
</ul>
41-
{#if posts.isFetching}
42-
<div style="color:darkgreen; font-weight:700">
43-
Background Updating...
44-
</div>
45-
{/if}
41+
<pre
42+
class={['updating-text', posts.isFetching && 'on']}
43+
style="font-weight:700">Background Updating...</pre>
4644
{/if}
4745
</div>
4846
</div>
@@ -53,8 +51,16 @@
5351
}
5452
a {
5553
display: block;
56-
color: white;
5754
font-size: 1.5rem;
5855
margin-bottom: 1rem;
5956
}
57+
58+
.updating-text {
59+
color: transparent;
60+
transition: all 0.3s ease;
61+
}
62+
.updating-text.on {
63+
color: green;
64+
transition: none;
65+
}
6066
</style>

examples/svelte/load-more-infinite-scroll/src/lib/LoadMore.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@
6060
.card {
6161
background-color: #111;
6262
margin-bottom: 1rem;
63+
color: rgba(255, 255, 255, 0.87);
6364
}
6465
</style>

examples/svelte/optimistic-updates/src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
const client = useQueryClient()
2222
23-
const endpoint = 'http://localhost:5173/api/data'
23+
const endpoint = '/api/data'
2424
2525
const fetchTodos = async (): Promise<Todos> =>
2626
await fetch(endpoint).then((r) => r.json())

examples/svelte/optimistic-updates/src/routes/api/data/+server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Todo = {
66
text: string
77
}
88

9-
const items: Todo[] = []
9+
const items: Array<Todo> = []
1010

1111
/** @type {import('./$types').RequestHandler} */
1212
export const GET: RequestHandler = async (req) => {

examples/svelte/playground/src/routes/AddTodo.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
let name = $state('')
1515
1616
const postTodo = async ({ name, notes }: Omit<Todo, 'id'>) => {
17-
console.info('postTodo', { name, notes })
1817
return new Promise((resolve, reject) => {
1918
setTimeout(
2019
() => {
@@ -31,7 +30,7 @@
3130
}
3231
const todo = { name, notes, id: id.value }
3332
id.value = id.value + 1
34-
list.value = [...list.value, todo]
33+
list.value.push(todo)
3534
resolve(todo)
3635
},
3736
queryTimeMin.value +

examples/svelte/playground/src/routes/App.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
<button
2828
onclick={() => {
29-
views.value = [...views.value, '']
29+
views.value.push('')
3030
}}
3131
>
3232
Add Filter List

examples/svelte/ssr/src/lib/Posts.svelte

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838
</article>
3939
{/each}
4040
</ul>
41-
{#if posts.isFetching}
42-
<div style="color:darkgreen; font-weight:700">
43-
Background Updating...
44-
</div>
45-
{/if}
41+
<pre
42+
class={['updating-text', posts.isFetching && 'on']}
43+
style="font-weight:700">Background Updating...</pre>
4644
{/if}
4745
</div>
4846
</div>
@@ -53,8 +51,15 @@
5351
}
5452
a {
5553
display: block;
56-
color: white;
5754
font-size: 1.5rem;
5855
margin-bottom: 1rem;
5956
}
57+
.updating-text {
58+
color: transparent;
59+
transition: all 0.3s ease;
60+
}
61+
.updating-text.on {
62+
color: green;
63+
transition: none;
64+
}
6065
</style>

examples/svelte/ssr/src/routes/+layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { browser } from '$app/environment'
21
import { QueryClient } from '@tanstack/svelte-query'
32
import type { LayoutLoad } from './$types'
3+
import { browser } from '$app/environment'
44

55
export const load: LayoutLoad = () => {
66
const queryClient = new QueryClient({

examples/svelte/ssr/src/routes/+page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { api } from '$lib/api'
21
import type { PageLoad } from './$types'
2+
import { api } from '$lib/api'
33

44
export const load: PageLoad = async ({ parent, fetch }) => {
55
const { queryClient } = await parent()

examples/svelte/ssr/src/routes/[postId]/+page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { api } from '$lib/api'
21
import type { PageLoad } from './$types'
2+
import { api } from '$lib/api'
33

44
export const load: PageLoad = async ({ parent, fetch, params }) => {
55
const { queryClient } = await parent()

examples/svelte/star-wars/src/routes/+page.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
<h2 class="text-4xl">React Query Demo</h2>
33
<p>Using the Star Wars API</p>
44
<p>
5-
(Built by <a href="https://twitter.com/Brent_m_Clark">@Brent_m_Clark</a>
6-
)
5+
(Built by <a href="https://twitter.com/Brent_m_Clark">@Brent_m_Clark</a>)
76
</p>
87
<section>
98
<h5 class="text-2xl">Why React Query?</h5>

examples/svelte/star-wars/src/routes/characters/[characterId]/+page.svelte

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
import { createQuery } from '@tanstack/svelte-query'
33
import Homeworld from './Homeworld.svelte'
44
import Film from './Film.svelte'
5-
6-
let { data } = $props()
5+
import { page } from '$app/state'
76
87
const getCharacter = async () => {
98
const res = await fetch(
10-
`https://swapi.dev/api/people/${data.params.characterId}/`,
9+
`https://swapi.dev/api/people/${page.params.characterId}/`,
1110
)
1211
return await res.json()
1312
}
1413
1514
const query = createQuery(() => ({
16-
queryKey: ['character', data.params.characterId],
15+
queryKey: ['character', page.params.characterId],
1716
queryFn: getCharacter,
1817
}))
1918
</script>

examples/svelte/star-wars/src/routes/characters/[characterId]/+page.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)