Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Automatic Refresh On Update' added in Exanples #453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/content/en/workbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,36 @@ if (workbox) {
});
}
```

### Refresh On Update (Automatically)

Refresh the page on every update so that user sees new content automatically. Create a file `pwa-update.js` in `plugins` folder and paste the following code.

```js{}[plugins/pwa-update.js]
export default async (context) => {
const workbox = await window.$workbox

if (!workbox) {
console.log("Workbox couldn't be loaded.")
return
}

workbox.addEventListener('installed', (event) => {
if (!event.isUpdate) {
console.log('The PWA is on the latest version.')
return
}

console.log('There is an update for the PWA, reloading...')
window.location.reload()
})
}
```

Connect this plugin in your `nuxt.config.js` file.

```js{}[nuxt.config.js]
plugins: [
{ src: '~/plugins/pwa-update.js', mode: 'client' },
],
```