Skip to content

Commit f607943

Browse files
authored
Merge pull request #330 from NekR/v4.9.0
V4.9.0
2 parents 0845c39 + 5eec617 commit f607943

92 files changed

Lines changed: 6837 additions & 480 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.appveyor.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ environment:
33
matrix:
44
# node.js
55
- nodejs_version: "4"
6-
- nodejs_version: "5"
76
- nodejs_version: "6"
7+
- nodejs_version: "8"
8+
- nodejs_version: "9"
89

910
# Install scripts. (runs after repo cloning)
1011
install:

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules/
22
__output/
3+
/__*
34
.DS_Store
4-
.idea
5+
.idea
6+
npm-debug.log

.travis.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
sudo: false
22
language: node_js
33
node_js:
4+
- "4"
45
- "6"
5-
- "5"
6-
- "4"
6+
- "8"
7+
- "9"
8+
env:
9+
- TEST_METHOD=ci_fixtures
10+
matrix:
11+
include:
12+
- node_js: "8"
13+
env: TEST_METHOD=ci_all
14+
script: npm run test:"$TEST_METHOD"

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
<a href="#sponsors"><img src="https://opencollective.com/offline-plugin/sponsors/badge.svg" alt="sponsors" /></a>
88
<a href="https://www.npmjs.com/package/offline-plugin"><img src="https://img.shields.io/npm/v/offline-plugin.svg?maxAge=3600&v4" alt="npm"></a>
99
<a href="https://www.npmjs.com/package/offline-plugin"><img src="https://img.shields.io/npm/dm/offline-plugin.svg?maxAge=3600" alt="npm"></a>
10-
<a href="https://gitter.im/NekR/offline-plugin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"><img src="https://badges.gitter.im/NekR/offline-plugin.svg" alt="Join the chat at https://gitter.im/NekR/offline-plugin"></a>
1110
</div>
1211
<br>
1312

1413
This plugin is intended to provide an offline experience for **webpack** projects. It uses **ServiceWorker**, and **AppCache** as a fallback under the hood. Simply include this plugin in your ``webpack.config``, and the accompanying runtime in your client script, and your project will become offline ready by caching all (or some) of the webpack output assets.
1514

1615
<div align="center">
17-
<strong>Demo:<br><a href="https://offline-plugin.now.sh/"> Progressive Web App built with <code>offline-plugin</code></a></strong><br>
16+
<strong>Demo:<br><a href="https://offline-plugin.now.sh/"> Progressive Web App built with <code>offline-plugin</code></a></strong><br>
1817
<div>(<a href="https://github.com/NekR/offline-plugin-pwa"><i>source code</i></a>)</div>
1918
</div>
2019

docs/app-shell.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# `appShell: string`
2+
3+
> Default: `null`.
4+
> **Example:** `'/'`
5+
6+
When making a Singe Page Application, it's common to use [AppShell](https://medium.com/google-developers/instant-loading-web-apps-with-an-application-shell-architecture-7c0c2f10c73) model for it.
7+
8+
To make `offline-plugin` redirect all unknown navigation requests to a specific cache, specify `appShell` option, e.g. `appShell: '/'`.
9+
10+
### SSR
11+
12+
When using Server Side Rendering with AppShell model, make sure that you do not cache any server rendered data with it. Easiest way would be to make a route which will be serving the HTML file without any server rendered data in it (e.g. ready for client side rendering) and cache that route. Example: `appShell: '/app-shell.html'`
13+
14+
### Advanced
15+
16+
Previously, to achieve the same effect, `ServiceWorker.navigateFallbackURL` and `AppCache.FALLBACK` option had to be used. `ServiceWorker.navigateFallbackURL` is now deprecated and shouldn't be used at all. Instead, `appShell` should be used.
17+
18+
`appShell` is baked by `cacheMaps` option for `ServiceWorker` and `AppCache.FALLBACK` option for `AppCache`.

