Service Worker weird caching
Is this a bug report?
Yes
Did you try recovering your dependencies?
Yes
Which terms did you search for in User Guide?
https://github.com/facebook/create-react-app/issues/2398
Environment
Environment Info:
System: OS: macOS High Sierra 10.13.6 CPU: x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz Binaries: Node: 9.10.0 - /usr/local/bin/node Yarn: 1.10.1 - /usr/local/bin/yarn npm: 5.6.0 - /usr/local/bin/npm Browsers: Chrome: 69.0.3497.100 Firefox: 62.0.3 Safari: 12.0 npmPackages: react: ^16.5.2 => 16.5.2 react-dom: ^16.5.2 => 16.5.2 react-scripts: 2.0.4 => 2.0.4 npmGlobalPackages: create-react-app: 2.0.3
Steps to Reproduce
(Write your steps here:)
- Clone https://github.com/gabrielmicko/react-create-app-pwa. It is almost a clean app of react-create-app.
- Install dependencies.
yarn install. Create a build.yarn run build. - Run
serve -s build(if you don't have serveyarn global add serve), ornode server.js. - Open localhost:5000. You should see service worker precaching files.
- Do some change in the
App.jsand runyarn run buildagain. - Refresh the page and check your console. You should see the new files. It will log an "Update happened" message.
- Refresh the page to see the updates.
- Can't see the changes I made.
Expected Behavior
After refreshing the page at point 7. I should see my changes.
Actual Behavior
I see the old version of my app.
(Write what happened. Please add screenshots!)
Reproducible Demo
https://www.youtube.com/watch?v=Hl5HbZ0TWoY
You need to close all tabs or fully close the browser to see the changes reflected. A page reload will not reflect the changes.
Hey @timer, from what I see on the video, OP is using the incognito mode with a single open tab.
I cloned the repo and I can reproduce the problem as well. Maybe something is wrong with the V2 service worker?
/cc @jeffposnick
Is this the expected behaviour? Does it happen only on localhost? When onUpdate gets called I would like to add a popup which prompts the user to reload the page.
Yes -- the expected behavior is that you must close all tabs or browser before the new content appears. This is the behavior everywhere, not just on localhost.
You should be able to override this via ServiceWorkerGlobalScope.skipWaiting() but you need to make sure you coordinate this across all instances of your apps running in their browser.
May we convert this into a "doc improvement" issue so that we keep track of this? I think the current documentation should at least mention it.
Yeah we should probably note this in https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#offline-first-considerations
In the meantime I found this: https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin skipWaiting: true does the job.
Yes, this does warrant improvements to the v2 docs. I can tackle that next week.
FYI, https://github.com/facebook/create-react-app/issues/3613#issuecomment-353467430 is the best explanation of why defaulting skipWaiting to false was the safest thing to do for c-r-a v2.
tl;dr is that if skipWaiting is true and you lazy-load resources, there's a decent chance that you might end up trying to lazy-load a URL that no longer exists in the service worker's cache or on the server. If you don't lazy-load versioned URLs and you want the old behavior, then changing your config to using skipWaiting: true is the alternative.
@gabrielmicko , @jeffposnick , Can the skipWaiting: true option be applied without ejecting?
On this line the workbox-webpack-plugin is registered with clientsClaim: true and the documentation for workbox-webpack-plugin's skipWaiting option says
Whether or not the service worker should skip over the waiting lifecycle stage. Normally this is used with
clientsClaim: true.
Should the default behaviour be setting clientsClaim: true and skipWaiting: true?
@mymattcarroll yes, that way you get the update asap. For now I don't know how am I going to get a callback about the update onUpdate seems to be broken if skipWaiting is set to true. I'll take a look later. Also I don't know how I can add skipWaiting: true without hacking in the node_modules. Any idea?
Finally I was able to test with "react-scripts": "^1.1.0". Actually it works how it should!
serviceWorker.register({
onUpdate:() => {
//Some UI notification comes here, then reload
window.location.reload();
}
})
Once it reloads, I instantly see the change, whereas in create-react-app v2 I won't. I guess this is something that has to be addressed. Please at least make it configurable. For example:
serviceWorker.register({
skipWaiting: true
onUpdate:() => {
});
This case onUpdate would instantly fire once skipWaiting() get's called.
What do you think?
/cc @jeffposnick
Edit: actually I am a little confused if it could be added to serviceWorker.js file. Cause I guess it has to be in the service-worker.js.
@gabrielmicko, it will have to be a build time configuration as it needs to be added to the webpack configuration here.
Perhaps configuration in package.json? The package.json is already being used for build time configuration using the browserlist property. Maybe something like:
{
"serviceWorkerOptions": {
"skipWaiting": true
}
}
@mymattcarroll This would be awesome. Other questions is how do you let serviceWorker.js know that skipWaiting has been called? The developer want's to subscribe to that event, so he can let the users know there was an update.
@Timer, it seems the default behaviour of the service worker has changed between v1 and v2. Our project is no longer hitting this line to allow us to prompt the user when new content is available by just refreshing the current page.
Was this intentional?
If it was, I have been able to make changes to a local fork to get v1 behaviour to occur again by making the following changes:
- Updating the
serviceWorker.jscheck theeventobject passed toregistration.installing.onstatechangeinstead of checkingregistration.installing.state(it seemed there was a race condition where multiple state changes were occurring so fast thatregistration.installing.statewas always equal to'activated') - Updating
webpack.config.prod.jsto passskipWaiting: trueto workbox-webpack-plugin configuration
I would be happy to contribute with a pull request if you believe these changes are desirable for others.
If setting skipWaiting: true by default is not desirable, would you consider a configuration option, perhaps in package.json? I would be happy to work on making the UX similar to how the browserslist property is added to package.json with a prompt.
This library is awesome, would hate to consider ejecting...
Very well said @mymattcarroll! This is what I realised as well! I guess the default behaviour should match v1 so that devs would not need to change anything!
So I had mistakenly thought that being able to override the default Workbox options via an external config file had made it into c-r-a v2 branch, but as per https://github.com/facebook/create-react-app/pull/5111#issuecomment-425458687 that was just a proposal and not actually something that landed.
That would allow folks who feel comfortable with the tradeoffs around potentially buggy lazy-loading to opt-in to using skipWaiting: true, and get a quicker update flow.
I've filed separate issue to track the addition of that config file: https://github.com/facebook/create-react-app/issues/5359
I'm in favor of adding a cra.config.js for these config and others, overriding default ones!
At first I thought it could already be done.
@mymattcarroll
Can the skipWaiting: true option be applied without ejecting?
This is the hacky way I had to do it install replace-in-file
npm install --save-dev replace-in-file
create a file in the root called hack-add-skipWaiting-true-to-react-script.js
UPDATE
Changed webpack-config.prod.js to webpack-config.js since the file is renamed in current version
'use strict';
const replace = require('replace-in-file');
try {
console.log('Adding skipWaiting: true to react-script webpack-config.js');
replace.sync({
files: 'node_modules/react-scripts/config/webpack.config.js',
from: /clientsClaim: true,$/gm,
to: (match) => {
return match + ' skipWaiting: true,';
},
});
} catch (e) {
console.log('Something went wrong when trying to add skipWaiting: true to react-script webpack-config.js', e);
process.exit();
}
Add it as a postinstall script to your package.json
{
...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"postinstall": "node hack-add-skipWaiting-true-to-react-script.js"
}
}
run npm install and skipWaiting should now be set to true