reactjscreate-react-appservice-workerworkbox

Workbox and Create react app doesn't serve new version of index.html


I used create-react-app my-app --template cra-template-pwa-typescript to init my pwa project. Keep most of the code from the template, which has:

precacheAndRoute(self.__WB_MANIFEST)

I run into the problem that service worker doesn't detect new changes I made to a react app, as well as a new version of index.html. In addition, the react app is served from AWS S3

From my investigation, it looks like workbox doesn't give index.html a new post fix __WB_REVISION. I did the following steps:

  1. Remove workbox pre-cache
  2. Unregister the workbox via chrome dev console
  3. Relead the page which re-register the service worker and new index.html is cached

Is there a way to have new __WB_REVISION postfix for every build or what could be a solution to force service worker to served a new index.html - could start with non userfriendly way


Solution

  • I figured it out and it has nothing to do with workbox, the problem is because of double caching. I found an idea from this question.

    In my case, where I deployed the react app to AWS S3 and served it with S3 + cloudfront. The way I solve the problem is invaliding the service-worker.js on new deploy.

    Here is a small snippet with AWS CDK to solve the problem

        new s3Deployment.BucketDeployment(this, `${servicePrefix}-bucket-deployment`, {
          sources: [s3Deployment.Source.asset('./build')],
          destinationBucket: bucket,
          distribution,
          distributionPaths: ['/', '/index.html', '/service-worker.js'],
        });
    

    The distributionPaths tells cloudfront to invalidate new service-worker.js for every new deployment