{"id":18274750,"url":"https://github.com/liuderchi/sw-precache-cra","last_synced_at":"2025-04-05T03:30:42.362Z","repository":{"id":35068036,"uuid":"136501172","full_name":"liuderchi/sw-precache-cra","owner":"liuderchi","description":"👷 Customize your service worker in create react app without `npm run eject`","archived":false,"fork":false,"pushed_at":"2022-04-12T22:55:13.000Z","size":172,"stargazers_count":22,"open_issues_count":7,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T14:44:40.164Z","etag":null,"topics":["cli","create-react-app","service-worker","service-worker-precache","sw-precache"],"latest_commit_sha":null,"homepage":"https://sw-precache-cra-demo-cra-contributors.netlify.com/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liuderchi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-07T16:03:02.000Z","updated_at":"2024-11-01T10:39:32.000Z","dependencies_parsed_at":"2022-08-08T04:16:04.728Z","dependency_job_id":null,"html_url":"https://github.com/liuderchi/sw-precache-cra","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liuderchi%2Fsw-precache-cra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liuderchi%2Fsw-precache-cra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liuderchi%2Fsw-precache-cra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liuderchi%2Fsw-precache-cra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liuderchi","download_url":"https://codeload.github.com/liuderchi/sw-precache-cra/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247284911,"owners_count":20913691,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","create-react-app","service-worker","service-worker-precache","sw-precache"],"created_at":"2024-11-05T12:10:38.261Z","updated_at":"2025-04-05T03:30:40.704Z","avatar_url":"https://github.com/liuderchi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sw-precache-cra\n\n👷 Customize your service worker in create react app without `npm run eject`\n\n\n## The Problem\n\nCreate-react-app provides built-in `service-worker.js` but _when you want to extend it_ \\\n(e.g. to cache REST API response) you need to do `npm run eject` and maintain more configs.\n\nSometimes we just want to enjoy customization but keep those big configs unchanged. \\\nThis cli tool helps you achieve this with ease.\n\n## Demo\n\nhttps://sw-precache-cra-demo-cra-contributors.netlify.com/\n\nA page caching GitHub API response with [one simple config][sw-config]. Done in [4 steps][examples-sw-precache-cra]\n\n## Extend Service Worker in Create React App in 2 steps\n\n1. `$ npm i -S sw-precache-cra`\n\n2. Edit the `build` script in `package.json`\n\nThere are 2 examples:\n\n💡 If you want your service worker _cache API response with url `/messages`_:\n\nconcat the build script\n\n```diff\n\"scripts\": {\n  \"start\": \"react-scripts start\",\n- \"build\": \"react-scripts build\",\n+ \"build\": \"react-scripts build \u0026\u0026 sw-precache-cra --config sw-config.js\",\n  \"test\": \"react-scripts test --env=jsdom\",\n  \"eject\": \"react-scripts eject\"\n}\n```\n\nwith one additional js module having any [`sw-precache` configs][sw-precache-configs]:\n\n```js\n// sw-config.js\nmodule.exports = {\n  runtimeCaching: [\n    {\n      urlPattern: '/messages',\n      handler: 'networkFirst',\n    },\n  ],\n};\n```\n\n🎉  You've got custom `build/service-worker.js` after `npm run build`\n\n---\n\n💡 If you want to have _Non minified_ `build/service-workder.js` you can do this:\n\n```diff\n\"scripts\": {\n  \"start\": \"react-scripts start\",\n- \"build\": \"react-scripts build\",\n+ \"build\": \"react-scripts build \u0026\u0026 sw-precache-cra --no-minify\",\n  \"test\": \"react-scripts test --env=jsdom\",\n  \"eject\": \"react-scripts eject\"\n}\n```\n\n🎉  You've got Un-minified `build/service-worker.js` after `npm run build`\n\n\n## Other API\n\n```bash\n$ sw-precache-cra --list-config\n\n# Print current config for sw-precache\n# If you do not specify a config file, default config by CRA is printed\n#\n{ swFilePath: './build/service-worker.js',\n  cacheId: 'sw-precache-webpack-plugin',\n  dontCacheBustUrlsMatching: /\\.\\w{8}\\./,\n  navigateFallback: '/index.html',\n  navigateFallbackWhitelist: [ /^(?!\\/__).*/ ],\n  staticFileGlobsIgnorePatterns: [ /\\.map$/, /asset-manifest\\.json$/ ],\n  staticFileGlobs:\n   [ './build/**/**.html',\n     './build/static/js/*.js',\n     './build/static/css/*.css',\n     './build/static/media/**' ],\n  stripPrefix: './build' }\n```\n\nFor more APIs please do `sw-precache-cra --help`\n\n## Reference\n\n- [Add more service-worker functionality with create-react-app\n](https://stackoverflow.com/questions/47636757/add-more-service-worker-functionality-with-create-react-app)\n- [sw-precache README][sw-precache-configs]\n- [facebook/create-react-app issue #3132](https://github.com/facebook/create-react-app/issues/3132#issuecomment-330610440)\n- [bbhlondon/cra-append-sw\n](https://github.com/bbhlondon/cra-append-sw)\n\n\n## License\n\n[MIT License][mit-license]\n\n\n[mit-license]: https://liuderchi.mit-license.org/ \"mit-license\"\n\n[sw-precache-configs]: https://github.com/GoogleChromeLabs/sw-precache#options-parameter \"sw-precache-config\"\n\n\n[sw-config]: https://github.com/liuderchi/sw-precache-cra/blob/examples/examples/cra-contributors/sw-config.js\n[examples-sw-precache-cra]: https://github.com/liuderchi/sw-precache-cra/commits/examples\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliuderchi%2Fsw-precache-cra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliuderchi%2Fsw-precache-cra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliuderchi%2Fsw-precache-cra/lists"}