我想修改create-react-appservice worker文件并实现弹出消息,如果新的 service worker 准备好激活,它将要求用户更新应用程序。我几乎完成了解决方案,但有一个陷阱。我想在用户确认 service worker 更新弹出窗口时重新加载应用程序,所以我在register函数的末尾添加了一些代码,见下文:
export default function register(config) {
if (process.env.NODE_ENV === "production" && "serviceWorker" in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location)
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used …Run Code Online (Sandbox Code Playgroud)