Skip to content
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

docs(svelte-query): Use API wrapper to simplify SSR logic #5322

Merged
merged 4 commits into from Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/svelte/ssr/src/lib/Post.svelte
@@ -1,13 +1,13 @@
<script lang="ts">
import { createQuery } from '@tanstack/svelte-query'
import { getPostById } from './data'
import { api } from './api'
import type { Post } from './types'

export let postId: number

const post = createQuery<Post, Error>({
queryKey: ['post', postId],
queryFn: () => getPostById(postId),
queryFn: () => api().getPostById(postId),
})
</script>

Expand Down
4 changes: 2 additions & 2 deletions examples/svelte/ssr/src/lib/Posts.svelte
@@ -1,6 +1,6 @@
<script lang="ts">
import { useQueryClient, createQuery } from '@tanstack/svelte-query'
import { getPosts } from './data'
import { api } from './api'

const client = useQueryClient()

Expand All @@ -11,7 +11,7 @@
Error
>({
queryKey: ['posts', limit],
queryFn: () => getPosts(limit),
queryFn: () => api().getPosts(limit),
})
</script>

Expand Down
18 changes: 18 additions & 0 deletions examples/svelte/ssr/src/lib/api.ts
@@ -0,0 +1,18 @@
import type { Post } from './types'

export const api = (customFetch = fetch) => ({
getPosts: async (limit: number) => {
const response = await customFetch(
'https://jsonplaceholder.typicode.com/posts',
)
const data = (await response.json()) as Post[]
return data.filter((x) => x.id <= limit)
},
getPostById: async (id: number): Promise<Post> => {
const response = await customFetch(
`https://jsonplaceholder.typicode.com/posts/${id}`,
)
const data = (await response.json()) as Post
return data
},
})
23 changes: 0 additions & 23 deletions examples/svelte/ssr/src/lib/data.ts

This file was deleted.

4 changes: 2 additions & 2 deletions examples/svelte/ssr/src/routes/+page.ts
@@ -1,11 +1,11 @@
import { getPosts } from '$lib/data'
import { api } from '$lib/api'
import type { PageLoad } from './$types'

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

await queryClient.prefetchQuery({
queryKey: ['posts', 10],
queryFn: () => getPosts(10, fetch),
queryFn: () => api(fetch).getPosts(10),
})
}
4 changes: 2 additions & 2 deletions examples/svelte/ssr/src/routes/[postId]/+page.ts
@@ -1,4 +1,4 @@
import { getPostById } from '$lib/data'
import { api } from '$lib/api'
import type { PageLoad } from './$types'

export const load: PageLoad = async ({ parent, fetch, params }) => {
Expand All @@ -8,7 +8,7 @@ export const load: PageLoad = async ({ parent, fetch, params }) => {

await queryClient.prefetchQuery({
queryKey: ['post', postId],
queryFn: () => getPostById(postId, fetch),
queryFn: () => api(fetch).getPostById(postId),
})

return { postId }
Expand Down