Description
Is this a bug report?
Yes
Did you try recovering your dependencies?
Yes, happened on multiple deployments.
Which terms did you search for in User Guide?
Read the parts about service worker and also searched for similar issues, found this one: #3613 but that was about chunking, which I do not use.
Environment
node -v
: 9.4.0npm -v
: 5.6.0yarn --version
(if you use Yarn): 1.3.2npm ls react-scripts
(if you haven’t ejected): n/a
Then, specify:
- Operating system: Mac OS High Sierra
- Browser and version (if relevant): Google Chrome 63
Expected Behavior
When I deploy a new version of my application I expect it to load the old HTML and load the old JS bundle from it's cache. If that's no longer available (for whatever reason) it should fetch the new HTML and with that also the new JS bundle.
Actual Behavior
The service worker serves the old index.html, but it tries to get the JS bundle from the server. Because a new version has been deployed that bundle is no longer there, so it receives a 404 error, resulting in a white (broken) page. Refreshing the page fixes the issue.
The contents of my index.js
// @flow
import React from 'react';
import ReactDOM from 'react-dom';
import registerServiceWorker from './registerServiceWorker';
import Organisation from './apps/organisation';
const release = process.env.REACT_APP_GIT_COMMIT;
// Set up Sentry
if (process.env.NODE_ENV !== 'development') {
window.Raven.config(
'https://secret@sentry.io/secret',
{ release },
).install();
}
console.log(`Commit: ${String(release)}`);
const element = document.getElementById('root');
if (element) {
ReactDOM.render(<Organisation />, element);
}
registerServiceWorker();
Here screenshots of the network tab during the 'broken state'
Activity
gaearon commentedon Jan 21, 2018
cc @jeffposnick
robertvansteen commentedon Jan 21, 2018
Here is an example of a generated
service-worker.js
As you can see there is no
.js
bundle there.This is what the build process logged:
robertvansteen commentedon Jan 21, 2018
I tried a few more things and it seems like when I comment out some code it does gets included in the SW precache list. But it's not a particular piece of code, so it looks like it's related to the file size. The current size is far below the maximum default size in SWPrecache (which is 2MB) and as far as I can tell CRA doesn't change that limit.
jeffposnick commentedon Jan 23, 2018
Here are some "odd" things that your screenshots/code snippets illustrate which I'd like to get to the bottom of:
index.html
from within the service worker shown in your Network Panel screenshot, but there's only one reference to/index.html
in yourprecacheConfig
. (This might be a red herring, but it's odd to see.).js
files in theprecacheConfig
. Using a cached version ofindex.html
without a corresponding cached entry for the.js
would lead to version mismatches that you're seeing.You may be on the right track regarding size limits, as
sw-precache
will, by default, not precache any resource whose actual size, uncompressed, is above 2mb. Your log messages indicate that the compressed size of main.10cf50b4.js is 399.47kb, and I could easily imagine the uncompressed size being over 2mb. Can you confirm that? (There's normally a build-time message logged about resources that are too large to precache, but that's disabled increate-react-app
.)robertvansteen commentedon Feb 2, 2018
@jeffposnick I'm not sure how I can see the unminified file size with
create-react-app
but I can confirm that code splitting fixed this problem. I think it's a good thing that large files are not downloaded for offline usage but it would be good to show the message if the file size is too large. I now have to manually check theservice-worker.js
before deploying to make sure that all chunks are in there because if they don't my site breaks on the next deployment.@gaearon would it be possible to show that message?
jeffposnick commentedon Feb 2, 2018
It's going to get trickier to justify showing the logged output from the
sw-precache
plugin in the future because service worker registration isn't going to be on by default. So you'd end up with a scenario in which there were warning messages being logged corresponding to a feature that a developer isn't actually making use of.robertvansteen commentedon Feb 2, 2018
Maybe it could be a general warning like “hey be warned your bundle (or chunk) is big (and will not be cached for offline use if you use a service worker), consider splitting it in multiple chunks to improve load time” or something
jeffposnick commentedon Feb 8, 2018
FWIW, I found #2612 which effectively tracks the same request (and is closed).
RohovDmytro commentedon Feb 16, 2018
Had same issue. As it referenced I've outline some thought here. #3574
Stas-Buzunko commentedon May 17, 2018
@rogovdm i'm using firebase hosting as well and facing the same problem, have you fixed it?
Shpadoinkle commentedon Jun 12, 2018
yep same here. Have tried adding header caching rules to the header section with no success.
Currently trying to unregister the serviceWorker but this seems like a horrid way to get around this bug.
I swear this used to work just fine. So not sure where the issue began.
But judging on the history of react devs and how often issue threads just close down due to inactivity, I doubt this will be addressed any time soon.
samanmohamadi commentedon Jun 14, 2018
I have the same problem.
updating sw from chrome dev tools will refresh index.html.
sh00ter commentedon Jun 20, 2018
Any update on this or the only real solution is to opt-out of caching?
robertvansteen commentedon Jun 20, 2018
@sh00ter the solution is code splitting. See one of my comments above.
sh00ter commentedon Jun 20, 2018
@rovansteen I am using Loadable to split the code but I guess that one of the chunks is still large enough not to be picked up by the service worker.
robertvansteen commentedon Jun 20, 2018
@sh00ter yeah unfortunately there is no warning or anything like that to see if one of the bundles is too big so we do this by manually checking it.
robertvansteen commentedon Jun 20, 2018
But you could implement something like this to automate that process: #2612 (comment)
Stas-Buzunko commentedon Aug 21, 2018
#3882 (comment)
solution worked for me
nfantone commentedon Oct 4, 2018
@Timer I hit this very same issue today after deployment using latest CRA
2.0.4
and the new Workbox service worker script.Manually unregistering the worker and refreshing makes the site load, but any subsequent refreshes just crashes it again for me.
Used to work just fine with previous versions, mind you (
sw-precache
).I believe this issue should be re-opened.
EDIT
For what I've been gathering around, this seems to be related to chunk sizes - however, my app's chunks are actually quite small:
As you can see from the names of the chunks resulting from the build and the ones being fetched from the worker (screenshot),
service-worker.js
tries to load a different version every time (cc. @jeffposnick)I'm at a loss here, at the moment.
Timer commentedon Oct 4, 2018
Please file a new issue if you believe you're experiencing a bug.