Skip to content

Commit b72b806

Browse files
authored
Merge pull request #443 from inertiajs/useprefetch-docs
Update docs about the `usePrefetch()` hook
2 parents 213d9d8 + 40d9d77 commit b72b806

File tree

1 file changed

+49
-24
lines changed

1 file changed

+49
-24
lines changed

resources/js/Pages/prefetching.jsx

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ export default function () {
229229
/>
230230

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

236237
<TabbedCode
@@ -241,11 +242,7 @@ export default function () {
241242
code: dedent`
242243
import { usePrefetch } from '@inertiajs/vue3'
243244
244-
const { lastUpdatedAt, isPrefetching, isPrefetched } = usePrefetch(
245-
'/users',
246-
{ method: 'get', data: { page: 2 } },
247-
{ cacheFor: '1m' },
248-
)
245+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch()
249246
`,
250247
},
251248
{
@@ -254,11 +251,7 @@ export default function () {
254251
code: dedent`
255252
import { usePrefetch } from '@inertiajs/react'
256253
257-
const { lastUpdatedAt, isPrefetching, isPrefetched } = usePrefetch(
258-
'/users',
259-
{ method: 'get', data: { page: 2 } },
260-
{ cacheFor: '1m' },
261-
)
254+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch()
262255
`,
263256
},
264257
{
@@ -267,11 +260,47 @@ export default function () {
267260
code: dedent`
268261
import { usePrefetch } from '@inertiajs/svelte'
269262
270-
const { lastUpdatedAt, isPrefetching, isPrefetched } = usePrefetch(
271-
'/users',
272-
{ method: 'get', data: { page: 2 } },
273-
{ cacheFor: '1m' },
274-
)
263+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch()
264+
`,
265+
},
266+
]}
267+
/>
268+
<P>
269+
You can also pass visit options when you need to differentiate between different request configurations for the same URL.
270+
</P>
271+
<TabbedCode
272+
examples={[
273+
{
274+
name: 'Vue',
275+
language: 'js',
276+
code: dedent`
277+
import { usePrefetch } from '@inertiajs/vue3'
278+
279+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
280+
headers: { 'X-Custom-Header': 'value' }
281+
})
282+
`,
283+
},
284+
{
285+
name: 'React',
286+
language: 'js',
287+
code: dedent`
288+
import { usePrefetch } from '@inertiajs/react'
289+
290+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
291+
headers: { 'X-Custom-Header': 'value' }
292+
})
293+
`,
294+
},
295+
{
296+
name: 'Svelte',
297+
language: 'js',
298+
code: dedent`
299+
import { usePrefetch } from '@inertiajs/svelte'
300+
301+
const { lastUpdatedAt, isPrefetching, isPrefetched, flush } = usePrefetch({
302+
headers: { 'X-Custom-Header': 'value' }
303+
})
275304
`,
276305
},
277306
]}
@@ -286,8 +315,7 @@ export default function () {
286315
<Code>router.flush</Code> method.
287316
</P>
288317
<P>
289-
Furthermore, if you are using the prefetch helper, it will return a <Code>flush</Code> method for you to use for
290-
that specific page.
318+
Furthermore, if you are using the <Code>usePrefetch</Code> hook, it will return a <Code>flush</Code> method for you to use.
291319
</P>
292320
<TabbedCode
293321
examples={[
@@ -304,12 +332,9 @@ export default function () {
304332
{ method: 'get', data: { page: 2 } },
305333
)
306334
307-
const { flush } = usePrefetch(
308-
'/users',
309-
{ method: 'get', data: { page: 2 } },
310-
)
335+
const { flush } = usePrefetch()
311336
312-
// Flush cache for a specific page
337+
// Flush cache for the current page
313338
flush()
314339
`,
315340
},

0 commit comments

Comments
 (0)