简体   繁体   English

如何从Workbox实际显示更新的index.html?

[英]How do I actually display my updated index.html from Workbox?

I have a completely static site - index.html and some CSS/JS/PNG files. 我有一个完全静态的站点 - index.html和一些CSS / JS / PNG文件。 I'd like to use a service worker to cache all of them. 我想使用服务工作者来缓存所有这些。 It seems that Workbox should make this easy. 似乎Workbox应该让这很容易。 Here's my sw.js: 这是我的sw.js:

importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.5.0/workbox-sw.js');
workbox.precaching.precacheAndRoute([]);
workbox.routing.registerNavigationRoute('/index.html');

And here's my build script: 这是我的构建脚本:

const workboxBuild = require("workbox-build");

// NOTE: This should be run *AFTER* all your assets are built
const buildSW = async () => {
  console.log("Generating sw.js...")

  const {count, size, warnings} = await workboxBuild.injectManifest({
    swSrc: "src/sw.js",
    swDest: "build/sw.js",
    globDirectory: "build",
    globPatterns: [
      "**/*.{js,css,html,png}",
    ]
  })

  warnings.forEach(console.warn);
  console.log(`${count} files will be precached, totaling ${size} bytes.`);
}

buildSW();

That all seems to work fine. 这一切似乎都很好。 The build script runs, the service worker is activated, and my site continues to work even when I'm offline. 构建脚本运行,服务工作程序被激活,即使我处于脱机状态,我的站点也会继续工作。 Fantastic! 太棒了!

Then I make some modifications to index.html. 然后我对index.html进行了一些修改。 I reload my page. 我重装了我的页面。 I see this message in the console, indicating that it has cached my index.html, although it's still showing the old one in the browser: 我在控制台中看到此消息,表明它已缓存我的index.html,尽管它仍在浏览器中显示旧的:

Precaching 1 file. 35 files are already cached.

I believe this is correct, it's caching the new index.html after displaying the cached one. 我相信这是正确的,它在显示缓存的一个后缓存新的index.html。 So then next reload it should display the new index.html, right? 那么接下来重装它应该显示新的index.html,对吗?

Wrong! 错误! Reload again, and the old index.html is still displayed. 再次重新加载,旧的index.html仍然显示。 Repeat this many times, no change. 重复这几次,没有变化。 To actually see the new index.html, a normal reload will never work, I need to do ctrl+shift+R. 要实际看到新的index.html,正常的重新加载永远不会工作,我需要做ctrl + shift + R.

This can't be how it's supposed to work, right? 这不可能是它应该如何工作,对吧? I must be missing something obvious, but my code is so simple and practically pulled from the examples on the Workbox website, I don't see where I am messing up. 我必须遗漏一些明显的东西,但我的代码是如此简单,几乎从工作箱网站上的示例中提取出来,我看不出我搞砸了。

EDIT: I put a ridiculously simple example of this problem on GitHub . 编辑:我在GitHub上提出了这个问题的一个非常简单的例子 I feel very stupid - the example is so simple, yet I still can't figure out why it behaves like it does. 我觉得非常愚蠢 - 这个例子很简单,但我仍然无法弄清楚它为什么会这样。

Okay I stupidly figured out the answer shortly after creating a bounty. 好吧,我在创造赏金后不久就愚蠢地想出了答案。 As https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle says: 正如https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle所说:

"Once successfully installed, the updated worker will wait until the existing worker is controlling zero clients. (Note that clients overlap during a refresh.)" “成功安装后,更新的工作程序将等待现有工作程序控制零客户端。(请注意,客户端在刷新期间会重叠。)”

So refreshing will never switch to the new version, you have to totally close all open tabs of your app and then navigate to it again. 因此,刷新永远不会切换到新版本,您必须完全关闭应用程序的所有打开选项卡,然后再次导航到它。 That does seem to work. 这确实有效。

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

相关问题 如何链接回index.html上的选项卡? - How do I link back to a tab on my index.html? 工作箱 - SPA - 回退到 /index.html - Workbox - SPA - fallback to /index.html 如何将contact.handlebars添加为index.html而不是contact.html的链接? - How do I add contact.handlebars as a link to my index.html instead of contact.html? 如何将index.html中的内容放入我的view.html? - How can I put content from my index.html into my view.html? 如何在 index.html 文件中定义变量? (window.parentPage = true;) - How do I define a variable in my index.html file? (window.parentPage = true;) 如何使用 JavaScript 在我的 index.html 文件中嵌入 API 调用? - How do I embed an API call in my index.html file using JavaScript? 如何将外部.js文件添加到index.html? (VueJS2) - How do I add an external .js file to my index.html? (VueJS2) 如何将我的index.html与Systemsj配置为使用现有的包? - How do I configure my index.html with Systemsj to use an existing bundle? Angular 2 - 如何使关键字和描述的index.html文件标题和元标记动态化 - Angular 2 - how do I make my index.html files title and meta tags for keywords and description dynamic 如何在电子上加载 index.html 文件? - How do I load the index.html file on electron?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM