Skip to content

Commit

Permalink
docs(gatsby-plugin-offline): Show how to handle a SW update (gatsbyjs…
Browse files Browse the repository at this point in the history
…#10417)

Closes gatsbyjs#9087

I also deleted `/docs/add-offline-support/` since it just redirects to `/docs/add-offline-support-with-a-service-worker/` anyway, and hasn't been updated in a while.
  • Loading branch information
valin4tor authored and gpetrioli committed Jan 22, 2019
1 parent 9910c16 commit e680985
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 48 deletions.
37 changes: 27 additions & 10 deletions docs/docs/add-offline-support-with-a-service-worker.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ If you've run an [audit with Lighthouse](/docs/audit-with-lighthouse/), you may
1. You can [add a manifest file](/docs/add-a-manifest-file/). Ensure that the manifest plugin is listed _before_ the offline plugin so that the offline plugin can cache the created `manifest.webmanifest`.
2. You can also add offline support, since another requirement for a website to qualify as a PWA is the use of a [service worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API). [Gatsby's offline plugin](/packages/gatsby-plugin-offline/) makes a Gatsby site work offline--and makes it more resistant to bad network conditions--by creating a service worker for your site.

### What is a service worker
### What is a service worker?

A service worker is a script that your browser runs in the background, separate from a web page, opening the door to features that don't need a web page or user interaction. They increase your site availability in spotty connections, and are essential to making a nice user experience.

Expand All @@ -27,22 +27,39 @@ Add this plugin to your `gatsby-config.js`

```javascript:title=gatsby-config.js
{
plugins: [
{
resolve: `gatsby-plugin-manifest`,
options: {
...
}
},
'gatsby-plugin-offline'
]
plugins: [
{
resolve: `gatsby-plugin-manifest`,
options: {
...
}
},
'gatsby-plugin-offline'
]
}
```

That's all you need to add offline support to your Gatsby site.

Note: Service worker registers only in production builds (`gatsby build`).

### Displaying a message when a service worker updates

To display a custom message once your service worker finds an update, you can use the [`onServiceWorkerUpdateFound`](/docs/browser-apis/#onServiceWorkerUpdateFound) browser API in your `gatsby-browser.js` file. The following code will display a prompt asking the user whether they would like to refresh the page when an update is found:

```javascript:title=gatsby-browser.js
exports.onServiceWorkerUpdateFound = () => {
const answer = window.prompt(
`This application has been updated. ` +
`Reload to display the latest version?`
)

if (answer === true) {
window.reload()
}
}
```

## References

- [Service Workers: an Introduction](https://developers.google.com/web/fundamentals/primers/service-workers/)
Expand Down
38 changes: 0 additions & 38 deletions docs/docs/add-offline-support.md

This file was deleted.

0 comments on commit e680985

Please sign in to comment.