简体   繁体   English

动态缓存 angular 4 不工作 PWA

[英]Dynamic cache angular 4 not working PWA

I am working on a progressive web app in angular 4 which seems to be working fine in online mode.我正在开发一个 angular 4 的渐进式网络应用程序,它似乎在在线模式下运行良好。 It does work on offline mode as well unless I involve dynamic caching.它也适用于离线模式,除非我涉及动态缓存。

So there is this ngsw-manifest.json in which I have done some configuration:所以有这个ngsw-manifest.json我在其中做了一些配置:

{
    "routing": {
        "index": "/index.html",
        "routes": {
            "/": {
                "match": "exact"
            },
            "/coffee": {
                "match": "prefix"
            }
        }
    },
    "static.ignore": [
        "^\/icons\/.*$"
    ],
    "external": {
        "urls": [
            {
                "url": "https://fonts.googleapis.com/icon?family=Material+Icons"
            },
            {
                "url": "https://fonts.gstatic.com/s/materialicons/v29/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2"
            }
        ]
    },
    "dynamic": {
        "group": [
            {
                "name": "api",
                "urls": {
                    "http://localhost:3000/coffees": {
                        "match": "prefix"
                    }
                },
                "cache": {
                    "optimizeFor": "freshness",
                    "networkTimeoutMs": 1000,
                    "maxEntries": 30,
                    "strategy": "lru",
                    "maxAgeMs": 360000000
                }
            }
        ]
    }
}

So the dynamic key in the above json caches the content on the page for offline use.所以上面json中的dynamic key缓存了页面上的内容供离线使用。 Here is an image of the content being cached:这是正在缓存的内容的图像:

在此处输入图片说明

However when I try to reload the page in offline mode after caching, the content is not being shown.但是,当我尝试在缓存后以离线模式重新加载页面时,内容并未显示。 Is there some configuration that I missed?有没有我错过的配置?

While waiting for ngsw to be ready, you could use workbox-build npm package in your Angular project.在等待ngsw准备就绪的同时,您可以在 Angular 项目中使用workbox-build npm 包。

For precaching assets:对于预缓存资产:

const workbox: WorkboxBuild = require('workbox-build');
workbox.injectManifest({
  globDirectory: './dist/',
  globPatterns: ['**\/*.{html,js,css,png,jpg,json}'],
  globIgnores: ['build/*', 'sw-default.js', 'workbox-sw.js','assets/icons/**/*'],
  swSrc: './src/sw-template.js',
  swDest: './dist/sw-default.js',
});

For dynamic caching:对于动态缓存:

const workboxSW = new self.WorkboxSW();

// work-images-cache
workboxSW.router.registerRoute('https://storage.googleapis.com/xxx.appspot.com/(.*)',
  workboxSW.strategies.cacheFirst({
    cacheName: 'work-images-cache',
    cacheExpiration: {
      maxEntries: 60
    },
    cacheableResponse: { statuses: [0, 200] }
  })
);

You could cache web fonts etc. by calling another registerRoute .您可以通过调用另一个registerRoute来缓存网络字体等。 Realistic usage example here .实际使用示例在这里

Even I faced the same issue when I used the same configuration.当我使用相同的配置时,甚至我也遇到了同样的问题。 Below worked out for me and I was able to retrieve list of coffees from the cache in offline mode Use dataGroups for dynamic caching in ngsw-config.json as shown below下面为我​​工作,我能够在离线模式下从缓存中检索咖啡列表使用数据dataGroupsngsw-config.json进行动态缓存,如下所示

    "dataGroups":[
      {
       "name":"api",
       "urls":[
        "/coffees"
       ],
      "cacheConfig": {
       "strategy": "performance",
       "timeout": "10s",
       "maxSize": 30,
       "maxAge": "1d"
       }
     }
   ]

Below articles can be helpful:以下文章可能会有所帮助:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM