Skip to content

Commit 662f30f

Browse files
committed
chore(README): Update README
1 parent dc3f0c1 commit 662f30f

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ Import from [unpkg.com](https://unpkg.com/):
1919

2020
## Usage
2121

22+
You should inject refresh dialog script to two place.
23+
24+
- Your Page: `index.html`
25+
- Your Service Worker: `sw.js`
26+
27+
**Add to your page**(index.html):
28+
2229
```html
2330
<!DOCTYPE html>
2431
<html lang="en">
@@ -40,6 +47,21 @@ Import from [unpkg.com](https://unpkg.com/):
4047
</html>
4148
```
4249

50+
**Add to your service worker**(sw.js):
51+
52+
```js
53+
self.addEventListener("message", event => {
54+
if (!event.data) {
55+
return;
56+
}
57+
if (event.data === "skipWaiting") {
58+
self.skipWaiting();
59+
}
60+
});
61+
```
62+
63+
-
64+
4365
### Options
4466

4567
- `message`: Custom message
@@ -93,6 +115,50 @@ For example, you can overwrite it by defining `--sw-updatefound-refresh-dialog--
93115
</style>
94116
```
95117

118+
## FAQ
119+
120+
### Does not refresh when click the banner
121+
122+
Do you forget to inject a script to service worker like `sw.js`?
123+
124+
```js
125+
// sw.js
126+
self.addEventListener("message", event => {
127+
if (!event.data) {
128+
return;
129+
}
130+
if (event.data === "skipWaiting") {
131+
self.skipWaiting();
132+
}
133+
});
134+
```
135+
136+
### `skipWaiting()` integration
137+
138+
If you already did `skipWaiting()` in sw.js, you should remove the code from sw.js
139+
140+
For example, workbox has `skipWaiting` and `clientsClaim`.
141+
These method trigger `statechange` event of the service worker without asking the user to reload manually.
142+
143+
- [Skip Waiting and Clients Claim](https://developers.google.com/web/tools/workbox/modules/workbox-sw#skip_waiting_and_clients_claim)
144+
145+
```diff
146+
// workbox init setting
147+
importScripts("https://storage.googleapis.com/workbox-cdn/releases/3.6.1/workbox-sw.js");
148+
149+
workbox.core.setCacheNameDetails({ prefix: "website-v1" });
150+
workbox.googleAnalytics.initialize();
151+
- workbox.skipWaiting();
152+
- workbox.clientsClaim();
153+
154+
workbox.precaching.suppressWarnings();
155+
workbox.precaching.precacheAndRoute([]);
156+
```
157+
158+
If you have called `skipWaiting` without asking the user to reload manually, this script do refresh page instantly.
159+
160+
- [javascript - Refresh page on controllerchange in service worker - Stack Overflow](https://stackoverflow.com/questions/41891031/refresh-page-on-controllerchange-in-service-worker)
161+
96162
## Resources
97163

98164
- [pwa-update-available/index.html at master · deanhume/pwa-update-available](https://github.com/deanhume/pwa-update-available/blob/master/index.html)

0 commit comments

Comments
 (0)