Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/router/eslint/create-route-property-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/path')({
loader: async ({ context }) => {
await context.queryClient.ensureQueryData(getQueryOptions(context.hello))
await context.queryClient.query({ ...getQueryOptions(context.hello), staleTime: 'static' })
},
beforeLoad: () => ({ hello: 'world' }),
})
Expand All @@ -55,7 +55,7 @@ import { createFileRoute } from '@tanstack/solid-router'

export const Route = createFileRoute('/path')({
loader: async ({ context }) => {
await context.queryClient.ensureQueryData(getQueryOptions(context.hello))
await context.queryClient.query({ ...getQueryOptions(context.hello), staleTime: 'static' })
},
beforeLoad: () => ({ hello: 'world' }),
})
Expand All @@ -80,7 +80,7 @@ import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/path')({
beforeLoad: () => ({ hello: 'world' }),
loader: async ({ context }) => {
await context.queryClient.ensureQueryData(getQueryOptions(context.hello))
await context.queryClient.query({ ...getQueryOptions(context.hello), staleTime: 'static' })
},
})
```
Expand All @@ -98,7 +98,7 @@ import { createFileRoute } from '@tanstack/solid-router'
export const Route = createFileRoute('/path')({
beforeLoad: () => ({ hello: 'world' }),
loader: async ({ context }) => {
await context.queryClient.ensureQueryData(getQueryOptions(context.hello))
await context.queryClient.query({ ...getQueryOptions(context.hello), staleTime: 'static' })
},
})
```
Expand Down
8 changes: 4 additions & 4 deletions docs/router/guide/deferred-data-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ import { slowDataOptions, fastDataOptions } from '~/api/query-options'
export const Route = createFileRoute('/posts/$postId')({
loader: async ({ context: { queryClient } }) => {
// Kick off the fetching of some slower data, but do not await it
queryClient.prefetchQuery(slowDataOptions())
void queryClient.query(slowDataOptions()).catch(noop)

// Fetch and await some data that resolves quickly
await queryClient.ensureQueryData(fastDataOptions())
await queryClient.query({...fastDataOptions(), staleTime: 'static')
},
})
```
Expand All @@ -113,10 +113,10 @@ import { slowDataOptions, fastDataOptions } from '~/api/query-options'
export const Route = createFileRoute('/posts/$postId')({
loader: async ({ context: { queryClient } }) => {
// Kick off the fetching of some slower data, but do not await it
queryClient.prefetchQuery(slowDataOptions())
void queryClient.query(slowDataOptions()).catch(noop)

// Fetch and await some data that resolves quickly
await queryClient.ensureQueryData(fastDataOptions())
await queryClient.query({...fastDataOptions(), staleTime: 'static')
},
})
```
Expand Down
4 changes: 2 additions & 2 deletions docs/router/guide/external-data-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const postsQueryOptions = queryOptions({

export const Route = createFileRoute('/posts')({
// Use the `loader` option to ensure that the data is loaded
loader: () => queryClient.ensureQueryData(postsQueryOptions),
loader: () => queryClient.query({...postsQueryOptions, staleTime: 'static'),
component: () => {
// Read the data from the cache and subscribe to updates
const {
Expand All @@ -106,7 +106,7 @@ When an error occurs while using `suspense` with `TanStack Query`, you need to l

```tsx
export const Route = createFileRoute('/')({
loader: () => queryClient.ensureQueryData(postsQueryOptions),
loader: () => queryClient.query({...postsQueryOptions, staleTime: 'static' }),
errorComponent: ({ error, reset }) => {
const router = useRouter()
const queryErrorResetBoundary = useQueryErrorResetBoundary()
Expand Down
3 changes: 2 additions & 1 deletion docs/router/guide/router-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,10 @@ Then, in your route:
export const Route = createFileRoute('/todos')({
component: Todos,
loader: async ({ context }) => {
await context.queryClient.ensureQueryData({
await context.queryClient.query({
queryKey: ['todos', { userId: user.id }],
queryFn: fetchTodos,
staleTime: 'static'
})
},
})
Expand Down
6 changes: 3 additions & 3 deletions docs/router/guide/type-safety.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ As your application scales, TypeScript check times will naturally increase. Ther

### Only infer types you need

A great pattern with client side data caches (TanStack Query, etc.) is to prefetch data. For example with TanStack Query you might have a route which calls `queryClient.ensureQueryData` in a `loader`.
A great pattern with client side data caches (TanStack Query, etc.) is to prefetch data. For example with TanStack Query you might have a route which calls `queryClient.query` in a `loader`.

```tsx
export const Route = createFileRoute('/posts/$postId/deep')({
loader: ({ context: { queryClient }, params: { postId } }) =>
queryClient.ensureQueryData(postQueryOptions(postId)),
queryClient.query({ ...postQueryOptions(postId), staleTime: 'static' }),
component: PostDeepComponent,
})

Expand All @@ -159,7 +159,7 @@ This may look fine and for small route trees and you may not notice any TS perfo
```tsx
export const Route = createFileRoute('/posts/$postId/deep')({
loader: async ({ context: { queryClient }, params: { postId } }) => {
await queryClient.ensureQueryData(postQueryOptions(postId))
await queryClient.query({ ...postQueryOptions(postId), staleTime: 'static' })
},
component: PostDeepComponent,
})
Expand Down
3 changes: 2 additions & 1 deletion docs/router/how-to/setup-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,10 @@ describe('React Query Integration', () => {
path: '/posts',
component: PostsList,
loader: ({ context: { queryClient } }) =>
queryClient.ensureQueryData({
queryClient.query({
queryKey: ['posts'],
queryFn: mockFetchPosts,
staleTime: 'static'
}),
})

Expand Down
4 changes: 2 additions & 2 deletions docs/router/integrations/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const postsQuery = queryOptions({

export const Route = createFileRoute('/posts')({
// Ensure the data is in the cache before render
loader: ({ context }) => context.queryClient.ensureQueryData(postsQuery),
loader: ({ context }) => context.queryClient.query({...postsQuery, staleTime: 'static' }),
component: PostsPage,
})

Expand All @@ -133,7 +133,7 @@ function PostsPage() {

### Prefetching and streaming

You can also prefetch with `fetchQuery` or `ensureQueryData` in a loader without consuming the data in a component. If you return the promise directly from the loader, it will be awaited and thus block the SSR request until the query finishes. If you don't await the promise nor return it, the query will be started on the server and will be streamed to the client without blocking the SSR request.
You can also prefetch with `query` in a loader without consuming the data in a component. If you return the promise directly from the loader, it will be awaited and thus block the SSR request until the query finishes. If you don't await the promise nor return it, the query will be started on the server and will be streamed to the client without blocking the SSR request.

<!-- ::start:framework -->

Expand Down
2 changes: 1 addition & 1 deletion docs/start/framework/react/comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const postQueryOptions = (postId: string) =>

export const Route = createFileRoute('/posts/$postId')({
loader: ({ context, params }) =>
context.queryClient.ensureQueryData(postQueryOptions(params.postId)),
context.queryClient.query({ ...postQueryOptions(params.postId), staleTime: `static` }),
})

function Post() {
Expand Down
Loading