- Notifications
You must be signed in to change notification settings - Fork0
👷 Customize your service worker in create react app without `npm run eject`
License
liuderchi/sw-precache-cra
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
👷 Customize your service worker in create react app withoutnpm run eject
Create-react-app provides built-inservice-worker.js
butwhen you want to extend it
(e.g. to cache REST API response) you need to donpm run eject
and maintain more configs.
Sometimes we just want to enjoy customization but keep those big configs unchanged.
This cli tool helps you achieve this with ease.
https://sw-precache-cra-demo-cra-contributors.netlify.com/
A page caching GitHub API response withone simple config. Done in4 steps
$ npm i -S sw-precache-cra
Edit the
build
script inpackage.json
There are 2 examples:
💡 If you want your service workercache API response with url/messages
:
concat the build script
"scripts": { "start": "react-scripts start",- "build": "react-scripts build",+ "build": "react-scripts build && sw-precache-cra --config sw-config.js", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject"}
with one additional js module having anysw-precache
configs:
// sw-config.jsmodule.exports={runtimeCaching:[{urlPattern:'/messages',handler:'networkFirst',},],};
🎉 You've got custombuild/service-worker.js
afternpm run build
💡 If you want to haveNon minifiedbuild/service-workder.js
you can do this:
"scripts": { "start": "react-scripts start",- "build": "react-scripts build",+ "build": "react-scripts build && sw-precache-cra --no-minify", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject"}
🎉 You've got Un-minifiedbuild/service-worker.js
afternpm run build
$ sw-precache-cra --list-config# Print current config for sw-precache# If you do not specify a config file, default config by CRA is printed#{ swFilePath:'./build/service-worker.js', cacheId:'sw-precache-webpack-plugin', dontCacheBustUrlsMatching: /\.\w{8}\./, navigateFallback:'/index.html', navigateFallbackWhitelist: [ /^(?!\/__).*/ ], staticFileGlobsIgnorePatterns: [ /\.map$/, /asset-manifest\.json$/ ], staticFileGlobs: ['./build/**/**.html','./build/static/js/*.js','./build/static/css/*.css','./build/static/media/**' ], stripPrefix:'./build' }
For more APIs please dosw-precache-cra --help
About
👷 Customize your service worker in create react app without `npm run eject`
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.