Skip to content

Commit 6de5d2e

Browse files
committed
solid prefetching update
1 parent e6dbeb0 commit 6de5d2e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

docs/framework/solid/guides/prefetching.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ For now, let's focus on the client side case and look at an example of how you c
218218

219219
When integrating at the router level, you can choose to either _block_ rendering of that route until all data is present, or you can start a prefetch but not await the result. That way, you can start rendering the route as soon as possible. You can also mix these two approaches and await some critical data, but start rendering before all the secondary data has finished loading. In this example, we'll configure an `/article` route to not render until the article data has finished loading, as well as start prefetching comments as soon as possible, but not block rendering the route if comments haven't finished loading yet.
220220

221+
Note that many route loaders use error boundaries to trigger error fallbacks. Whereas up to now we have been using `.catch(noop)` to ignore errors for data that will be retried by `useQuery`, for critical data that the route will not work without, you should `await` the promise without `noop` and handle the error in a `try` block or the router's error handling (such as TanStack Router's `errorComponent`).
222+
221223
```tsx
222224
const queryClient = new QueryClient()
223225
const routerContext = new RouterContext()
@@ -238,11 +240,19 @@ const articleRoute = new Route({
238240
context: { queryClient },
239241
routeContext: { articleQueryOptions, commentsQueryOptions },
240242
}) => {
241-
// Fetch comments asap, but don't block
243+
// Fetch comments asap, but don't block or throw errors
242244
void queryClient.query(commentsQueryOptions).catch(noop)
243245

244246
// Don't render the route at all until article has been fetched
245-
await queryClient.query(articleQueryOptions).catch(noop)
247+
// As this is critical data we want the error component to trigger
248+
// as soon as possible if something goes wrong
249+
await queryClient.query({
250+
...articleQueryOptions,
251+
// If we have the article loaded already, we don't want to block on
252+
// an extra prefetch; fallback on the default useQuery behavior to
253+
// keep the data fresh
254+
staleTime: 'static'
255+
})
246256
},
247257
component: ({ useRouteContext }) => {
248258
const { articleQueryOptions, commentsQueryOptions } = useRouteContext()
@@ -257,6 +267,7 @@ const articleRoute = new Route({
257267
})
258268
```
259269

270+
260271
Integration with other routers is also possible.
261272

262273
[//]: # 'Router'

0 commit comments

Comments
 (0)