dev
This commit is contained in:
parent
5fc4768e77
commit
43521cf93f
@ -1,3 +1,4 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
|
<meta charset="UTF-8">
|
||||||
<title>PCDN</title>
|
<title>PCDN</title>
|
||||||
<script src="./main.js"></script>
|
<script src="./main.js"></script>
|
||||||
|
8
main.js
8
main.js
@ -21,9 +21,13 @@
|
|||||||
//pcdn.get('xxx.jpg').then(file => {})
|
//pcdn.get('xxx.jpg').then(file => {})
|
||||||
|
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
|
navigator.serviceWorker.addEventListener('message', event => {
|
||||||
|
console.log('收到消息 Service Worker: ', event.data) // 接收消息
|
||||||
|
event.source.postMessage('Hello from main thread') // 回应 Service Worker
|
||||||
|
})
|
||||||
navigator.serviceWorker.register('/sw.js').then((registration) => {
|
navigator.serviceWorker.register('/sw.js').then((registration) => {
|
||||||
console.log('Service Worker 注册成功: ', registration)
|
console.log('Service Worker 注册成功: ')
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
console.log('Service Worker 注册失败: ', error)
|
console.log('Service Worker 注册失败: ')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
17
sw.js
17
sw.js
@ -1,11 +1,20 @@
|
|||||||
self.addEventListener('install', (event) => {
|
self.addEventListener('install', (event) => {
|
||||||
console.log('Service Worker 安装')
|
console.log('Service Worker 安装')
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener('activate', (event) => {
|
self.addEventListener('activate', (event) => {
|
||||||
console.log('Service Worker 激活')
|
console.log('Service Worker 激活')
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener('fetch', (event) => {
|
self.addEventListener('fetch', (event) => {
|
||||||
console.log('Fetch intercepted for:', event.request.url)
|
console.log('Fetch intercepted for:', event.request.url)
|
||||||
})
|
self.clients.matchAll().then(clients => {
|
||||||
|
clients.forEach(client => {
|
||||||
|
client.postMessage('Hello from Service Worker')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
self.addEventListener('message', event => {
|
||||||
|
console.log('收到主线程消息: ', event.data)
|
||||||
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user