我一直试图让一个非常基本的PWA与api一起工作。
虽然PWA 在我的笔记本电脑上按预期工作(包括在添加到主屏幕时脱机工作), 在移动设备上看不到安装/添加到主屏幕提示/横幅。
该应用在移动设备上也无法脱机工作
这是服务工作者代码:
const cacheName = 'news';
const staticAssets = [
'./',
'./app.js',
'./styles.css',
'./fallback.json',
];
self.addEventListener('install', async function () {
const cache = await caches.open(cacheName);
cache.addAll(staticAssets);
});
self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', event => {
const request = event.request;
const url = new URL(request.url);
if (url.origin === location.origin) {
event.respondWith(cacheFirst(request));
} else {
event.respondWith(networkFirst(request));
}
});
async function cacheFirst(request) {
const cachedResponse = await caches.match(request);
return cachedResponse || fetch(request);
}
async function networkFirst(request) {
const dynamicCache = await caches.open('news-dynamic');
try {
const networkResponse = await fetch(request);
dynamicCache.put(request, networkResponse.clone());
return networkResponse;
} catch (err) {
const cachedResponse = await dynamicCache.match(request);
return cachedResponse || await caches.match('./fallback.json');
}
}
这是manifest.json文件:
{
"name": "News",
"short_name": "News",
"theme_color": "#2196f3",
"background_color": "#2196f3",
"display": "standalone",
"Scope": "/",
"start_url": ".",
"icons": [
{
"src": "images/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
{
"src": "images/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "images/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png"
},
{
"src": "images/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "images/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png"
},
{
"src": "images/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "images/icons/icon-384x384.png",
"sizes": "384x384",
"type": "image/png"
},
{
"src": "images/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"splash_pages": null
}
有关如何让它在移动设备上按预期工作的想法吗?