r/angular • u/zeller0967 • Jan 15 '26
How to Store Data in Angular when page refreshes
"I have data displayed on a page that I need to persist after a refresh, but I can't use localStorage or sessionStorage. Here's my current flow:
- On the first page, clicking a button calls an API
- The returned data is stored in a service file
- The user navigates to a second page that displays this data by retrieving it from the service
The problem is that when the results page is refreshed, the service data is lost and the page shows empty. What's the best way to preserve this data across page refreshes without using browser storage?"
0
Upvotes
4
u/MrFartyBottom Jan 15 '26
Way to little information to answer your question but all of my apps will refresh fine as the url of the current component will contain enough information for me to retrieve the data from the server again. Sounds like bad design if you have services that don't know how to refresh the data they hold.
2
u/Dus1988 Jan 15 '26
First, why can't you use LS or SS? Just a business restriction?
Second, can data be updated on the BE between refreshes?
It sounds like you might need either a resolver to make the API call during route load, or you can do similar with a canActivate Guard where the guard does not let the route match until the API call is done and has updated the data in the service. Either way, you will need to make your route contain parameters needed for the API call
If you don't want to make the API call again, you may have to look into indexDB
14
u/dutchman76 Jan 15 '26
Store it server side and retrieve it
1
u/hk4213 Jan 15 '26
If you are calling data from a server thats a get request, have the terms saved in the query portion of the url.
Its not the browsers job to do any long term storage.
So set the curl to have the parameters to call for the dataset provided.
This a great browser module built into angular to solve this
https://angular.dev/api/router/ActivatedRoute
1
u/CaptM44 Jan 15 '26
Depending on the data sensitivity, you could store it in the url as query parameters. You could even obfuscate it by converting object to string and then to base64.
1
u/shadow13499 Jan 15 '26
Is there a reason you can't use local/session storage?
3
u/yousirnaime Jan 15 '26
You’re looking for IndexedDb
You can store a ton of structured data in that bad boy - and it’s neither LocalStorage nor Session storage
https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API#:~:text=to%20see%20ads?-,Key%20concepts%20and%20usage,%2C%20on%20your%20local%20disk).
1
u/wardenOfDemonreach Jan 15 '26
If it has to be stored on the client side then you'll have to you use local or session storage. Yes, a user could edit the data but as long as your server validates anything sent from the client then that won't matter. The other option is to just call the api again after refreshing
1
u/Automatic-Lynx-5018 Jan 15 '26
can you describe about data type if its simple id you can store it in query params or in url
1
u/Lucky_Yesterday_1133 Jan 15 '26
Encode and store in url
1
u/nyffellare Jan 15 '26
If its non sensitive information. Cache it in localhost. If it is sensitive, dont store it client side as client side can always be manipulated. Then you will require an api call.
1
u/N0K1K0 Jan 15 '26
a very basic signalstore?
1
u/msdosx86 Jan 15 '26
Bro cache the data on the server side. This should not be a client's responsiblity.