UNPKG

8.55 kBMarkdownView Raw
1# gatsby-plugin-offline
2
3Adds drop-in support for making a Gatsby site work offline and more resistant to
4bad network connections. It uses [Workbox Build](https://developers.google.com/web/tools/workbox/modules/workbox-build)
5to create a service worker for the site and loads the service worker into the client.
6
7If you're using this plugin with `gatsby-plugin-manifest` (recommended) this
8plugin should be listed _after_ that plugin so the manifest file can be included
9in the service worker.
10
11## Install
12
13`npm install gatsby-plugin-offline`
14
15## How to use
16
17```javascript
18// In your gatsby-config.js
19plugins: [`gatsby-plugin-offline`]
20```
21
22## Available options
23
24In `gatsby-plugin-offline` 3.x, the following options are available:
25
26- `precachePages` lets you specify pages whose resources should be precached by the service worker, using an array of globs. For example:
27
28 ```javascript:title=gatsby-config.js
29 plugins: [
30 {
31 resolve: `gatsby-plugin-offline`,
32 options: {
33 precachePages: [`/about-us/`, `/projects/*`],
34 },
35 },
36 ]
37 ```
38
39 Note: while essential resources of specified pages will be precached, such as JavaScript and CSS, non-essential resources such as fonts and images will not be included. Instead, these will be cached at runtime when a user visits a given page that includes these resources.
40
41- `appendScript` lets you specify a file to be appended at the end of the generated service worker (`sw.js`). For example:
42
43 ```javascript:title=gatsby-config.js
44 plugins: [
45 {
46 resolve: `gatsby-plugin-offline`,
47 options: {
48 appendScript: require.resolve(`src/custom-sw-code.js`),
49 },
50 },
51 ]
52 ```
53
54 <br />
55
56 ```javascript:title=src/custom-sw-code.js
57 // show a notification after 15 seconds (the notification
58 // permission must be granted first)
59 setTimeout(() => {
60 self.registration.showNotification("Hello, world!")
61 }, 15000)
62
63 // register a custom navigation route
64 const customRoute = new workbox.routing.NavigationRoute(({ event }) => {
65 // ...
66 })
67 workbox.routing.registerRoute(customRoute)
68 ```
69
70- `debug` specifies whether Workbox should show debugging output in the browser console at runtime. When undefined, defaults to showing debug messages on `localhost` only.
71
72- `workboxConfig` allows you to override the default Workbox options - see [Overriding Workbox configuration](#overriding-workbox-configuration). For example:
73
74 ```javascript:title=gatsby-config.js
75 plugins: [
76 {
77 resolve: `gatsby-plugin-offline`,
78 options: {
79 workboxConfig: {
80 importWorkboxFrom: `cdn`,
81 },
82 },
83 },
84 ]
85 ```
86
87## Upgrading from 2.x
88
89To upgrade from 2.x to 3.x, move any existing options into the `workboxConfig` option. If you haven't specified any options, you have nothing to do.
90
91For example, here is a 2.x config:
92
93```javascript
94plugins: [
95 {
96 resolve: `gatsby-plugin-offline`,
97 options: {
98 importWorkboxFrom: `cdn`,
99 },
100 },
101]
102```
103
104Here is the equivalent 3.x config:
105
106```javascript
107plugins: [
108 {
109 resolve: `gatsby-plugin-offline`,
110 options: {
111 workboxConfig: {
112 importWorkboxFrom: `cdn`,
113 },
114 },
115 },
116]
117```
118
119In version 3, Workbox is also upgraded to version 4 so you may need to update your `workboxConfig` if any of those changes apply to you. Please see the [docs on Google Developers](https://developers.google.com/web/tools/workbox/guides/migrations/migrate-from-v3) for more information.
120
121## Overriding Workbox configuration
122
123When adding this plugin to your `gatsby-config.js`, you can use the option `workboxConfig` to override the default Workbox config. To see the full list of options, see [this article on Google Developers](https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config).
124
125The default `workboxConfig` is as follows. Note that some of these options are configured automatically, e.g. `globPatterns`. If you're not sure about what all of these options mean, it's best to leave them as-is - otherwise, you may end up causing errors on your site, causing old files to be remain cached, or even breaking offline support.
126
127```javascript
128const options = {
129 importWorkboxFrom: `local`,
130 globDirectory: rootDir,
131 globPatterns,
132 modifyURLPrefix: {
133 // If `pathPrefix` is configured by user, we should replace
134 // the default prefix with `pathPrefix`.
135 "/": `${pathPrefix}/`,
136 },
137 cacheId: `gatsby-plugin-offline`,
138 // Don't cache-bust JS or CSS files, and anything in the static directory,
139 // since these files have unique URLs and their contents will never change
140 dontCacheBustURLsMatching: /(\.js$|\.css$|static\/)/,
141 runtimeCaching: [
142 {
143 // Use cacheFirst since these don't need to be revalidated (same RegExp
144 // and same reason as above)
145 urlPattern: /(\.js$|\.css$|static\/)/,
146 handler: `CacheFirst`,
147 },
148 {
149 // page-data.json files, static query results and app-data.json
150 // are not content hashed
151 urlPattern: /^https?:.*\/page-data\/.*\.json/,
152 handler: `StaleWhileRevalidate`,
153 },
154 {
155 // Add runtime caching of various other page resources
156 urlPattern: /^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/,
157 handler: `StaleWhileRevalidate`,
158 },
159 {
160 // Google Fonts CSS (doesn't end in .css so we need to specify it)
161 urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/,
162 handler: `StaleWhileRevalidate`,
163 },
164 ],
165 skipWaiting: true,
166 clientsClaim: true,
167}
168```
169
170## Remove
171
172If you want to remove `gatsby-plugin-offline` from your site at a later point,
173substitute it with [`gatsby-plugin-remove-serviceworker`](https://www.npmjs.com/package/gatsby-plugin-remove-serviceworker)
174to safely remove the service worker. First, install the new package:
175
176```shell
177npm install gatsby-plugin-remove-serviceworker
178npm uninstall gatsby-plugin-offline
179```
180
181Then, update your `gatsby-config.js`:
182
183```diff:title=gatsby-config.js
184 plugins: [
185- `gatsby-plugin-offline`,
186+ `gatsby-plugin-remove-serviceworker`,
187 ]
188```
189
190This will ensure that the worker is properly unregistered, instead of leaving an
191outdated version registered in users' browsers.
192
193## Notes
194
195### Empty View Source and SEO
196
197Gatsby offers great SEO capabilities and that is no different with `gatsby-plugin-offline`. However, you shouldn't think that Gatsby doesn't serve HTML tags anymore when looking at your source code in the browser (with `Right click` => `View source`). `View source` doesn't represent the actual HTML data since `gatsby-plugin-offline` registers and loads a service worker that will cache and handle this differently. Your site is loaded from the service worker, not from its actual source (check your `Network` tab in the DevTools for that).
198
199To see the HTML data that crawlers will receive, run this in your terminal:
200
201**on Windows (using powershell):**
202
203```shell
204Invoke-WebRequest https://www.yourdomain.tld | Select -ExpandProperty Content
205```
206
207**on Mac OS/Linux:**
208
209```shell
210curl https://www.yourdomain.tld
211```
212
213Alternatively you can have a look at the `/public/index.html` file in your project folder.
214
215### App shell and server logs
216
217Server logs (like from [Netlify analytics](https://www.netlify.com/products/analytics/)) may show a large number of pageviews to a route like `/offline-plugin-app-shell-fallback/index.html`, this is a result of `gatsby-plugin-offline` adding an [app shell](https://developers.google.com/web/fundamentals/architecture/app-shell) to the page. The app shell is a minimal amount of user interface that can be cached offline for reliable performance loading on repeat visits. The shell can be loaded from the cache, and the content of the site loaded into the shell by the service worker.
218
219### Using with gatsby-plugin-manifest
220
221If using this plugin with `gatsby-plugin-manifest` you may find that your icons are not cached.
222In order to solve this, update your `gatsby-config.js` as follows:
223
224```js
225// gatsby-config.js
226{
227 resolve: 'gatsby-plugin-manifest',
228 options: {
229 icon: 'icon.svg',
230 cache_busting_mode: 'none'
231 }
232},
233{
234 resolve: 'gatsby-plugin-offline',
235 options: {
236 workboxConfig: {
237 globPatterns: ['**/icon-path*']
238 }
239 }
240}
241```
242
243Updating `cache_busting_mode` is necessary. Otherwise, workbox will break while attempting to find the cached URLs.
244Adding the `globPatterns` makes sure that the offline plugin will cache everything.
245Note that you have to prefix your icon with `icon-path` or whatever you may call it