首页 > 解决方案 > 在 Asp.net Web.Config 中使用“Service-Worker-Allowed”(PWA 顶部不需要的 URL)

问题描述

我有以下网站托管两个 PWA。

www.xy.com/z

我有PWA A托管在

www.xy.com/z/a

PWA B托管在

www.xy.com/z/b

我像这样从原点为PWA A注册了一个服务人员。www.xy.com/z/a

           if ('serviceWorker' in navigator) {
                navigator.serviceWorker.register('/z/a/sw.js', {scope: '/'}).then(function (registration) {
                     console.log('Service worker registration succeeded:', registration);
                }).catch(function (error) {
                    console.log('Service worker registration failed:', error);
                });
            } else {
                console.log('Service workers are not supported.');
            }

我以同样的方式从源头注册PWA B的服务人员www.xy.com/z/b

             if ('serviceWorker' in navigator) {
                    navigator.serviceWorker.register('/z/b/sw.js', {scope: '/'}).then(function (registration) {
                         console.log('Service worker registration succeeded:', registration);
                    }).catch(function (error) {
                        console.log('Service worker registration failed:', error);
                    });
                } else {
                    console.log('Service workers are not supported.');
                }

至于注册服务人员并重定向到属性 start_urls,一切似乎都很好。两个 PWA 都会自动提示 A2HS 并安装到设备的应用程序部分。问题是PWA AB都访问了www.xy.com/z/. 当他们的服务人员提供超出范围的内容时,www.xy.com/z/ 我会在 PWA 顶部显示一个不需要的浏览器 URL。

我希望能够通过将Service-Worker-Allowed添加到我的 web.config 来解决此问题,以表明这些服务工作者可以提供超出其范围的内容。

 <location path="z/a/sw.js">
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Service-Worker-Allowed" value="/" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</location>

<location path="z/b/sw.js">
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Service-Worker-Allowed" value="/" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</location>

我是否应该能够消除使用重定向到超出范围的内容(同一域的)时显示的浏览器 url,Service-Worker-Allowed还是我误解了这一点?

编辑:

Service-Worker-Allowed尽管我们知道这是因为超出范围的服务人员突然注册,但我已经使用 chrome 工具验证了它已添加到标题中。

安卓

IOS

标签: javascriptasp.netiisservice-workerprogressive-web-apps

解决方案


推荐阅读