如何正确地从spring boot服务service-worker.js?

时间:2017-07-25 23:53:37

标签: javascript spring-mvc spring-boot service-worker progressive-web-apps

浏览器抱怨该文件位于重定向后面:

DOMException: Failed to register a ServiceWorker: 
The script resource is behind a redirect, which is disallowed.

到目前为止我尝试了什么:

Service worker on `static/service-worker.js`
Service worker on @GetMapping("/service-worker.js") @ResponseBody

在这两种情况下,我都可以从/service-worker.js中的浏览器中读取文件,但注册失败并出现相同的异常。

编辑:(这是我遇到问题的注册)

<script>
if ('serviceWorker' in navigator) {
    console.log("Will the service worker register?");
    navigator.serviceWorker.register('service-worker.js')
        .then(function(reg){
            console.log("Yes, it did.");
        }).catch(function(err) {
        console.log("No it didn't. This happened: ", err)
    });
}
</script>

1 个答案:

答案 0 :(得分:1)

我尝试注册的脚本位于/store/All。注册navigator.serviceWorker.register('service-worker.js')正在调用/store/service-worker.js,根据我在控制器中编程的规则重定向navigator.serviceWorker.register('/service-worker.js')

正确的调用应该是:/这确保我们在root中调用service-worker.js,而不是在与当前页面相同的路径中。

tl; dr我错过了Classic Cloud Service