Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGRX Offline Cache with a PWA and Service Worker

This isn't a bug, but rather a question on how to approach my issue!

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)

like image 300
Flo Avatar asked Dec 08 '18 10:12

Flo


People also ask

Can I cache POST requests in my PWA?

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.

Why doesn't the browser's cache API support 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.

Can post requests be used offline in a progressive web app?

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.

How many instances of a PWA should I set up?

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.


1 Answers

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.

like image 67
satanTime Avatar answered Sep 27 '22 22:09

satanTime