小编Ben*_*aid的帖子

如何使用 Service Worker 处理 index.html 文件的缓存?

我目前正在使用create-react-app默认服务工作者开发渐进式 Web 应用程序。

在发布我们的一个 javascript 块的新版本时,我遇到了破坏缓存的问题。

构建时,输出 javascript 文件使用 acontenthash来确保当 JS 文件中的内容更改时,文件名也会更改。在没有 Service Worker 的情况下运行时,这成功地破坏了缓存。

但是,在使用create-react-appService Worker 时,包括我的index.html文件在内的所有静态资产都会被缓存。这意味着旧index.html的将提供给用户,其中包括<script>我的旧缓存 javascript 文件的标签,而不是带有更新的 .js 文件的新文件contenthash

我已经弹出并修改了 webpack.config.jsWorkboxWebpackPlugin以排除我的 index.html 文件:

 new WorkboxWebpackPlugin.GenerateSW({
      clientsClaim: true,
      exclude: [/\.map$/, /asset-manifest\.json$/, /index.html/],
      importWorkboxFrom: "cdn",
      navigateFallbackBlacklist: [
          // Exclude URLs starting with /_, as they're likely an API call
             new RegExp("^/_"),
          // Exclude URLs containing a dot, as they're likely a resource in …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs service-worker create-react-app

7
推荐指数
1
解决办法
2884
查看次数