Skip to content

Commit 41d4c5b

Browse files
committed
Update docs about the usePrefetch() hook
see inertiajs/inertiajs.com#443
1 parent 5b02679 commit 41d4c5b

File tree

1 file changed

+201
-9
lines changed

1 file changed

+201
-9
lines changed

docs/guide/prefetching.md

Lines changed: 201 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ You can also pass visit options when you need to differentiate between different
266266
import { usePrefetch } from '@inertiajs/vue3'
267267

268268
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
269-
headers: { 'X-Custom-Header': 'value' }
269+
headers: { 'X-Custom-Header': 'value' },
270270
})
271271
```
272272

@@ -276,7 +276,7 @@ const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
276276
import { usePrefetch } from '@inertiajs/react'
277277

278278
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
279-
headers: { 'X-Custom-Header': 'value' }
279+
headers: { 'X-Custom-Header': 'value' },
280280
})
281281
```
282282

@@ -286,20 +286,71 @@ const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
286286
import { usePrefetch } from '@inertiajs/svelte'
287287

288288
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
289-
headers: { 'X-Custom-Header': 'value' }
289+
headers: { 'X-Custom-Header': 'value' },
290290
})
291291
```
292292

293293
:::
294294

295+
## Cache tags
295296

296-
## Flushing prefetch cache
297+
@available_since core=2.1.2
297298

298-
You can flush the prefetch cache by calling `router.flushAll`. This will remove all cached data for all pages.
299+
Cache tags allow you to group related prefetched data and invalidate it all at once when specific events occur.
299300

300-
If you want to flush the cache for a specific page, you can pass the page URL and options to the `router.flush` method.
301+
To tag cached data, pass a `cacheTags` prop to your `Link` component.
301302

302-
Furthermore, if you are using the `usePrefetch` hook, it will return a `flush` method for you to use.
303+
:::tabs key:frameworks
304+
305+
== Vue
306+
307+
```vue
308+
<script setup>
309+
import { Link } from '@inertiajs/vue3'
310+
</script>
311+
312+
<template>
313+
<Link href="/users" prefetch cache-tags="users">Users</Link>
314+
<Link href="/dashboard" prefetch :cache-tags="['dashboard', 'stats']">
315+
Dashboard
316+
</Link>
317+
</template>
318+
```
319+
320+
== React
321+
322+
```jsx
323+
import { Link } from '@inertiajs/react'
324+
325+
<Link href="/users" prefetch cacheTags="users">Users</Link>
326+
<Link href="/dashboard" prefetch cacheTags={['dashboard', 'stats']}>Dashboard</Link>
327+
```
328+
329+
== Svelte 4 | Svelte 5
330+
331+
```svelte
332+
import {inertia} from '@inertiajs/svelte'
333+
334+
<a href="/users" use:inertia={{ prefetch: true, cacheTags: 'users' }}>Users</a>
335+
<a
336+
href="/dashboard"
337+
use:inertia={{ prefetch: true, cacheTags: ['dashboard', 'stats'] }}
338+
>Dashboard</a
339+
>
340+
```
341+
342+
:::
343+
344+
When prefetching programmatically, pass `cacheTags` in the third argument to `router.prefetch`.
345+
346+
```js
347+
router.prefetch('/users', {}, { cacheTags: 'users' })
348+
router.prefetch('/dashboard', {}, { cacheTags: ['dashboard', 'stats'] })
349+
```
350+
351+
## Cache invalidation
352+
353+
You can manually flush the prefetch cache by calling `router.flushAll` to remove all cached data, or `router.flush` to remove cache for a specific page.
303354

304355
```js
305356
// Flush all prefetch cache
@@ -308,10 +359,151 @@ router.flushAll()
308359
// Flush cache for a specific page
309360
router.flush('/users', { method: 'get', data: { page: 2 } })
310361

362+
// Using the usePrefetch hook
311363
const { flush } = usePrefetch()
312364

