I've been reading SO posts and Github issues for the past few days on this topic and I still can't seem to find a combination that works for my setup. Hoping that someone can point me to a specific solution. Here's what's going on.
I'm using create-react-app v2 with sw-precache
. Builds go through CircleCI to push new files to S3, which are in turn served from CloudFront. The final step of the build is to invalidate the previous CloudFront distribution.
The problem I'm running into is when a new build is deployed and the service worker is updated, the next page load throws an error because the bundle files that are served by the service worker no longer exist. To say that again, I can load the site at www.example.com
but if I then open a new tab and load www.example.com/test
I get the Uncaught Syntax Error.
A few notes about my setup.
I created a behavior in CloudFront to set
Minimum and Maximum TTL
forservice-worker.js
to0
.For my
sw-precache-config.js
, here's my setup:
module.exports = {
staticFileGlobs: [
'build/index.html',
'build/static/css/**.css',
'build/static/js/**.js'
],
swFilePath: './build/service-worker.js',
stripPrefix: 'build/',
handleFetch: false,
runtimeCaching: [...]
}
- When I load the first page after a build, for the index.html I see the following response:
Status Code: 200 OK (from ServiceWorker)
My assumption is that I want the index.html file to not be cached by the service worker. But if I do that, won't I lose my offline capabilities? Should I set up a runtimeCaching for index.html to be networkFirst
so that I can always ask for it but fall back to cache when offline? If so, would that look like this:
runtimeCaching: [
urlPattern: '/index.html',
handler: 'networkFirst'
]
Some of the things I've tried but haven't worked for me include showing a message to users to reload when this happens (technically works but user experience is not optimal). Also looked at skipWaiting: false
option, but kept seeing the same error on new builds.