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

SW: working in multiple tabs and updating #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
/*.log
.idea/
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,56 @@ serviceWorker.register({
});
```

### 3. Check for service worker update and activation service worker in other tabs

In your main js file (`App.js`), you you can add specific behavior:

```
componentDidMount() {
if ('serviceWorker' in navigator) {
// Reload page after message from worker (if sw activated in another tab)
navigator.serviceWorker.addEventListener('message', function(event){
if(event.data === 'reloadPage') {
window.location.reload();
}
});

// Check for sw updates every second. You can change this interval.
// Don't forget to clear the interval in the componentWillUnmount
this.intervalSW = setInterval(() => navigator.serviceWorker.getRegistrations()
.then(registrationsArray => {
// Check for update service worker
if (registrationsArray && registrationsArray[0]) {
registrationsArray[0].update();
} else {
console.error("serviceWorker: getRegistrations empty!")
}
}), 1000)
} else {
console.error("Browser does not support serviceWorker!")
}
}
```

### Check if the service worker is already waiting.

```
navigator.serviceWorker
.register(swUrl)
.then(registration => {
if(registration.waiting && registration.active) {
// The page has been loaded when there's already a waiting and active SW.
// This would happen if skipWaiting() isn't being called, and there are
// still old tabs open.
if(config && config.onUpdate) {
config.onUpdate(registration);
}
}
// ...
})
// ...
```

## Options

### textContent
Expand Down
13 changes: 12 additions & 1 deletion inject-workbox-refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ const fs = require("fs-extra");

const codeToAppend = `
self.addEventListener('message', function handleSkipWaiting(event) {
if (event.data === 'skipWaiting') { self.skipWaiting(); }
if (event.data === 'skipWaiting') {
clients.matchAll({includeUncontrolled: true}).then(function(clients) {
// Loop over all available clients
clients.forEach(function(client) {
// No need to update the tab that sent the data
if (client.id !== event.source.id ) {
client.postMessage('reloadPage')
}
})
})
self.skipWaiting();
}
});
`;

Expand Down