How To: Update the Application Without Rebuilding

PWA uses ServiceWorkers to cache data to track file changes by revision. When changing any file, you need to:

  • make sure it is in the precache manifest;
  • get a hash from the content of that file;
  • update the revision in precache manifest.

The manifest is located in the service-worker.js file - this is minified JS code. So, changing the revision there is not the easiest thing.

The manifest is in a variable:

const A={precacheManifest:[{...}, {...}]}

where A can vary. Therefore, a fixed property was created not to change the uglify rules for the whole project.

You can get it out with a regular expression by precacheManifest: from [ to ]. Look at the example file below.

The result would be the string like:


A possible variant of how to parse a string: replace ‘ with “ and split the string into an array line by line to get an array of strings, each of them is parsed as JSON.

Further, let’s consider that we have the names of the files that were changed during the update. Find these files in the resulting array by the file name and change the revision in the found object.

A new revision is created with the following logic:

(string) => {
  const md5 = crypto.createHash('md5');
  return md5.digest('hex');

So, an encryptor is created using the md5 algorithm: we send the string (file content) and get the result as a cache string.

After that, collect the array again into a string and replace it in the original service-worker.js.