Skip to content

Commit

Permalink
feat: rewrite workbox (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 4, 2019
1 parent 0b6a8d7 commit 9e49896
Show file tree
Hide file tree
Showing 17 changed files with 627 additions and 872 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ jobs:
build:
working_directory: /usr/src/app
docker:
- image: banian/node
- image: node:alpine
steps:
# Checkout repository
- checkout

# Restore cache
- restore_cache:
key: yarn-{{ checksum "yarn.lock" }}
key: yarn-alpine-{{ checksum "yarn.lock" }}

# Install dependencies
- run:
Expand Down
96 changes: 85 additions & 11 deletions docs/modules/workbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

Workbox is a collection of JavaScript libraries for Progressive Web Apps.
([Learn more](https://developers.google.com/web/tools/workbox)). This module adds full offline support using workbox.
Workbox module is only enabled on *production* builds.

You can pass options to `workbox` section in `nuxt.config.js` to override defaults.

Expand All @@ -17,25 +16,99 @@ workbox: {

### Options

**dev** - Use dev build for workbox service worker lib.
### `workboxVersion`

**swURL** - If for any reason you need to register another service worker (OneSignal, etc) you can use this option.
(String) Version of workbox. Default value is latest tested version which will be updated by updating this package.

**importScripts** (Array) - Additional scripts to be imported in service worker script. (Relative to `/`. Can be placed in `assets/` directory)
### `workboxURL`

**offlineAssets** (Array) - List of assets to precache, in case you need extra files precached other than the ones automatically processed (`_nuxt/*` etc) or you want to ensure assets used by your `offlinePage`. (Example: `['/offline.png']`)
(String) URL for workbox CDN. Default is JSDelivr - See [Workbox CDN](https://github.com/nuxt-community/workbox-cdn). You can also use `config.modulePathPrefix` if you need to rewrite module paths.

**offlinePage** (String) - Enables routing all offline requests to the specified path. (Example: `/offline.html`)
### `config`

**Please note** that enabling `offlinePage` will disable `/.*` caching and will route all offline requests to the specified path. Nuxt assets are still precached as are the ones specified in `offlineAssets`.
(Object) Options to be passed to workbox before using it's modules. By default `debug` field will be set to `false` for production builds.

**cachingExtensions** (String) - Loads and inserts the contents of the specified file path into the service worker script, below autogenerated calls to `workbox.precaching.*`. You may add as many extra calls as you want to this file.
### `importScripts`

**routingExtensions** (String) - Loads and inserts the contents of the specified file path into the service worker script, below autogenerated calls to `workbox.routing.*`. You may add as many extra calls as you want to this file.
(Array) Additional scripts to be imported in service worker script. (Relative to `/`. Can be placed in `assets/` directory)

**config** (Object) - Sets custom configurations to workbox using `workbox.setConfig()`. For example, set `{ modulePathPrefix: '/third_party/workbox/' }` to use local workbox files instead of google CDN.
### `offline`

(Boolean) Cache all routes. Enabled by default.


### `offlinePage`

(String) Enables routing all offline requests to the specified path. (Example: `/offline.html`)

**Please note** that enabling `offlinePage` will disable `/.*` caching (`offline` option) and will route all offline requests to the specified path.

### `offlineAssets`

(Array) List of assets to precache, in case you need extra files precached other than on the fly caching (`_nuxt/*` etc) or you want to ensure assets used by your `offlinePage`. (Example: `['/offline.png']`)

### `cachingExtensions`

(String) Loads and inserts the contents of the specified file path into the service worker script, below autogenerated calls to `workbox.precaching.*`. You may add as many extra calls as you want to this file.

### `routingExtensions`

(String) Loads and inserts the contents of the specified file path into the service worker script, below autogenerated calls to `workbox.routing.*`. You may add as many extra calls as you want to this file.

### `clientsClaim`

(Boolean) Start controlling any existing clients as soon as it activates. Enabled by default.

### `skipWaiting`

(Boolean) Skip over the SW waiting lifecycle stage. Enabled by default.

### `runtimeCaching`

(Array) Custom routes to cache with specific strategy. Useful for caching requests to other origins. Example:

```js
[
{
urlPattern: 'https://google.com/.*',
handler: 'cacheFirst',
method: 'GET'
}
]
```

### `cacheAssets`

(Boolean) Cache requests to the `/_nuxt/*` with **cacheFirst** strategy on the fly. Enabled by default.

**NOTE:** This is considered safe because all assets should be correctly hashed there.

**NOTE:** Nuxt smart links trigger chunk downloads for next pages when user sees a link to a non-cached page.

### `autoRegister`

(Boolean) Automatically register `/sw.js` on page load. Enabled by default.

### `routerBase`

(String) Defaults to router base.

### `publicPath`

(String) Defaults to `/_nuxt`.

### `swTemplate`

(String) You can use this to customize generated `sw.js`. Not recommanded to be directly used.

### `swURL`

(String) If you need to register another service worker (OneSignal, etc) you can use this option.

### `swScope`

(String) Defaults to `routerBase`.

For list of all available options see [here](https://developers.google.com/web/tools/workbox/modules/workbox-build)

### Adding custom runtimeCaching items (For CDN)

Expand All @@ -60,6 +133,7 @@ workbox: {
```

#### Adding custom cache [StrategyOption](https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.strategies)

```js
workbox: {
runtimeCaching: [
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"name": "@nuxtjs/pwa",
"version": "0.0.0",
"private": true,
"contributors": [
Expand All @@ -8,7 +7,7 @@
"Jonas Galvez <jonasgalvez@gmail.com>"
],
"scripts": {
"test": "npm run lint && jest",
"test": "yarn lint && jest",
"lint": "eslint --ext .js packages",
"release": "lerna publish --conventional-commits"
},
Expand Down
Loading

0 comments on commit 9e49896

Please sign in to comment.