0

UPDATE - Not sure how to update this to say pate's comment made me realise what I was doing wrong? Either way this is resolved for me now thanks

I return JSON from the server that contains a list of objects, each representing a digital post it note (text, position on page, database id etc.) I cache this in the service worker during fetch as normal.

Currently as users update/add post its I update a local copy of the JSON in the javascript as well as sending the info to the server.

What I want to do is as they update/add items the client JS will also save the new JSON to the application cache, then on page load use a cache-while-revalidate pattern so they only need to refresh if another user makes changes to their data. Otherwise they will get the cached JSON that will already contain their most recent changes.

As the application cache is versioned and the version number is stored in the sw.js file I'm currently sending a message (using MessageChannel) from the client to the SW to get the version number so the client can then put the JSON into the right cache. The only other options I can think of are to either make the application cache version a global variable somewhere other then the SW.js or just send the entire JSON in the message to the SW and let it put the update JSON into the cache.

Either way these all seem like workarounds/anti-patterns and I can't seem to find a better way of the client updating the application cache.

The other reason I want to do this is so that I can eventually move to an offline mode of working using the background sync api to handle add/updates etc. so want the cached JSON to be as up to date as possible.

Am I missing a trick somewhere?

Simon
  • 1,613
  • 1
  • 12
  • 27
  • I might be missing something but are you sure you need a versioned cache? If your *static assets* include their version information in their filenames (bundle.kjasdfkajskdfadsf.js etc.) why would you need a version number for the *API* responses? The API responses are anyhow completely out ot date the moment someone makes a change --> the db changes, your version number in the SW.js doesn't change. My gut says you should drop the version number from the API stuff and just use stale-while-revalidate and always update to the latest version through Caches API. What do you think? – pate Apr 16 '18 at 04:12
  • Can you add any details like: code used, error problem encountered? [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask), [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) Show the community what you have tried. – ReyAnthonyRenacia Apr 16 '18 at 10:57
  • @pate This is for data rather the javascript itself, all the javascript is already bundled and versioned. I used a versioned cache for the data if we decided to change the data structure and so don't want old versions of the data persisting BUT you made me realised if I'm using cache-while-revalidate then I can put this stuff in an unversioned cache as it will always get the latest regardless, so thanks :-) – Simon Apr 16 '18 at 11:00
  • @noogui This is sorted for me now thanks, I normally do put code etc. up but I didn't think it was that relevant for this one as it's pretty much just boiler plate SW stuff, this was more of an architectural question really as my PWA/SW knowledge is a work in progress and I couldn't find any similar examples after a lot of Googling! – Simon Apr 16 '18 at 11:04
  • @Simon great that it got sorted out! – pate Apr 16 '18 at 17:19

0 Answers0