In this episode, we dive into a common annoyance with web forms: when you submit a form on a long page and suddenly find yourself jolted back to the top. This is the default browser behavior, especially when a new page loads, but in single-page applications—like the ones you build with Inertia—it doesn't always make sense.
We'll kick things off by demonstrating the problem. If you scroll down a bit and hit the post button, the page defaults right back to the top. Not ideal! We talk through why this happens, and why you'll usually want to keep the user's position on the page, especially if your forms or important content aren't right at the top.
The fix is easy: Inertia provides a handy preserveScroll
option you can pass when you submit your forms. By setting this to true
, it keeps the user's scroll position after the form is submitted—super useful when showing validation errors or when your page is lengthy.
We'll also briefly mention that this option isn't just for forms; you can use it with other types of Inertia requests too. We'll go into more situations where it's helpful later in the course, but for now, this is a simple and practical trick you'll be using a lot to improve your app's user experience.