-
-
Notifications
You must be signed in to change notification settings - Fork 321
/
index.js
89 lines (76 loc) · 3.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { useEffect } from 'react'
import Head from 'next/head'
const Index = () => {
// This hook only run once in browser after the component is rendered for the first time.
// It has same effect as the old componentDidMount lifecycle callback.
useEffect(() => {
if (typeof window !== 'undefined' && 'serviceWorker' in navigator && window.workbox !== undefined) {
const wb = window.workbox
// add event listeners to handle any of PWA lifecycle event
// https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-window.Workbox#events
wb.addEventListener('installed', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
wb.addEventListener('controlling', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
wb.addEventListener('activated', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
// A common UX pattern for progressive web apps is to show a banner when a service worker has updated and waiting to install.
// NOTE: MUST set skipWaiting to false in next.config.js pwa object
// https://developers.google.com/web/tools/workbox/guides/advanced-recipes#offer_a_page_reload_for_users
const promptNewVersionAvailable = event => {
// `event.wasWaitingBeforeRegister` will be false if this is the first time the updated service worker is waiting.
// When `event.wasWaitingBeforeRegister` is true, a previously updated service worker is still waiting.
// You may want to customize the UI prompt accordingly.
if (confirm('A newer version of this web app is available, reload to update?')) {
wb.addEventListener('controlling', event => {
window.location.reload()
})
// Send a message to the waiting service worker, instructing it to activate.
wb.messageSkipWaiting()
} else {
console.log(
'User rejected to reload the web app, keep using old version. New version will be automatically load when user open the app next time.'
)
}
}
wb.addEventListener('waiting', promptNewVersionAvailable)
// ISSUE - this is not working as expected, why?
// I could only make message event listenser work when I manually add this listenser into sw.js file
wb.addEventListener('message', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
/*
wb.addEventListener('redundant', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
wb.addEventListener('externalinstalled', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
wb.addEventListener('externalactivated', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
*/
// never forget to call register as auto register is turned off in next.config.js
wb.register()
}
}, [])
return (
<>
<Head>
<title>next-pwa example</title>
</Head>
<h1>Next.js + PWA = AWESOME!!</h1>
</>
)
}
export default Index