Guidlines for Using Workbox
See original GitHub issueI am on React-Scripts 2.0.5. I am eager to use the new Workbox functionality that is mentioned in the release notes for 2.0. I can’t find any directions on how to do it though.
There are posts on Medium, Freecodecamp and such that are really just hacks to the 1.x versions of React-scripts.
I had thought since it has just been added as a dependency we could get some assistance on how and where to add configurations. When I include Workbox in my registerServiceWorker.js
and attempt to set some strategies on assets it crashes the entire app once built.
Unable to import module 'workbox-routing' from 'WORKBOX_CDN_ROOT_URL/workbox-routing.prod.js'.
I understand serviceworkers are now opt-in.
I understand that in the latest version of create react app the service worker register file is now just called serviceworker.js
.
I compared the old registerServiceWorker.js
to the new serviceworker.js
and they are the same. I also am able to build with the default service worker file and I do see Workbox in the application tab in Chrome dev tools. I know its working out of the box. I just want to be able to edit it. Guidance would be greatly appreciated.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:22
Top GitHub Comments
Here’s how I’ve done it with CRA and without ejecting. It uses the workbox build workflow. (cra 2.x)
TL;DR - This involves not using the SW generated by create-react-app but generating your own SW with injectManifest mode using the Workbox build workflow. Build commands will be added to react-build script in package.json
NOTE: Keep an eye on #5369 I think it will allow configuring Workbox using the Webpack workflow using a custom configuration file.
Step1 : Make sure you register for a SW in the main index.js file. i.e. change
serviceWorker.unregister();
toserviceWorker.register();
Step2: Inside
src/serviceWorker.js
inwindow.addEventListener('load', () => {
change the swURL to point to a new file (this is what will be created in the build step) so, change this -const swUrl = ``${process.env.PUBLIC_URL}/service-worker.js``;
toconst swUrl = ``${process.env.PUBLIC_URL}/sw.js``;
Step3: Create two files under
src/ - sw-build.js and sw.js
Step4: In
sw-build.js
add the code as provided by Google’s injectManifest example here -Step 5: Inside your custom service worker template file i.e.
src/sw.js
add the injection point and your cache rules. e.g - in the file src/sw.js -step 6:
npm install workbox-build --save-dev
I think workbox-build is already included in node_modules, but good idea to make an entry in package.jsonstep 7: in
package.json
add the workbox build step so, in package.json, under scripts add a new entry -"build-sw": "node ./src/sw-build.js"
and then change:"build": "react-scripts build
to"build": "react-scripts build && npm run build-sw
When your run
npm run build
you will see the the filesw.js
inside your build folder. The CRA generated service-worker.js will continue to be there. You can run a build command to clear it, but it won’t be used because we change the swURL in step2.Again, all of this may not be relevant if #5369 provides a way to use the webpack flow by providing a custom configuration file.
@Nick-t-go from what i have seen in the other blog posts they all dont work with the current v2 create react app scripts as they all try to get rid of sw-precache and use workbox instead. Now with v2 you dont have to get rid of sw-precache anymore and because of that, all guides i have seen are obsolete and do not work anymore