位置或范围在Web推送服务工作者中重要吗?

发布于 2025-02-08 04:41:29 字数 1843 浏览 0 评论 0 原文

我有一个Django Web应用程序,其中包含一个用于处理推送通知的服务工作者。一切都很好。在许多文章,教程和文档中,写着服务工作人员的位置和范围很重要,并确保将其放在域的根源上。

参见例如本文

服务工作者文件必须在您网站的根目录中注册。

和:

我花了15个小时来学习这一事实。你们很幸运!

因此,当我第一次在Django创建我的服务工作者时,我必须确保正确设置范围。我创建了一个将服务工作者的JS代码作为模板的视图,以便我可以设置 servication-worker-wasered header:

class ServiceWorkerView(TemplateView):
    template_name = 'web_push/web_push_service_worker.js'

    def get(self, request, *args, **kwargs):
        response = super().get(request, *args, **kwargs)
        response.headers['Content-Type'] = 'application/javascript'
        response.headers['Service-Worker-Allowed'] = '/'
        return response

该模板包含推送处理代码:

self.addEventListener('push', function(event) {
    ...
    self.registration.showNotification(...)
    ...
})

如前所述:正常工作。

但是现在我想知道:为什么范围很重要?我刚刚改变了事情,并将服务工作者的代码移至Django静态文件夹中的标准位置。在 sw noreferrer“> sw regigistration code 我删除了范围范围属性属性。现在很简单:

navigator.serviceWorker
    .register('/static/account/sw.js')
    .then(function(registration) {
        ...
    })

因此,此SW的新范围现在为/static/account/

我已经对Chrome和Firefox,Localhost进行了一些测试,并使用 ngrok ,这似乎也很好。

因此,我的问题是:范围 服务工作者的位置,处理推动通知,真的重要吗?我想念什么吗?是否存在推动SW的范围很重要的情况?

I have a Django web app that contains a service worker designed for handling push notifications. Everything is working beautifully. In the many articles, tutorials and documentation it is written that the location and scope of service workers is important and to make sure to put it at the root of the domain.

See for example this article, that states:

Service worker file must registered at the root directory of your website.

And:

I’ve spend 15 hours to learn that fact. You guys are lucky !

So when I first created my service worker in Django, I had to make sure to set the scope correctly. I created a View that served the JS code of the service worker as a template, just so that I could set the Service-Worker-Allowed header:

class ServiceWorkerView(TemplateView):
    template_name = 'web_push/web_push_service_worker.js'

    def get(self, request, *args, **kwargs):
        response = super().get(request, *args, **kwargs)
        response.headers['Content-Type'] = 'application/javascript'
        response.headers['Service-Worker-Allowed'] = '/'
        return response

That template contained the push handling code:

self.addEventListener('push', function(event) {
    ...
    self.registration.showNotification(...)
    ...
})

As mentioned: this works fine.

But now I wonder: why does the scope matter? I just changed things around and moved the code of the service worker to a standard location inside Django's static folders. In the SW registration code I removed the scope attribute. It is now simply:

navigator.serviceWorker
    .register('/static/account/sw.js')
    .then(function(registration) {
        ...
    })

So the new scope of this SW is now /static/account/.

I have done some testing on Chrome and Firefox, from localhost and using ngrok, and this seems to work fine as well.

So my question: does the scope and location of a service worker, that handles push notifications, really matter? Am I missing something? Are there cases where the scope for a push SW is important?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百思不得你姐 2025-02-15 04:41:29

当您拥有带有 fetch 处理程序的服务工作者时,使范围和位置非常重要的考虑因素不适用于 push 仅适用于服务工人。

如果您只使用默认范围并注册一个在您的原点中某个地方的服务工作脚本( navigator.serviceworker.register.register('/static/static/account/account/sw.js')在您的示例中),对于推送事件应该很好。

我相信 push 仅使用合成范围(配置为使用其消息后端)您的任何实际URL,并意外地控制了您不会期望的客户。不过,这对您要做的事情太过杀了。

The same considerations that make scope and location very important when you have a service worker with a fetch handler do not apply to push-only service workers.

If you just use the default scope and register a service worker script that exists somewhere in your origin (navigator.serviceWorker.register('/static/account/sw.js') in your example), that should be fine for push events.

I believe that OneSignal and Firebase Cloud Messaging for the web both have client libraries that will register a push-only service worker for you (configured to use their messaging backend) with a synthetic scope, which is presumably guaranteed not to interfere with any of your actual URLs and accidentally take control of clients that you don't expect. That's overkill for what you're trying to do, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文