You're viewing the Inertia.js v2.0 pre-release documentation. Upgrade guide →

Prefetching

Inertia supports prefetching data for pages that are likely to be visited next. This can be useful for improving the perceived performance of your app by allowing the data to be fetched in the background while the user is still interacting with the current page.

To prefetch data for a page, you can use the prefetch method on the Inertia link component. By default, Inertia will prefetch the data for the page when the user hovers over the link after more than 75ms.

import { Link } from '@inertiajs/vue3'

<Link href="/users" prefetch>Users</Link>

By default, data is cached for 30 seconds before being evicted. You can customize this behavior by passing a cacheFor prop to the Link component.

import { Link } from '@inertiajs/vue3'

<Link href="/users" prefetch cache-for="1m">Users</Link>
<Link href="/users" prefetch cache-for="10s">Users</Link>
<Link href="/users" prefetch :cache-for="5000">Users</Link>

You can also start prefetching on mousedown by passing the click value to the prefetch prop.

import { Link } from '@inertiajs/vue3'

<Link href="/users" prefetch="click">Users</Link>

If you're confident that the user will visit a page next, you can prefetch the data on mount as well.

import { Link } from '@inertiajs/vue3'

<Link href="/users" prefetch="mount">Users</Link>

You can also combine strategies by passing an array of values to the prefetch prop.

import { Link } from '@inertiajs/vue3'

<Link href="/users" :prefetch="['mount', 'hover']">Users</Link>

Programmatic prefetching

You can also prefetch data programmatically using router.prefetch. The signature is identical to router.visit with the exception of a third argument that allows you to specify prefetch options.

When the cacheFor option is not specified, it defaults to 30 seconds.

router.prefetch(
    '/users',
    { method: 'get', data: { page: 2 } },
)

router.prefetch(
    '/users',
    { method: 'get', data: { page: 2 } },
    { cacheFor: '1m' },
)

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.

import { usePrefetch } from '@inertiajs/vue3'

const { lastUpdatedAt, isPrefetching, isPrefetched } = usePrefetch(
    '/users',
    { method: 'get', data: { page: 2 } },
    { cacheFor: '1m' },
)

Flushing prefetch cache

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

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

Furthermore, if you are using the prefetch helper, it will return a flush method for you to use for that specific page.

// Flush all prefetch cache
router.flushAll()

// Flush cache for a specific page
router.flush(
    '/users',
    { method: 'get', data: { page: 2 } },
)

const { flush } = usePrefetch(
    '/users',
    { method: 'get', data: { page: 2 } },
)

// Flush cache for a specific page
flush()

Stale while revalidate

By default, Inertia will fetch a fresh copy of the data when the user visits the page if the cached data is older than the cache duration. You can customize this behavior by passing a tuple to the cacheFor prop.

The first value in the array represents the number of seconds the cache is considered fresh, while the second value defines how long it can be served as stale data before fetching data from the server is necessary.

import { Link } from '@inertiajs/vue3'

<Link href="/users" prefetch :cacheFor="['30s', '1m']">Users</Link>

How it works

If a request is made within the fresh period (before the first value), the cache is returned immediately without making a request to the server.

If a request is made during the stale period (between the two values), the stale value is served to the user, and a request is made in the background to refresh the cached value. Once the value is returned, the data is merged into the page so the user has the most recent data.

If a request is made after the second value, the cache is considered expired, and the value is fetched from the sever as a regular request.