|
2 | 2 | title: Offline caching with Service Worker Precache
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -<div> |
6 |
| -{% include 'outdated.html' %} |
7 |
| -</div> |
| 5 | +<!-- toc --> |
8 | 6 |
|
9 |
| -To provide a better experience in offline and spotty network situations, App |
10 |
| -Toolbox uses a service worker to provide offline caching of critical resources. A |
11 |
| -service worker is a script associated with a specific web site that acts as a |
| 7 | +A service worker is a script associated with a specific web site that acts as a |
12 | 8 | client-side proxy for network requests. The service worker can intercept network
|
13 | 9 | requests, access the browser's cache, and serve requests out of the cache
|
14 | 10 | instead of accessing the network.
|
15 | 11 |
|
16 |
| -The first time someone opens the site, the browser installs the site's service |
17 |
| -worker, and the service worker ensures that the site's critical resources are |
18 |
| -cached. On subsequent visits, the service worker can load resources directly |
19 |
| -from the cache. If the user is completely offline, the service worker can still |
20 |
| -load the site, and display cached data or an offline message, as appropriate. |
| 12 | +A service worker can improve your app's performance and allow it to work |
| 13 | +offline. |
21 | 14 |
|
22 |
| -Service worker works well with an _app shell_ strategy, where the app's main UI |
23 |
| -views and logic (the app shell) are cached so that they can be served from the |
24 |
| -cache. |
| 15 | +The first time someone opens your site, the browser installs the site's service |
| 16 | +worker, and the service worker caches your app's critical resources. On |
| 17 | +subsequent visits, the service worker can load resources directly from the |
| 18 | +cache. If the user is completely offline, the service worker can still load the |
| 19 | +site, and display cached data or an offline message, as appropriate. |
25 | 20 |
|
26 | 21 | The Polymer Starter Kit template uses the Service Worker Precache (`sw-precache`) module for offline
|
27 | 22 | support. This module takes a list of files to cache and generates a service
|
28 | 23 | worker at build time, so you don't need to write your own service worker code.
|
| 24 | +Service worker works well with an _app shell_ strategy, where the app's main |
| 25 | +views and logic (the app shell) are cached so that they can be served without |
| 26 | +accessing the network. |
29 | 27 |
|
30 |
| -For background, gotchas and debugging tips on service workers, see [Introduction to Service |
31 |
| -Worker](https://developers.google.com/web/fundamentals/primers/service-worker/) on Web Fundamentals. |
| 28 | +Polymer CLI uses the [Service Worker Precache |
| 29 | +(`sw-precache`)](https://github.com/GoogleChromeLabs/sw-precache) library. All |
| 30 | +of the Polymer [build |
| 31 | +presets](/{{{polymer_version_dir}}}/docs/tools/polymer-json#presets), and the |
| 32 | +default build, include a service worker. |
32 | 33 |
|
33 |
| -## Prerequisites |
34 |
| - |
35 |
| -To work with service worker, your application **must** be served over HTTPS. However, you can |
36 |
| -test service worker on your local system without a SSL certificate, because `localhost` is |
37 |
| -considered a secure origin. |
38 |
| - |
39 |
| -## Add a service worker |
40 |
| - |
41 |
| -Support for Service Worker Precache is built into the [Polymer CLI](/{{{polymer_version_dir}}}/docs/tools/polymer-cli), |
42 |
| -so a service worker script is automatically generated when you build your app. |
| 34 | +For background, gotchas and debugging tips on service workers, see |
| 35 | +[Introduction to Service Workers](https://developers.google.com/web/fundamentals/primers/service-worker/) on Web Fundamentals. |
43 | 36 |
|
44 |
| -However, to use the service worker, you need to add code to register it: |
| 37 | +## Prerequisites |
45 | 38 |
|
46 |
| -```js |
47 |
| -// Register service worker if supported. |
48 |
| -if ('serviceWorker' in navigator) { |
49 |
| - navigator.serviceWorker.register('/service-worker.js'); |
| 39 | +To work with a service worker, your application **must** be served over HTTPS. |
| 40 | +However, you can test your service worker locally without a SSL certificate, |
| 41 | +because `localhost` is considered a secure origin. |
| 42 | + |
| 43 | +For information on browser support for service worker, see [Is Service Worker |
| 44 | +Ready](https://jakearchibald.github.io/isserviceworkerready/). |
| 45 | + |
| 46 | +## Add a service worker to your build |
| 47 | + |
| 48 | +To add a service worker to your build: |
| 49 | + |
| 50 | +1. [Configure polymer.json](#configpolymerjson). |
| 51 | +2. [Add code to your entrypoint to register a service worker](#register). |
| 52 | +3. Run `polymer build`. |
| 53 | + |
| 54 | +A service worker is added to your build. |
| 55 | + |
| 56 | +### Configure polymer.json |
| 57 | + |
| 58 | +To configure polymer.json, set the `"entrypoint"` and `"shell"` properties. The |
| 59 | +service worker will automatically precache these resources. |
| 60 | + |
| 61 | +Polymer CLI generates a service worker by default. You can switch service worker |
| 62 | +generation on or off with the `"addServiceWorker"` option in your build |
| 63 | +configuration. |
| 64 | + |
| 65 | +Example polymer.json configuration {.caption} |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "entrypoint": "index.html", |
| 70 | + "shell": "src/my-app.js", |
| 71 | + "builds": [ |
| 72 | + { |
| 73 | + "name": "esm-unbundled", |
| 74 | + "browserCapabilities": [ |
| 75 | + "es2015", |
| 76 | + "modules" |
| 77 | + ], |
| 78 | + "js": { |
| 79 | + "minify": true |
| 80 | + }, |
| 81 | + "css": { |
| 82 | + "minify": true |
| 83 | + }, |
| 84 | + "html": { |
| 85 | + "minify": true |
| 86 | + }, |
| 87 | + "bundle": false, |
| 88 | + "addServiceWorker": true |
| 89 | + } |
| 90 | + ] |
50 | 91 | }
|
| 92 | +``` |
| 93 | + |
| 94 | +### Register your service worker |
| 95 | + |
| 96 | +To use the service worker, add code to your app's entrypoint to register it: |
| 97 | + |
| 98 | +Register a service worker {.caption} |
| 99 | + |
| 100 | +```html |
| 101 | +<head> |
| 102 | + <script> |
| 103 | + // Feature detect for service worker capability in the browser |
| 104 | + if ('serviceWorker' in navigator) { |
| 105 | + // Delay registering until page load |
| 106 | + window.addEventListener('load', function() { |
| 107 | + navigator.serviceWorker.register('service-worker.js'); |
| 108 | + }); |
| 109 | + } |
| 110 | + </script> |
| 111 | +</head> |
51 | 112 | ```
|
52 | 113 |
|
53 |
| -Registering a service worker doesn't speed up the first load of your site, so you can delay |
54 |
| -registering it until after your app has loaded. |
55 |
| - |
56 |
| -## Configuring the service worker |
57 |
| - |
58 |
| -You can specify any Service Worker Precache options by passing an options file |
59 |
| -to the build command: |
| 114 | +Registering a service worker doesn't speed up the first load of your site. You |
| 115 | +can delay registering it until after your app has loaded. |
60 | 116 |
|
61 |
| -<code>polymer build --sw-precache-config <var>config-file</var>.json</code> |
| 117 | +## Customize your service worker |
62 | 118 |
|
63 |
| -The config file is a JavaScript file that exports a set of configuration options supported by |
64 |
| -Service Worker Precache. See [Options parameter](https://github.com/GoogleChrome/sw-precache#options-parameter) |
65 |
| -in the `sw-precache` README for more information. |
| 119 | +To customize your service worker, create a configuration file called |
| 120 | +`sw-precache-config.js` in your top-level project folder. |
66 | 121 |
|
67 |
| -If you identify resources using the `--entrypoint`, `--shell` and `--fragment`, arguments, those |
68 |
| -files are added in to the `staticFileGlobs` parameter to ensure that they're cached. |
| 122 | +`sw-precache-config.js` exports a set of configuration options supported by |
| 123 | +Service Worker Precache. See [Options |
| 124 | +parameter](https://github.com/GoogleChrome/sw-precache#options-parameter) in the |
| 125 | +`sw-precache` README for more information. |
69 | 126 |
|
70 |
| -If you're writing a single-page app and you want it to work completely offline, you probably want |
71 |
| -to specify a _fallback_ document, to be served when the requested URL is not in the cache. For a |
72 |
| -single—page app, this is typically the same as the entrypoint. Configure fallback using the |
73 |
| -[navigateFallback](https://github.com/GoogleChrome/sw-precache#navigatefallback-string) and |
| 127 | +If you're writing a single-page app and you want it to work completely offline, |
| 128 | +specify a _fallback_ document to be served when the requested URL is not in the |
| 129 | +cache. For a single—page app, this is typically the same as the entrypoint. |
| 130 | +Configure fallback using the |
| 131 | +[navigateFallback](https://github.com/GoogleChrome/sw-precache#navigatefallback-string) |
| 132 | +and |
74 | 133 | [navigateFallbackWhitelist](https://github.com/GoogleChrome/sw-precache#navigatefallbackwhitelist-arrayregexp)
|
75 | 134 | parameters.
|
76 | 135 |
|
77 |
| -The following config file sets up some common options, including falling back to the `/index.html` |
78 |
| -file when offline. |
| 136 | +Example sw-precache-config.js {.caption} |
79 | 137 |
|
80 | 138 | ```js
|
81 | 139 | module.exports = {
|
82 | 140 | staticFileGlobs: [
|
83 | 141 | '/index.html',
|
84 | 142 | '/manifest.json',
|
85 |
| - '/bower_components/webcomponentsjs/webcomponents-lite.js', |
| 143 | + '/node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js', |
86 | 144 | '/images/*'
|
87 | 145 | ],
|
88 | 146 | navigateFallback: '/index.html',
|
89 |
| - navigateFallbackWhitelist: [/^(?!.*\.html$|\/data\/).*/] |
| 147 | + navigateFallbackWhitelist: [/^(?!.*\.js$|\/data\/).*/] |
90 | 148 | }
|
91 | 149 | ```
|
92 | 150 |
|
93 |
| -Only paths that match the whitelist fall back to `/index.html`. In this case, the whitelist includes |
94 |
| -all files _except_ those that end in `.html` (for HTML imports) and ones with `/data/` in the path |
95 |
| -(for dynamically-loaded data). |
| 151 | +Only paths that match the whitelist fall back to `/index.html`. In this case, |
| 152 | +the whitelist includes all files _except_ those that end in `.js` (for |
| 153 | +JavaScript imports) and ones with `/data/` in the path (for dynamically-loaded |
| 154 | +data). |
96 | 155 |
|
97 | 156 | ## More resources
|
98 | 157 |
|
99 |
| -The library supports a number of other features, including runtime caching of |
100 |
| -your app's dynamic content. |
| 158 | +The `sw-precache` library supports a number of other features, including runtime |
| 159 | +caching of your app's dynamic content. |
101 | 160 |
|
102 |
| -For more information on the library, see [Service Worker Precache Getting |
| 161 | +For more information on the library, see [Service Worker Precache: Getting |
103 | 162 | Started](https://github.com/GoogleChrome/sw-precache/blob/master/GettingStarted.md).
|
104 | 163 |
|
105 | 164 | For background on service workers, see [Introduction to Service
|
106 |
| -Worker](https://developers.google.com/web/fundamentals/primers/service-worker/) on Web Fundamentals. |
| 165 | +Worker](https://developers.google.com/web/fundamentals/primers/service-worker/) |
| 166 | +on Web Fundamentals. |
0 commit comments