When navigating between pages, Inertia mimics default browser behaviour by automatically resetting the scroll position of the document body (as well as any scroll regions you've defined), back to the top. Further, Inertia keeps track of the scroll position of each page and automatically restores that scroll position as you navigate forward and back in history.
Sometimes it's desirable to prevent the default scroll resetting when making visits. You can disable this behaviour using the preserveScroll
option when manually making visits.
Inertia.visit(url, { preserveScroll: true })
You can also lazily evaluate the preserveScroll
option based on the response by providing a callback.
Inertia.post('/users', data, {
preserveScroll: (page) => Object.keys(page.props.errors).length,
})
You can also preserve the scroll position with Inertia links using the preserve-scroll
attribute.
import { Link } from '@inertiajs/inertia-vue3'
<Link href="/" preserve-scroll>Home</Link>
If your app doesn't use document body scrolling, but instead has scrollable elements (using the overflow
CSS property), scroll resetting will not work. In these situations you must tell Inertia which scrollable elements to manage by adding a scroll-region
attribute.
<div class="overflow-y-auto" scroll-region>
<!-- Your page content -->
</div>