313-
// Flush cache for the current page
314-
flush()
365+
flush() // Flush cache for the current page
366+
```
367+
368+
For more granular control, you can flush cached data by their tags using `router.flushByCacheTags`. This removes any cached response that contains _any_ of the specified tags.
369+
370+
```js
371+
// Flush all responses tagged with 'users'
372+
router.flushByCacheTags('users')
373+
374+
// Flush all responses tagged with 'dashboard' OR 'stats'
375+
router.flushByCacheTags(['dashboard', 'stats'])
376+
```
377+
378+
### Invalidate on requests
379+
380+
@available_since core=2.1.2
381+
382+
To automatically invalidate caches when making requests, pass an `invalidateCacheTags` prop to the Form component. The specified tags will be flushed when the form submission succeeds.
383+
384+
:::tabs key:frameworks
385+
386+
== Vue
387+
388+
```vue
389+
<script setup>
390+
import { Form } from '@inertiajs/vue3'
391+
</script>
392+
393+
<template>
394+
<Form
395+
action="/users"
396+
method="post"
397+
:invalidate-cache-tags="['users', 'dashboard']"
398+
>
399+
<input type="text" name="name" />
400+
<input type="email" name="email" />
401+
<button type="submit">Create User</button>
402+
</Form>
403+
</template>
404+
```
405+
406+
== React
407+
408+
```jsx
409+
import { Form } from '@inertiajs/react'
410+
411+
export default () => (
412+
<Form
413+
action="/users"
414+
method="post"
415+
invalidateCacheTags={['users', 'dashboard']}
416+
>
417+
<input type="text" name="name" />
418+
<input type="email" name="email" />
419+
<button type="submit">Create User</button>
420+
</Form>
421+
)
422+
```
423+
424+
== Svelte 4 | Svelte 5
425+
426+
```svelte
427+
<script>
428+
import { Form } from '@inertiajs/svelte'
429+
</script>
430+
431+
<Form
432+
action="/users"
433+
method="post"
434+
invalidateCacheTags={['users', 'dashboard']}
435+
>
436+
<input type="text" name="name" />
437+
<input type="email" name="email" />
438+
<button type="submit">Create User</button>
439+
</Form>
440+
```
441+
442+
:::
443+
444+
With the `useForm` helper, you can include `invalidateCacheTags` in the visit options.
445+
446+
:::tabs key:frameworks
447+
448+
== Vue
449+
450+
```vue
451+
import { useForm } from '@inertiajs/vue3' const form = useForm({ name: '',
452+
email: '', }) const submit = () => { form.post('/users', { invalidateCacheTags:
453+
['users', 'dashboard'] }) }
454+
```
455+
456+
== React
457+
458+
```jsx
459+
import { useForm } from '@inertiajs/react'
460+
461+
const { data, setData, post } = useForm({
462+
name: '',
463+
email: '',
464+
})
465+
466+
function submit(e) {
467+
e.preventDefault()
468+
post('/users', {
469+
invalidateCacheTags: ['users', 'dashboard'],
470+
})
471+
}
472+
```
473+
474+
== Svelte 4 | Svelte 5
475+
476+
```svelte
477+
import { useForm } from '@inertiajs/svelte'
478+
479+
const form = useForm({
480+
name: '',
481+
email: '',
482+
})
483+
484+
function submit() {
485+
$form.post('/users', {
486+
invalidateCacheTags: ['users', 'dashboard']
487+
})
488+
}
489+
```
490+
491+
:::
492+
493+
You can also invalidate cache tags with programmatic visits by including `invalidateCacheTags` in the options.
494+
495+
```js
496+
router.delete(
497+
`/users/${userId}`,
498+
{},
499+
{
500+
invalidateCacheTags: ['users', 'dashboard'],
501+
},
502+
)
503+
504+
router.post('/posts', postData, {
505+
invalidateCacheTags: ['posts', 'recent-posts'],
506+
})
315507
```
316508

317509
## Stale while revalidate

0 commit comments

Comments
 (0)