With the new PWA functionality for Angular it's quite simple to make your app work offline via a service worker. My question is how to leverage NGRX to cache ones state, so that it is available offline.
I'm also concerned, that the client might modify the state when it's cached in the browser. (e.g. when using ngrx-store-localstorage
)
So think carefully before caching POST requests in your PWA! If your are looking for a way of make modifying POST requests work offline, I recommend looking at the Mozilla code-recipe for deferred POSTs. Luckily, the Service Worker can intercept any asynchronous request, so there is not problem getting our hands on POST requests.
The browser's Cache API simply will not accept them. Here is a workaround using a custom cache in InexedDB. There are, of course, reasons, why POST requests are not part of the current Service Worker specifications. Here are some of them: Theoretically, POSTs are expected to modify data.
Many non-REST APIs work with POST requests even for reading data: GraphQL, SOAP and other RPC-APIs being typical examples. However, POST requests cannot be cached and used offline in a Progressive Web App out of the box. The browser's Cache API simply will not accept them. Here is a workaround using a custom cache in InexedDB.
When active and running, only one instance is typically available no matter how many clients are in memory (such as PWA windows or browser tabs). You should set the scope of your service worker as close to the root of your app as possible. This is the most common setup as it lets the service worker intercept all the requests related to your PWA.
You are right, a library such as ngrx-store-localstorage
is the solution, there's no magic behind.
The concern about modifying the state isn't your problem. If someone wants to abuse the frontend of your app / website - he / she can do that. The main goal here is to assure that your backend doesn't accept malformed data (never trust data you receive from a user) and you don't store sensitive data in the frontend's store.
Anybody can open developers tools on this page and modify your question - up to them, because it is their local copy and it doesn't mean something is wrong with stackoverflow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With