0

I am new to rxjs and am trying to figure out how to have Observables keep data on switching and/or reloading pages.

I can stay on the same page and the caching service works. But when I go to another page in the web application (this is not a single page web app) the data is refreshed and the cache service is empty. Is there a way besides using localStorage or cookies to store the data?

getToken(id: number): Observable<TempTokens> {
    if (!this._token) {
      const url = `${this.tokenUrl}/${id}`;
      this._token = this.http.get<TempTokens>(url).pipe(
        map((res: TempTokens) => res),
        publishReplay(1),
        refCount(),
      );
    }
    return this._token;
  }

My goal is to not store the data in the browser so I can prevent users from modifying the storage.

Edited to emphasize not local storage and not a single page web app

Seeker C
  • 67
  • 9
  • localStorage or a remote server is pretty much your only option. don't get caught up in worrying about users messing with data... there's really nothing you can do about that and it's not on you if they decide to. I could open my console at anytime on any webapp and start messing with the site... not the problem of the site owner – bryan60 Aug 04 '19 at 22:46
  • Possible duplicate of [In single page application, how to store data and get data after refresh the page](https://stackoverflow.com/questions/39750948/in-single-page-application-how-to-store-data-and-get-data-after-refresh-the-pag) – Zze Aug 04 '19 at 23:21
  • Possible duplicate of [rxjs observable angular 2 on localstorage change](https://stackoverflow.com/questions/40393703/rxjs-observable-angular-2-on-localstorage-change) – JayChase Aug 05 '19 at 00:56
  • I was trying to avoid localStorage, sessioStorage, or cookies – Seeker C Aug 05 '19 at 00:58
  • You keep saying you want to avoid storage, but you don’t say why. The only way to avoid those things is by using a remote server and loading from there on page load, but that’s not always an option. – bryan60 Aug 05 '19 at 21:02

2 Answers2

0

sessionStorage will survive a browser refresh and localStarage will survive closing the browser and reopening it.

https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
0

An alternative to using localStorage and SessionStorage is the Cache API.

https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/cache-api#retrieving_from_a_cache

While the user can delete from the cache they can't modify data stored in it.

Seeker C
  • 67
  • 9