如题,例如访问/aaa/bbb
,scope
设置为/
是可以正常拦截到的,如果设置为/aaa
这样就会拦截不到。代码:
<!-- index.html -->
<!--
目录结构:
> index.html
> worker.js
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
navigator.serviceWorker.register('/worker.js', { scope: '/aaa' })
.then(reg => {
if(reg.installing) {
console.log('Service worker installing')
} else if(reg.waiting) {
console.log('Service worker installed')
} else if(reg.active) {
console.log('Service worker active')
}
})
.catch(e => {
console.log(e)
})
const xhr = new XMLHttpRequest()
xhr.open('get', '/aaa/bbb/aaa')
xhr.send()
xhr.onload = () => {
console.log(xhr.response)
}
</script>
</head>
<body>
</body>
</html>
// worker.js
self.addEventListener('fetch', console.log)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…