【发布时间】:2019-04-08 07:28:52
【问题描述】:
我是 pwa 和 service worker 的新手,我已经在 app.xml 中添加了 manifest.json 和 service worker 文件。它还显示了保存到主屏幕的弹出窗口, 但它在离线模式下不起作用。
在控制台错误消息下方离线(使用 chrome 中的开发工具)。
The FetchEvent for "https://www.iservefinancial.com/" resulted in a network error response: the promise was rejected.
Promise.then (async)
(anonymous) @ sw.js:23
sw.js:1 Uncaught (in promise) TypeError: Failed to fetch
我也尝试过更改不同的网址 通过尝试使用像“/public/images/iserve-logo-blue.png”这样的绝对网址 并且还尝试通过提供代码 sn-p 中的直接 url。 它正在本地主机中离线加载资源,但无法在实时服务器上加载。(使用 chrome 开发工具进行离线测试)。
Service Worker 文件的代码
const BASE_URL = "https://www.iservefinancial.com/";
self.addEventListener('install', function (event) {
console.log('sw installed');
event.waitUntil(
caches.open('static').then(function (cache) {
cache.addAll([
BASE_URL,
BASE_URL + 'public/js/jquery-3.2.1.min.js',
BASE_URL + 'public/images/logos/iserve-logo-blue.png',
'https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css',
BASE_URL + 'public/css/fontawesomeak.css',
BASE_URL + 'public/css/questrial-font.css'
]);
})
);
});
self.addEventListener('activate', function () {
console.log('sw Activated');
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(function (res) {
if (res) {
return res
} else {
return fetch(event.request)
}
})
);
});
我已经包含在 PHP 视图文件中的以下代码
<link rel="manifest" href="<?php echo $baseurl; ?>public/scripts/manifest.json">
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js', {
scope: '.'
}).then(function () {
console.log('Service Worker Registered');
});
}
</script>
Manifest.json
{
"name": "iServeFinancial.Com",
"short_name": "iServeFinancial",
"start_url": "https://www.iservefinancial.com/",
"display": "standalone",
"orientation": "portrait",
"background_color": "#fff",
"theme_color": "#1770a3",
"icons": [{
"src": "https://www.iservefinancial.com/public/images/logos/iserve-logo-blue.png",
"sizes": "120x36",
"type": "image/png"
},
{
"src": "https://www.iservefinancial.com/public/images/logos/progressive.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "https://www.iservefinancial.com/public/images/logos/progressive1.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "https://www.iservefinancial.com/public/images/logos/progressive2.png",
"sizes": "256x256",
"type": "image/png"
},
{
"src": "https://www.iservefinancial.com/public/images/logos/progressive2.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
该页面应该可以在线和离线工作。
【问题讨论】:
标签: google-chrome-devtools service-worker progressive-web-apps mobile-application