如何从远程来源重新安装服务工作者.js文件

发布于 2025-02-01 18:28:01 字数 898 浏览 3 评论 0原文

我在Shopify平台中有一个应用程序,并且我正在使用脚本标签将功能性添加到Merchant的店面,在脚本标签文件中,我正在尝试注册服务工作者,但我得到了流浪的错误:

未能(在承诺中)domexception:未能注册服务工作人员:提供的Scripturl的起源('https://fea3-5-219-49-49-49-37.ngrok.io')与当前的原点不匹配( 'https://pouyas-store.myshopify.com')

这是我在嵌入式javascript文件中的代码:

const baseURL = "https://fea3-5-219-49-37.ngrok.io";
(function (){
      navigator.serviceWorker.register(baseURL+"/static/shopify_app/ServiceWorker.js")
          .then((reg) => {
            console.log("reg",reg);
              if (Notification.permission === "granted") {
                  getSubscription(reg);
              } else if (Notification.permission === "blocked") {
              } else {

                  $("#GiveAccess").show();
                  $("#PromptForAccessBtn").click(() => requestNotificationAccess(reg));
              }
          });
})()

I have a app in shopify platform and I'm using Script Tag to add functionlity to merchant's storefront, in script tag file I'm trying to register service worker but I got the fallowing error:

Uncaught (in promise) DOMException: Failed to register a ServiceWorker: The origin of the provided scriptURL ('https://fea3-5-219-49-37.ngrok.io') does not match the current origin ('https://pouyas-store.myshopify.com')

here is my code in embeded javascript file:

const baseURL = "https://fea3-5-219-49-37.ngrok.io";
(function (){
      navigator.serviceWorker.register(baseURL+"/static/shopify_app/ServiceWorker.js")
          .then((reg) => {
            console.log("reg",reg);
              if (Notification.permission === "granted") {
                  getSubscription(reg);
              } else if (Notification.permission === "blocked") {
              } else {

                  $("#GiveAccess").show();
                  $("#PromptForAccessBtn").click(() => requestNotificationAccess(reg));
              }
          });
})()

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

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

发布评论

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

评论(2

开始看清了 2025-02-08 18:28:01

是的,Shopify不要通过第三方访问来访问本地根文件。

因此,您验证脚本无法访问根文件夹中的SW.JS文件。

这项解决方法可以在Shopify中使用App-Proxy。您需要将SW.JS文件放在任何域的在线服务器中。

并将该服务器添加到代理-URL中。

像在前缀中一样,您可以选择应用程序,在子路上您可以给任何名称。

现在,在navigate.register.serviceworker()参数中提供相对路径,例如/apps/your-subpath/sw.js。

现在,每当此功能工作时,它都会访问此URL(您的shopify-domain/apps/source)。

由于代理-URL上方的代理 - 将其重定向到代理-URL。

请记住,在代理URL中不要仅提供服务器的入口点。

就像导航函数访问/apps/source/sw.js时一样。

它用代理-URL剥离sopifu domain/apps/sub-path
而且新的URL变成了代理-URL/SW.JS,

但响应域仍然是您的购物域。很好。

因为注册服务工作者导航。注册函数比较
请求和响应域。

因此,如果您的文件来自另一个域,则不会注册服务工作者。

希望它有帮助。

Yes shopify don't give access to local root files through third party access.

So you're validate script can't access the sw.js file in root folder.

The workaround for this to use app-proxy in the shopify. You need to place your sw.js file in the online server of any domain.

And add that server into your proxy-url.

Like in the prefix you can choose app and in the subpath you can give any name.

Now in the navigate.register.serviceworker() parameter give the relative path like /apps/your-subpath/sw.js.

Now whenever this function works it access this url (your-shopify-domain/apps/source).

Because of proxy-url above url redirects to the proxy-url.

Keep in mind in the proxy url don't give the file path just the entry point of your server.

As when navigate function access the /apps/source/sw.js.

It strips the sopifu-domain/apps/sub-path with your proxy - url
and the new url becomes proxy-url/sw.js

But the response domain remain your shopify-domain. It's good.

Because to register service worker navigate.register function compares the
requested and response domain.

So if your file comes from another domain It won't register the service worker.

Hope it helps.

笔落惊风雨 2025-02-08 18:28:01

您的顶级服务工作者脚本URL需要与调用navigator.serviceworker.register()的客户页面相同的来源。此脚本URL用于确定服务工作者注册的默认范围。因为相同 - 原始服务工作者脚本的位置 scope 服务工作者注册的内容,通常您需要从顶级路径上为您的服务工作者提供服务,例如/local-sw.js

在服务工作脚本的内部,您可以使用importScripts()在任何任意交叉脚本脚本URL中包含逻辑,只要使用CORS提供脚本即可。

因此允许以下内容:

// Inside your web app's client JavaScript:

navigator.serviceWorker.register('/local-sw.js');
// Inside /local-sw.js:

importScripts('https://cross-origin.com/sw-logic.sw');
// Inside https://cross-origin.com/sw-logic.sw:

// Your service worker logic goes here.

Your top-level service worker script URL needs to be served from the same origin as the client page that calls navigator.serviceWorker.register(). This script URL is used to determine the default scope of the service worker registration. Because the location of the same-origin service worker script impacts the scope of the service worker registration, you'll normally want to serve your service worker from a top-level path, like /local-sw.js.

Inside of your service worker script, you can use importScripts() to include logic from any arbitrary cross-origin script URL, as long as the script is served using CORS.

So the following is allowed:

// Inside your web app's client JavaScript:

navigator.serviceWorker.register('/local-sw.js');
// Inside /local-sw.js:

importScripts('https://cross-origin.com/sw-logic.sw');
// Inside https://cross-origin.com/sw-logic.sw:

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