#Redirect back with saved scroll position and updated data

1 messages · Page 1 of 1 (latest)

silk ridge
#

Context: In my application I have an index page with paginated records. I want to user to be able to view and then delete records.

The expected user flow for this interaction is:

  1. User on Index page scrolls to find the record they want.
  2. User clicks "view" Link to view the record.
  3. User clicks "delete" Button to delete the record.
  4. The User should be redirected back to the Index page with the same scroll position and the record now missing.

So far I have been handling redirects back by using the browsers window.history.back() functionality (which keeps the old scroll position and url params), however this will not refresh the records.

I'm looking to see if I'm missing something obvious or if anyone knows of a way to solve the issue.

silk ridge
#

Redirect back with saved scroll position and updated data

empty jacinth
#

I would seriously read through all of the Inertia documentation before proceeding. Inertia has built in things where you shouldn’t have to reach for window.history. In short, the server action responsible for deleting the record should simply redirect back. Use inertia to preserve scroll.

#

You can start here: https://inertiajs.com/manual-visits. I read through the documentation periodically because it doesn’t take long and you’ll avoid a lot of unnecessary logic for things Inertia already provides for.

silk ridge
#

Hi, thanks for the help!

I apologise, I should have mentioned which different approaches I have attempted so far before uploading the question.

I have checked the documentation, and tried using Inertia's preserveScroll feature with router.visit however unfortunately it only keeps the scroll of the page you are on.
So If I on /page1 I scrolled down 250px, clicked a link to navigate to /page2, then scrolled 500px
When navigating back to Page1 with router.visit('/page1', {preserveScroll: true})
Page1 would load scrolled down 500px not the 250px that I am looking for.

That's the reason I have been using the browser back functionality to handle this because it loads the previous page with it's previous scroll value. Unfortunately this also means state on the previous page is not updated.

I also tried looking into Inertia's event system (https://inertiajs.com/events) so the page itself could detect if it was navigated back to. However while Inertia intercepts navigation through history and emits events it doesn't send any information other than the page being loaded through. So there's no way to detect whether the page was visited normally or through a history navigation.