简体   繁体   English

如何使用sw-precache缓存除内容以外的所有内容

[英]How to cache everything but content using sw-precache

I'm using sw-precache in a jekyll website to add offline capabilities with the following configuration: 我正在jekyll网站中使用sw-precache通过以下配置添加脱机功能:

gulp.task('generate-service-worker', function(cb) {
  var path = require('path');
  var swPrecache = require('sw-precache');
  var rootDir = '_site';
  var packageJson = require('./package.json');

  swPrecache.write('./service-worker.js', {
    staticFileGlobs: [rootDir + '/**/*.{html,css,png,jpg,gif,svg}', rootDir + '/js/*'],
    stripPrefix: rootDir + '/',
    runtimeCaching: [{
      urlPattern: /\/$/,
      handler: 'networkOnly'
    }],
    handleFetch: argv.cacheAssets || false,
    maximumFileSizeToCacheInBytes: 10485760, // 10 mb
    cacheId: packageJson.name + '-v' + packageJson.version
  }, cb);
});

The problem is that, when I change content in the website (for example, text in a blog post, or some text from the index page) the changes won't be shown until the new serviceworker version has been installed and the browser has been refreshed, which of course, is the expected behaviour of cacheFirst . 问题是,当我更改网站上的内容(例如,博客文章中的文本或索引页面中的某些文本)时,直到安装了新的serviceworker版本并且浏览器已安装,更改才会显示出来。刷新,这当然是cacheFirst的预期行为。

What I want is to make the request to the index of the site always network first, which is what I'm trying here: 我想要的是始终首先将对站点索引的请求发送到网络,这就是我在这里尝试的操作:

runtimeCaching: [{
  urlPattern: /\/$/,
  handler: 'networkFirst'
}]

But this isn't working, the index is always getting fetch from the serviceworker and not from network, how can I accomplish this? 但这是行不通的,索引总是从服务工作者那里获取,而不是从网络获取,我该怎么做呢?

My problem is that I was including the actual page contents for precache: '/**/*.{html,css,png,jpg,gif,svg}' . 我的问题是我要包含预缓存的实际页面内容: '/**/*.{html,css,png,jpg,gif,svg}' . '/**/*.{html,css,png,jpg,gif,svg}'

Excluding the html files works as expected: 排除html文件可以按预期工作:

'/**/*.{css,png,jpg,gif,svg}'

Change the url pattern to 将网址格式更改为

urlPattern: "'/'"

This is a exact match pattern. 这是完全匹配模式。 Your index will match to this and nothing else. 您的索引将与此匹配,仅此而已。

The solution for this is, treat your index.html as dynamic content. 解决方案是将index.html视为动态内容。

Change you sw webpack config to 将您的sw webpack配置更改为

new SWPrecacheWebpackPlugin({
  cacheId: 'yourcacheid',
  filename: 'service-worker.js',
  staticFileGlobs: [
    'dist/**/*.{js,css}'
  ],
  minify: true,
  stripPrefix: 'dist/',
  runtimeCaching: [{
    urlPattern: /\/$/,
    handler: 'networkFirst'
  }]
})

Remove your index.html from staticFileGlobs and add you root index to runtime caching. 从staticFileGlobs中删除index.html,并将根索引添加到运行时缓存中。

Then look at your cache storage. 然后查看您的缓存存储。 You will see something like $$$toolbox-cache$$$ https://your-domain.com as a new cache item. 您会看到类似$$$$ toolbox-cache $$$ https://your-domain.com这样的新缓存项。 Inspect that and you can see your index cached there. 检查一下,您可以看到索引缓存在那里。

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

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