[SalesForce] How to handle caching for browser back button in lwc

I am building a multi-step webform for users to fill out for an application. This will be an unauthenticated public page.

I was hoping to get some guidance on which storage to us for caching data between steps.

I want the browsers navigation buttons to maintain functionality of going back and forth through the form.

Right now I have smaller components encapsulated in a parent component. When the user navigates between the pages I save the state to sessionstorage in the browser. I would like to get away from this.

Are there any services/apis provided by lightning components to handle this. Some things I have explored are.

  • Using session/local storage in the browser to save state between pages. (I opted out of this because I don't want to lose data if users have this blocked.)
  • Using history state. (Might still use this)
  • Cache.Session storage in apex. This seems like its only use is for authenticated users

Any direction would be greatly appreciated.

Best Answer

One option is to use lightning-navigation apis, you can store all of your information in the state object of the page object reference. State is stored in the history object of the browser, so going back and forth between pages would allow your data to persist. Your components can then pull from the current page reference on the page and grab the needed data from the state object.

https://developer.salesforce.com/docs/component-library/bundle/lightning:navigation/documentation

There are probably some good videos online explaining it.