js Service worker注册时设置非根目录无法拦截请求

小春日和
  • 39

如题,例如访问/aaa/bbbscope设置为/是可以正常拦截到的,如果设置为/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)
回复
阅读 465
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