Skip to content

Commit d9947a2

Browse files
committed
Update docs about the usePrefetch() hook
see inertiajs/inertiajs.com#443
1 parent a0257ec commit d9947a2

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

docs/guide/prefetching.md

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,54 +227,79 @@ router.prefetch(
227227
)
228228
```
229229

230-
To make this even easier, Inertia offers a prefetch helper. This helper provides some additional insight into the request, such as the last updated timestamp and if the request is currently prefetching.
230+
Inertia also provides a `usePrefetch` hook that allows you to track the prefetch state for the current page. It returns information about whether the page is currently prefetching, has been prefetched, when it was last updated, and a `flush` method that flushes the cache for the current page only.
231231

232232
:::tabs key:frameworks
233233
== Vue
234234

235235
```js
236236
import { usePrefetch } from '@inertiajs/vue3'
237237

238-
const { lastUpdatedAt, isPrefetching, isPrefetched } = usePrefetch(
239-
'/users',
240-
{ method: 'get', data: { page: 2 } },
241-
{ cacheFor: '1m' },
242-
)
238+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch()
243239
```
244240

245241
== React
246242

247243
```js
248244
import { usePrefetch } from '@inertiajs/react'
249245

250-
const { lastUpdatedAt, isPrefetching, isPrefetched } = usePrefetch(
251-
'/users',
252-
{ method: 'get', data: { page: 2 } },
253-
{ cacheFor: '1m' },
254-
)
246+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch()
255247
```
256248

257249
== Svelte 4|Svelte 5
258250

259251
```js
260252
import { usePrefetch } from '@inertiajs/svelte'
261253

262-
const { lastUpdatedAt, isPrefetching, isPrefetched } = usePrefetch(
263-
'/users',
264-
{ method: 'get', data: { page: 2 } },
265-
{ cacheFor: '1m' },
266-
)
254+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch()
267255
```
268256

269257
:::
270258

259+
You can also pass visit options when you need to differentiate between different request configurations for the same URL.
260+
261+
:::tabs key:frameworks
262+
263+
== Vue
264+
265+
```js
266+
import { usePrefetch } from '@inertiajs/vue3'
267+
268+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
269+
headers: { 'X-Custom-Header': 'value' }
270+
})
271+
```
272+
273+
== React
274+
275+
```js
276+
import { usePrefetch } from '@inertiajs/react'
277+
278+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
279+
headers: { 'X-Custom-Header': 'value' }
280+
})
281+
```
282+
283+
== Svelte 4|Svelte 5
284+
285+
```js
286+
import { usePrefetch } from '@inertiajs/svelte'
287+
288+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
289+
headers: { 'X-Custom-Header': 'value' }
290+
})
291+
```
292+
293+
:::
294+
295+
271296
## Flushing prefetch cache
272297

273298
You can flush the prefetch cache by calling `router.flushAll`. This will remove all cached data for all pages.
274299

275300
If you want to flush the cache for a specific page, you can pass the page URL and options to the `router.flush` method.
276301

277-
Furthermore, if you are using the prefetch helper, it will return a `flush` method for you to use for that specific page.
302+
Furthermore, if you are using the `usePrefetch` hook, it will return a `flush` method for you to use.
278303

279304
```js
280305
// Flush all prefetch cache
@@ -283,8 +308,9 @@ router.flushAll()
283308
// Flush cache for a specific page
284309
router.flush('/users', { method: 'get', data: { page: 2 } })
285310

286-
// Flush cache for a specific page
287-
const { flush } = usePrefetch('/users', { method: 'get', data: { page: 2 } })
311+
const { flush } = usePrefetch()
312+
313+
// Flush cache for the current page
288314
flush()
289315
```
290316

0 commit comments

Comments
 (0)