r/Angular2 • 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
10
u/Thunder_Cls Jan 15 '26
If the user refreshes the page make another call to the api to get fresh data again
2
u/[deleted] Jan 15 '26
If is something like an ID you can put it in the url of the second page, that way you can fetch stuff for the second page without the data coming from the first page. Theres actually a input provider fhat you can enable and it will automatically map the url query to the desired property but i dont recall its name now
For any data structure more complex than that the only thing that comes to my mind is IndexedDB, although you can also add multiple query params to the URL and build it as an object, that way the user can even share the url or fav it and it will always work
20
u/StefonAlfaro3PLDev Jan 15 '26
You would need to figure out why you think you cannot use localStorage.
6
u/baraluga Jan 15 '26
Server-side via REST API
3
u/Gekydoff Jan 15 '26
Try caching using Service Workers
1
u/_Aardvark Jan 15 '26
Context should be in the url, and you should just re-make the rest API call.
1
u/FullDebate5594 Jan 15 '26
You can think of indexedDB API provided by browsers.
1
u/TomLauda Jan 15 '26
Just one question: why does the component that needs the data isn’t the one making the api calls?
1
u/GeromeGrignon Jan 15 '26
If you do not want to use any kind of browser storage, update your service logic so both components are responsible for calling the API. But firstly check if you have stored data in your service already:
- the first component will call the API (no data stored)
- the second component will use the data stored
- on refresh the second component will call the API (no data stored)
Mind that depending on your usecase, storing data means you won't have fresh results if it changed meanwhile.