我收到此警告:
未检测到匹配的服务程序。您可能需要重新加载页面, 或检查当前页面的服务工作者是否也可以控制 清单清单中URL的开头。
我的服务人员正在工作的东西。
我什至尝试使用另外两个URL,两个都使用HTTP。
现在我将向您展示代码,我正在使用:
site.webmanifest
{
...
"start_url": "/",
"display": "standalone"
...
}
要创建我的服务工作者,我使用webpack插件:结合了Laravel Mix的WorkboxPlugin:
webpack.mix.js
mix.webpackConfig({
plugins: [
build.jigsaw,
build.browserSync(),
build.watch(['source/**/*.md', 'source/**/*.php', 'source/**/*.scss', '!source/**/_tmp/*']),
new WorkboxPlugin.GenerateSW({
// these options encourage the ServiceWorkers to get in there fast
// and not allow any straggling "old" SWs to hang around
clientsClaim: true,
skipWaiting: true,
}),
],
output: {
publicPath: '/assets/build/', // fixes the output bug
},
});
它将创建 service-worker.js:
importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js");
importScripts(
"/assets/build/precache-manifest.cb67a9edd02323d8ea51560852b6cc0d.js"
);
workbox.core.skipWaiting();
workbox.core.clientsClaim();
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
使用 precache-manifest.js
self.__precacheManifest = (self.__precacheManifest || []).concat([
{
"revision": "4e264a665cf5133098ac",
"url": "/assets/build//js/main.js"
},
{
"revision": "4e264a665cf5133098ac",
"url": "/assets/build/css/main.css"
},
{
"url": "/assets/build/images/vendor/leaflet/dist/layers-2x.png?4f0283c6ce28e888000e978e537a6a56"
},
{
"url": "/assets/build/images/vendor/leaflet/dist/layers.png?a6137456ed160d7606981aa57c559898"
},
{
"url": "/assets/build/images/vendor/leaflet/dist/marker-icon.png?2273e3d8ad9264b7daa5bdbf8e6b47f8"
}
]);
该警告该怎么办?
答案 0 :(得分:0)
我找到了解决方案!范围设置不正确。您需要了解,服务人员必须位于根文件夹中。
所以我的改变是:
webpack.mix.js
mix.webpackConfig({
plugins: [
build.jigsaw,
build.browserSync(),
build.watch(['source/**/*.md', 'source/**/*.php', 'source/**/*.scss', '!source/**/_tmp/*']),
new WorkboxPlugin.GenerateSW({
// these options encourage the ServiceWorkers to get in there fast
// and not allow any straggling "old" SWs to hang around
clientsClaim: true,
skipWaiting: true,
swDest: '../../service-worker.js', //Need to move the service-worker to the root
}),
],
output: {
publicPath: '/assets/build/', // fixes the output bug
},
});
现在我需要使用新路径注册服务工作者
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/service-worker.js')
.then(reg => {
console.log('Registration succeeded. Scope is ' + reg.scope);
})
.catch(registrationError => {
console.log('SW registration failed: ', registrationError);
});
});
}
答案 1 :(得分:0)
最简单的解决方案是,如前面的答案中所述,将服务工作者文件与站点webmanifest文件一起放在根目录中。
但是,如果您喜欢将资产分组在子目录中的人,则可以通过将scope
选项设置为/
并添加Service-Worker-Allowed
HTTP来手动设置范围响应的标头。这样,您的服务工作者文件就不必位于根目录中。
请参阅https://w3c.github.io/ServiceWorker/#service-worker-script-response
中的示例11// Set the scope to an upper path of the script location // Response included "Service-Worker-Allowed : /" navigator.serviceWorker.register("/js/sw.js", { scope: "/" }).then(() => { console.log("Install succeeded as the max allowed scope was overriden to '/'."); });