简体   繁体   English

服务工作者:从webpack创建的asset-manifest.json中缓存数据

[英]Service-worker: Cache data from asset-manifest.json created by webpack

I was trying to cache and serve all my static assets using service-worker. 我试图使用service-worker缓存和服务我所有的静态资产。 So for that, I wrote service-worker code as below: 因此,我编写了如下的服务人员代码:

var filesToCache = [
  '/src/assets/images/a.png',
  '/src/assets/images/b.png',
  '/src/assets/images/c.svg',
  '/src/assets/images/d.svg',
];

and the caching part would be like 而缓存部分会像

self.addEventListener('install', function(event) {
  console.log('cache static assets');
  event.waitUntil(
   caches.open(dataCacheName)
   .then(function(cache) {
     return cache.addAll(filesToCache);
   })
  );
});

This works perfectly until I add revisioning to all my assets using webpack . 直到我使用webpack 将修订添加到我所有资产的过程中,此方法webpack I use webpack-assets-manifest to create a manifest file of the revisioned assets. 我使用webpack-assets-manifest创建修订资产的清单文件。 That will look like: 看起来像:

{
  "src/assets/images/a.png": "/caa80bc36fced529800b0fc6e1d10bbc.png",
  "src/assets/images/b.png": "/973d60bc669967dd3a29d09f45fbd7bd.png",
  "src/assets/images/c.svg": "/727f31a23fe57eaf9ac47c6f23fc2af8.svg",
  "src/assets/images/d.svg": "/b95ecc4ec50d56eca49231508d57223f.svg"
}

After doing this, my html files are looking for a revisioned image/asset URL instead of a.png . 完成此操作后,我的html文件正在寻找修订的图像/资产URL,而不是a.png There fails my service-worker caching and serving. 我的service-worker缓存和服务失败。

Question: How can I use service-worker to cache my assets from a json file instead of manually created filesToCache array? 问题: 如何使用服务工作者从json文件而不是手动创建的filesToCache数组缓存资产? Or how can I cache the files to service worker while revisioning using webpack plugins 或者在使用webpack插件进行修订时如何将文件缓存到Service Worker

I have tried to use sw-precache , but couldn't figure out much about how to use in webpack config file. 我尝试过使用sw-precache ,但无法弄清楚如何在webpack配置文件中使用。

After doing this, my html files are looking for a revisioned image/asset URL instead of a.png. 完成此操作后,我的html文件正在寻找修订的图像/资产URL,而不是a.png。 There fails my service-worker caching and serving. 我的服务工作者缓存和服务失败。

It won't work, since the URLs are changed. 由于网址已更改,因此无法使用。

You can try using webpack-workbox-plugin , it's the successor of sw-precache. 您可以尝试使用webpack-workbox-plugin ,它是sw-precache的后继产品。 It generates precache manifest for both versioned (hashed) and not hashed filenames: 它为版本化(哈希)和非哈希文件名生成precache manifest

self.__precacheManifest = [
  {
    "revision": "62714997346e4959a02dbe66160b0cec",
    "url": "index.html"
  },
  {
    "revision": "762d606865bc8a04c69942e5ed42d226",
    "url": "assets/image1.png"  
  },
  {
    "url": "1.ef0046486233d8b9fcda.chunk.js"
  },
  {
    "url": "0.f660bbae32c384124068.chunk.js"
  }
];

Be sure to use the Workbox plugin as the last plugin, in order to cache all the files produced during the bundling process (for ex. generated by other plugins too). 确保将Workbox插件用作最后一个插件,以便缓存在捆绑过程中生成的所有文件(例如,其他插件也生成的文件)。

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

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