Link Prefetching
To prefetch data for a page, you can add theprefetch prop to the Inertia link component. By default, Inertia will prefetch the data for the page when the user hovers over the link for more than 75ms. You may customize this hover delay by setting the prefetch.hoverDelay option in your application defaults.
prefetch.cacheFor option in your application defaults. You may also customize the cache duration on a per-link basis by passing a cacheFor prop to the Link component.
mousedown by passing the click value to the prefetch prop.
prefetch prop.
Programmatic Prefetching
You can prefetch data programmatically usingrouter.prefetch. This method’s 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.
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.
Cache Tags
Cache tags allow you to group related prefetched data and invalidate all cached data with that tag when specific events occur. To tag cached data, pass acacheTags prop to your Link component.
cacheTags in the third argument to router.prefetch.
Cache Invalidation
You can manually flush the prefetch cache by callingrouter.flushAll to remove all cached data, or router.flush to remove cache for a specific page.
router.flushByCacheTags. This removes any cached response that contains any of the specified tags.
Automatic Cache Flushing
By default, Inertia does not automatically flush the prefetch cache when you navigate to new pages. Cached data is only evicted when it expires based on the cache duration. If you want to flush all cached data on every navigation, you can set up an event listener.Invalidate on Requests
To automatically invalidate caches when making requests, pass aninvalidateCacheTags prop to the Form component. The specified tags will be flushed when the form submission succeeds.
useForm helper, you can include invalidateCacheTags in the visit options.
invalidateCacheTags in the options.
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 thecacheForprop.
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.