# sw-precache-cra

> customize your service worker in create react app without `npm run eject`

Latest version **1.0.0** (published 2019-08-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install sw-precache-cra
pnpm add sw-precache-cra
yarn add sw-precache-cra
bun add sw-precache-cra
```

Provides the command `sw-precache-cra`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-08-17 |
| First published | 2018-06-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Te-Chi Liu |
| Maintainers | liuderchi |
| Keywords | service-worker-precache, sw-precache, cli, create-react-app, service-worker, pwa |

## Links

- npm: https://www.npmjs.com/package/sw-precache-cra
- Homepage: https://github.com/liuderchi/sw-precache-cra
- npm.io page: https://npm.io/package/sw-precache-cra

## Dependencies (3)

- [commander](https://npm.io/package/commander.md) ~2.15.0
- [uglify-js](https://npm.io/package/uglify-js.md) ^3.4.0
- [sw-precache](https://npm.io/package/sw-precache.md) ^5.2.1

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2019-08-17
- 1.0.0-alpha.2 — 2018-06-07
- 1.0.0-alpha.1 — 2018-06-07

## README

# sw-precache-cra

👷 Customize your service worker in create react app without `npm run eject`


## The Problem

Create-react-app provides built-in `service-worker.js` but _when you want to extend it_ \
(e.g. to cache REST API response) you need to do `npm 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.

## Demo

https://sw-precache-cra-demo-cra-contributors.netlify.com/

A page caching GitHub API response with [one simple config][sw-config]. Done in [4 steps][examples-sw-precache-cra]

## Extend Service Worker in Create React App in 2 steps

1. `$ npm i -S sw-precache-cra`

2. Edit the `build` script in `package.json`

There are 2 examples:

💡 If you want your service worker _cache API response with url `/messages`_:

concat the build script

```diff
"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 any [`sw-precache` configs][sw-precache-configs]:

```js
// sw-config.js
module.exports = {
  runtimeCaching: [
    {
      urlPattern: '/messages',
      handler: 'networkFirst',
    },
  ],
};
```

🎉  You've got custom `build/service-worker.js` after `npm run build`

---

💡 If you want to have _Non minified_ `build/service-workder.js` you can do this:

```diff
"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-minified `build/service-worker.js` after `npm run build`


## Other API

```bash
$ 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 do `sw-precache-cra --help`

## Reference

- [Add more service-worker functionality with create-react-app
](https://stackoverflow.com/questions/47636757/add-more-service-worker-functionality-with-create-react-app)
- [sw-precache README][sw-precache-configs]
- [facebook/create-react-app issue #3132](https://github.com/facebook/create-react-app/issues/3132#issuecomment-330610440)
- [bbhlondon/cra-append-sw
](https://github.com/bbhlondon/cra-append-sw)


## License

[MIT License][mit-license]


[mit-license]: https://liuderchi.mit-license.org/ "mit-license"

[sw-precache-configs]: https://github.com/GoogleChromeLabs/sw-precache#options-parameter "sw-precache-config"


[sw-config]: https://github.com/liuderchi/sw-precache-cra/blob/examples/examples/cra-contributors/sw-config.js
[examples-sw-precache-cra]: https://github.com/liuderchi/sw-precache-cra/commits/examples

---
_Source: https://npm.io/package/sw-precache-cra · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
