javascript - 在 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 A和B都访问了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 工具验证了它已添加到标题中。
安卓
- 三星互联网(9.2.10.15)按预期工作。不显示网址。
- Chrome (74.0.3729.157) 浏览器 url 显示在 PWA 中
IOS
- Safari (IOS 12.3.1) url 在 PWA 中显示,带有“完成”按钮。
- 奇怪的是,如果我在 Safari 中注释掉服务人员并添加到主屏幕,我就没有得到任何 url。