docs/navigation-preload.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## `navigationPreload: boolean | Object | ':auto:'`
2+
> _Default:_ `':auto:'`
3+
4+
[Navigation preload](https://developers.google.com/web/updates/2017/02/navigation-preload) is a ServiceWorker's feature which provides a way to make a request to the website even before ServiceWorker or a page is initialized. This can be useful for data fetching to speedup loading of the application.
5+
6+
### Usage
7+
8+
In `offline-plugin`, _Navigation preload_ behaves differently for `cache-first` and `network-first` response strategies.
9+
10+
For `network-first` navigation preload is enabled by default and allows to fetch navigation pages ahead of time, even before ServiceWorker was initialized. To disabled it set `ServiceWorker.navigationPreload` option to `false`.
11+
12+
For `cache-first` navigation preload has to be enabled manually and also handled on the server side. To enabled navigation preload two functions has to be specified:
13+
14+
```js
15+
ServiceWorker: {
16+
navigationPreload: {
17+
map: (url) => {
18+
if (url.pathname === '/') {
19+
return '/api/feed';
20+
}
21+
22+
var post = url.pathname.match(/^\/post\/(\d+)$/);
23+
24+
if (post) {
25+
return '/api/post/' + post[1];
26+
}
27+
},
28+
test: (url) => {
29+
if (url.pathname.indexOf('/api/') === 0) {
30+
return true;
31+
}
32+
}
33+
},
34+
}
35+
```
36+
37+
* `map` function is used to map navigation preload request to other requests, e.g. API requests.
38+
* `test` function is used to test for possible consumers of navigation preload mapping
39+
40+
In previous example `map` function maps navigation preload of `/` to `/api/feed`. Then when a request to `/api/feed` happens, `test` function is used to determine that such request might be preloaded with _navigation preload_ (it checks if `pathname` of the request starts with `/api/`).
41+
42+
#### Server Side
43+
44+
When a navigation preload request happens, it contains `Service-Worker-Navigation-Preload: true` header. Server side should use this header to detect the preload and send different content to such request.
45+
46+
Express.js example:
47+
48+
```js
49+
function serveIndex(req, res) {
50+
if (req.headers['service-worker-navigation-preload']) {
51+
res.set({
52+
'Cache-Control': 'no-cache',
53+
'Vary': 'Service-Worker-Navigation-Preload'
54+
});
55+
56+
fetchFeedData(req).then((data) => {
57+
res.send(data);
58+
});
59+
60+
return;
61+
}
62+
63+
res.sendFile(path.join(WWW_FOLDER, 'index.html'), {
64+
cacheControl: false,
65+
acceptRanges: false,
66+
headers: {
67+
'Cache-Control': 'no-cache',
68+
'Vary': 'Service-Worker-Navigation-Preload'
69+
}
70+
});
71+
}
72+
```
73+
74+
Make sure to set **`'Vary': 'Service-Worker-Navigation-Preload'`** header if you're planning on caching those responses.
75+
76+
For more details on Navigation Preload see [this article](https://developers.google.com/web/updates/2017/02/navigation-preload).

docs/options.md

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
## Options
22

33
**All options are optional and `offline-plugin` can be used without specifying them.**
4-
_Also see list of default options [here](https://github.com/NekR/offline-plugin/blob/master/src/index.js#L17)._
4+
_Also see list of default options [here](../src/default-options.js)._
5+
6+
#### `appShell: string`
7+
8+
[See `appShell` option documentation](app-shell.md)
9+
10+
> Default: `null`.
11+
> **Example:** `'/index.html'`
512
613
#### `caches: 'all' | Object`
714

@@ -16,37 +23,45 @@ Allows you to define what to cache and how.
1623
1724
#### `publicPath: string`
1825

19-
Same as `webpack`'s `output.publicPath` option. Useful to specify or override `publicPath` specifically for `offline-plugin`. When not specified, `webpack`'s `output.publicPath` value is used. When `webpack`'s `output.publicPath` value isn't specified, relative paths are used (see `relativePaths` option).
26+
Similar to `webpack`'s `output.publicPath` option. Useful to specify or override `publicPath` specifically for `offline-plugin`. When not specified, `webpack`'s `output.publicPath` value is used. When `publicPath` value isn't spsecified at all (either by this option or by `webpack`'s `output.publicPath` option), relative paths are used (see `relativePaths` option).
2027

2128
> __Examples:__
2229
`publicPath: '/project/'`
2330
`publicPath: 'https://example.com/project'`
2431

2532
#### `responseStrategy: 'cache-first' | 'network-first'`
2633
Response strategy. Whether to use a cache or network first for responses.
34+
35+
With `'cache-first'` all request are sent to consult the cache first and if the cache is empty, request is sent to the network.
36+
37+
With `'network-first'` all request are sent to the network first and if network request fails consult the cache as a fallback.
2738
> Default: `'cache-first'`.
2839
2940
#### `updateStrategy: 'changed' | 'all'`
3041
Cache update strategy. [More details about `updateStrategy`](update-strategies.md)
42+
**Please, do not change this option unless you're sure you know what you're doing.**
3143
> Default: `'changed'`.
3244
3345
#### `externals: Array<string>`
34-
Allows you to specify _external_ assets (assets which aren't generated by webpack) that should be included in the cache. If you don't change the `caches` configuration option then it should be enough to simply add assets to this (`externals`) option. For other details and more advanced use cases see [`caches` docs](caches.md).
46+
47+
Allows you to specify additional (external to the build process) URLs to be cached.
3548

3649
> Default: `null`
37-
> **Example value:** `['fonts/roboto.woff']`
50+
> **Example:** `['/static/file-on-the-server.json', 'https://fonts.googleapis.com/css?family=Roboto']`
3851
3952
#### `excludes: Array<string | globs_pattern>`
4053
Excludes matched assets from being added to the [caches](https://github.com/NekR/offline-plugin#caches-all--object). Exclusion is performed before [_rewrites_](https://github.com/NekR/offline-plugin/blob/master/docs/options.md#rewrites-function--object) happens.
4154
[Learn more about assets _rewrite_](rewrites.md)
4255

43-
> Default: `['**/.*', '**/*.map']`
44-
> _Excludes all files which start with `.` or end with `.map`_
56+
> Default: `['**/.*', '**/*.map', '**/*.gz']`
57+
> _Excludes all files which start with `.` or end with `.map` or `.gz`_
4558
4659
#### `relativePaths: boolean`
4760
When set to `true`, all the asset paths generated in the cache will be relative to the `ServiceWorker` file or the `AppCache` folder location respectively.
48-
`publicPath` option is ignored when this is **explicitly** set to `true`.
49-
> **Default:** `true`
61+
62+
This option is ignored when `publicPath` is set.
63+
`publicPath` option is ignored when this option is set **explicitly** to `true`.
64+
> Default: `true`
5065
5166
#### `version: string | (plugin: OfflinePlugin) => void`
5267
Version of the cache. Can be a function, which is useful in _watch-mode_ when you need to apply dynamic value.
@@ -58,7 +73,10 @@ Version of the cache. Can be a function, which is useful in _watch-mode_ when yo
5873
5974
#### `rewrites: Function | Object`
6075

61-
Rewrite function or rewrite map (`Object`). Useful when assets are served in a different way from the client perspective, e.g. usually `index.html` is served as `/`.
76+
Provides a way to rewrite final representation of the file on the server.
77+
Useful when assets are served in a different way from the client perspective, e.g. usually `/index.html` is served as `/`.
78+
79+
Can be either a function or an `Object`.
6280

6381
[See more about `rewrites` option and default function](rewrites.md)
6482

@@ -68,7 +86,7 @@ See [documentation of `cacheMaps`](cache-maps.md) for syntax and usage examples
6886

6987
#### `autoUpdate: true | number`
7088

71-
Enables automatic updates of ServiceWorker and AppCache. If set to `true`, it uses default interval of _1 hour_. Set a `number` value to have provide custom update interval.
89+
Enables automatic updates of ServiceWorker and AppCache. If set to `true`, it uses default interval of _1 hour_. Set a `number` value to provide custom update interval.
7290

7391
_**Note:** Please note that if user has multiple opened tabs of your website then update may happen more often because each opened tab will have its own interval for updates._
7492

@@ -80,66 +98,72 @@ _**Note:** Please note that if user has multiple opened tabs of your website the
8098

8199
Settings for the `ServiceWorker` cache. Use `null` or `false` to disable `ServiceWorker` generation.
82100

83-
* `output`: `string`. Relative (from the _webpack_'s config `output.path`) output path for emitted script.
101+
* **`output`**: `string`. Relative (from the _webpack_'s config `output.path`) output path for emitted script.
84102
_Default:_ `'sw.js'`
85103

86-
* `entry`: `string`. Relative or absolute path to the file which will be used as the `ServiceWorker` entry/bootstrapping. Useful to implement additional features or handlers for Service Worker events such as `push`, `sync`, etc.
104+
* **`entry`**: `string`. Relative or absolute path to the file which will be used as the `ServiceWorker` entry/bootstrapping. Useful to implement additional features or handlers for Service Worker events such as `push`, `sync`, etc.
87105
_Default:_ _empty file_
88106

89-
* `scope`: `string`. Reflects [ServiceWorker.register](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register)'s `scope` option.
107+
* **`scope`**: `string`. Reflects [ServiceWorker.register](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register)'s `scope` option.
90108
_Default:_ `null`
91109

92-
* `cacheName`: `string`. **This option is very dangerous. Touching it you must realize that you should **not** change it after you go production. Changing it may corrupt the cache and leave old caches on users' devices. This option is useful when you need to run more than one project on _the same domain_.
110+
* **`cacheName`**: `string`. **This option is very dangerous.** This option should **not** be changed at all after you deploy `ServiceWorker` to production. Changing it may corrupt the cache and leave old caches on users' devices.
111+
112+
This option is useful when you need to run more than one project on _the same domain_.
93113
_Default:_ _`''`_ (empty string)
94114
_Example:_ `'my-project'`
95115

96-
* `navigateFallbackURL`: `string`. The URL that should be returned from the cache when a requested navigation URL isn't available on the cache or network. Similar to the `AppCache.FALLBACK` option.
97-
_Example:_ `navigateFallbackURL: '/'`
98-
99-
* `navigateFallbackForRedirects`: `boolean`. If this flag is false `navigateFallbackURL` will not be used for 3xx responses. (By default it will be used for all non 2xx navigate responses).
100-
_Example:_ `navigateFallbackForRedirects: false`
101-
_Default:_ `true`
102-
103-
* `events`: `boolean`. Enables runtime events for the ServiceWorker. For supported events see `Runtime`'s `install()` options.
116+
* **`events`**: `boolean`. Enables runtime events for the ServiceWorker. For supported events see [`Runtime`](runtime.md)'s `install()` options.
104117
_Default:_ `false`
105118

106-
* `publicPath`: `string`. Provides a way to override `ServiceWorker`'s script file location on the server. Should be an exact path to the generated `ServiceWorker` file.
119+
* **`publicPath`**: `string`. Provides a way to override `ServiceWorker`'s script file location on the server. Should be an exact path to the generated `ServiceWorker` file.
107120
_Default:_ `null`
108-
_Example:_ `'my/new/path/sw.js'`
121+
_Example:_ `'/my/new/path/sw.js'`
122+
123+
* **`navigationPreload`**: `boolean | Object | ':auto:'`. [See `ServiceWorker.navigationPreload` option documentation](navigation-preload.md)
124+
125+
_Default:_ `':auto:'`
109126

110-
* `prefetchRequest`: `Object`. Provides a way to specify [request init options](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request) for pre-fetch requests (pre-cache requests on `install` event). Allowed options: `credentials`, `headers`, `mode`, `cache`.
127+
* **`prefetchRequest`**: `Object`. Provides a way to specify [request init options](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request) for pre-fetch requests (pre-cache requests on `install` event). Allowed options: `credentials`, `headers`, `mode`, `cache`.
111128
_Default:_ `{ credentials: 'omit', mode: 'cors' }`
112-
_Example:_ `{ credentials: 'same-origin' }`
129+
_Example:_ `{ credentials: 'include' }`
113130

114-
* `minify`: `boolean`. If set to `true` or `false`, the `ServiceWorker`'s output will be minified or not accordingly. If set to something else, the `ServiceWorker` output will be minified **if** you are using `webpack.optimize.UglifyJsPlugin` in your configuration.
131+
* **`minify`**: `boolean`. If set to `true` or `false`, the `ServiceWorker`'s output will be minified or not accordingly. If set to something else, the `ServiceWorker` output will be minified **if** you are using `webpack.optimize.UglifyJsPlugin` in your configuration.
115132
_Default:_ `null`
116133

134+
* **[Deprecated]** `navigateFallbackURL`: `string`. The URL that should be returned from the cache when a requested navigation URL isn't available on the cache or network. Similar to the `AppCache.FALLBACK` option.
135+
_Example:_ `navigateFallbackURL: '/'`
136+
137+
* **[Deprecated]** `navigateFallbackForRedirects`: `boolean`. If this flag is false `navigateFallbackURL` will not be used for 3xx responses. (By default it will be used for all non 2xx navigate responses).
138+
_Example:_ `navigateFallbackForRedirects: false`
139+
_Default:_ `true`
140+
117141
#### `AppCache: Object | null | false`
118142

119143
Settings for the `AppCache` cache. Use `null` or `false` to disable `AppCache` generation.
120144

121145
> _**Warning**_: Officially the AppCache feature [has been deprecated](https://developer.mozilla.org/en-US/docs/Web/HTML/Using_the_application_cache) in favour of Service Workers. However, Service Workers are still being implemented across all browsers (you can track progress [here](https://jakearchibald.github.io/isserviceworkerready/)) so AppCache is unlikely to suddenly disappear. Therefore please don't be afraid to use the AppCache feature if you have a need to provide offline support to browsers that do not support Service Workers, but it is good to be aware of this fact and make a deliberate decision on your configuration.
122146
123-
* `directory`: `string`. Relative (from the _webpack_'s config `output.path`) output directly path for the `AppCache` emitted files.
147+
* **`directory`**: `string`. Relative (from the _webpack_'s config `output.path`) output directly path for the `AppCache` emitted files.
124148
_Default:_ `'appcache/'`
125149

126-
* `NETWORK`: `string`. Reflects `AppCache`'s `NETWORK` section.
150+
* **`NETWORK`**: `string`. Reflects `AppCache`'s `NETWORK` section.
127151
_Default:_ `'*'`
128152

129-
* `FALLBACK`: `Object`. Reflects `AppCache`'s `FALLBACK` section. Useful for single page applications making use of HTML5 routing or for displaying custom _Offline page_.
153+
* **`FALLBACK`**: `Object`. Reflects `AppCache`'s `FALLBACK` section. Useful for single page applications making use of HTML5 routing or for displaying custom _Offline page_.
130154
_Example 1:_ `{ '/blog': '/' }` will map all requests starting with `/blog` to the domain roboto when request fails.
131155
_Example 2:_ `{ '/': '/offline-page.html' }` will return contents of `/offline-page.html` for any failed request.
132156
_Default:_ `null`
133157

134-
* `events`: `boolean`. Enables runtime events for AppCache. For supported events see `Runtime`'s `install()` options.
158+
* **`events`**: `boolean`. Enables runtime events for AppCache. For supported events see [`Runtime`](runtime.md)'s `install()` options.
135159
_Default:_ `false`
136160

137-
* `publicPath`: `string`. Provides a way to override `AppCache`'s folder location on the server. Should be exact path to the generated `AppCache` folder.
161+
* **`publicPath`**: `string`. Provides a way to override `AppCache`'s folder location on the server. Should be exact path to the generated `AppCache` folder.
138162
_Default:_ `null`
139163
_Example:_ `'my/new/path/appcache'`
140164

141-
* `disableInstall` :`boolean`. Disable the automatic installation of the `AppCache` when calling to `runtime.install()`. Useful when you to specify `<html manifest="...">` attribute manually (to cache every page user visits).
165+
* **`disableInstall`**: `boolean`. Disable the automatic installation of the `AppCache` when calling `runtime.install()`. Useful when you want to specify `<html manifest="...">` attribute manually (to cache every page user visits).
142166
_Default:_ `false`
143167

144-
* `includeCrossOrigin` :`boolean`. Outputs cross-origin URLs into `AppCache`'s manifest file. **Cross-origin URLs aren't supported in `AppCache` when used on HTTPS.**
168+
* **`includeCrossOrigin`**: `boolean`. Outputs cross-origin URLs into `AppCache`'s manifest file. **Cross-origin URLs aren't supported in `AppCache` when used on HTTPS.**
145169
_Default:_ `false`

0 commit comments

Comments
 (0)