-->

从移动浏览器的缓存不加载PWA离线模式(PWA offline mode not loading f

2019-10-29 06:52发布

我写了一个简单的PWA( 当前版本的基础上) 本教程由Vaadin 。 它工作正常,在Chrome测试,也是在离线模式。

通过使用它在移动设备上,可能出现的问题:

  • 保存PWA,一旦启动后,它会运行良好。
  • 然后关闭,开启飞行模式并重新启动后PWA,我收到一条系统消息,说我有没有互联网连接 - >没问题,我可以忽略
  • 无视后,如我所料它的应用程序不会加载静态资产,但显示一个空白页,称该浏览器无法加载的页面,因为我没有互联网连接。

我认为这是,什么是PWA好? 为什么它不会加载静态资产? 我觉得我的服务工人就好了:

const staticAssets = [
    './',
    './styles.css',
    './app.js',
    './images',
    './fallback.json',
    './images/system/offline.png'
]



self.addEventListener('install', async event => {
    const cache = await caches.open('static-assets');
    cache.addAll(staticAssets);
});

self.addEventListener('fetch', event => {
    const req = event.request;
    const url = new URL(req.url);

    if(url.origin === location.origin){
        event.respondWith(cacheFirst(req));
    }else{
        event.respondWith(networkFirst(req));
    }

});


async function cacheFirst(req){
    const cachedResponse = await caches.match(req);
    return cachedResponse || fetch(req);
}

async function networkFirst(req){
    const cache = await caches.open('dynamic-content');
    try{
        const res = await fetch(req);
        cache.put(req, res.clone());
        return res;
    }catch(err){
        const cachedResponse = await cache.match(req);
        return cachedResponse || caches.match('./fallback.json');
    }
}

我很高兴与大家分享更多的代码,如果你认为这个问题是别的地方!

Answer 1:

问题是服务工作者中:

我忘了服务工作者文件添加到静态资产。

:通过阅读这个问题的答案找到了解决办法https://stackoverflow.com/a/44482764/7350000 。



文章来源: PWA offline mode not loading from cache on mobile browsers