简体   繁体   English

服务工作者使用spring MVC获取事件handleR

[英]Service worker fetch event handleR with spring MVC

I am trying a make a spring MVC app with offline features.Everything is working fine except the fetch handler. 我正在尝试使用离线功能制作一个Spring MVC应用程序。除了获取处理程序之外,一切正常。

Problem: 问题:

I am able to successfully register my service worker but when it comes to fetch handler it ain't working. 我能够成功注册我的服务工作者但是当涉及到获取处理程序时它无法正常工作。

Things I tried: 我试过的事情:

Used debugger above and below the fetch handler.I found out that it is never being called. 在fetch handler的上方和下方使用了调试器。我发现它永远不会被调用。 I am unable to find out the reason behind this strange behaviour. 我无法找出这种奇怪行为背后的原因。

self.addEventListener('install', function(event) {
    console.log('The service worker is being installed.');  
    event.waitUntil(precache());
});

self.addEventListener('fetch', function(event) {
    alert("Hi");  
});

"a service worker registration ties the provided script URL to a scope, which is subsequently used for navigation matching." “服务工作者注册将提供的脚本URL与范围联系起来,后者随后用于导航匹配。”

What you're probably doing is navigator.serviceWorker.register( '{SOME_RESOURCE_PATH}/service-worker.js') or even setting scope: './' . 您可能正在做的是navigator.serviceWorker.register( '{SOME_RESOURCE_PATH}/service-worker.js')或甚至设置scope: './' The problem with this is that is it will make your fetch only trigger on https://foo.bar/{SOME_RESOURCE_PATH}/ . 这样做的问题是它只会在https://foo.bar/{SOME_RESOURCE_PATH}/触发你的fetch触发器。 Meaning that you will need to set the sw onto the root of your site to remove the resource path being needed. 这意味着您需要将sw设置到站点的根目录以删除所需的资源路径。 What you'll need to do is change the config to put the sw on the root of the site. 您需要做的是更改配置以将sw放在站点的根目录上。

ex. 恩。 of what you can do in the mvc-config.xml 您可以在mvc-config.xml

<mvc:resources mapping="/**" location="classpath:/PATH_TO_SW/"/>

then registering the sw would look like 然后注册sw看起来像

navigator.serviceWorker.register( '/service-worker.js')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM