From b01643f58ea14c9869845fcfc209fd69da5c1590 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sat, 16 Apr 2022 14:18:36 +0800 Subject: [PATCH] enhance: notify user the assets are changed after service worker update --- src/index.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 80046c0..d411ac2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,24 +1,33 @@ import React, { Suspense } from "react"; import ReactDOM from "react-dom"; import * as serviceWorker from "./serviceWorker"; -import { Switch, Route } from "react-router-dom"; +import { Route, Switch } from "react-router-dom"; import { Provider } from "react-redux"; -import { createStore, applyMiddleware, compose } from "redux"; +import { applyMiddleware, compose, createStore } from "redux"; import thunk from "redux-thunk"; import App from "./App"; import cloureveApp from "./reducers"; import { UpdateSiteConfig } from "./middleware/Init"; import ErrorBoundary from "./component/Placeholder/ErrorBoundary"; import { createBrowserHistory } from "history"; -import { routerMiddleware } from "connected-react-router"; -import { ConnectedRouter } from "connected-react-router"; +import { ConnectedRouter, routerMiddleware } from "connected-react-router"; + const Admin = React.lazy(() => import("./Admin")); if (window.location.hash !== "") { window.location.href = window.location.hash.split("#")[1]; } -serviceWorker.register(); +serviceWorker.register({ + onUpdate: (registration) => { + alert("当前页面有新版本可用,准备刷新。"); + if (registration && registration.waiting) { + registration.waiting.postMessage({ type: "SKIP_WAITING" }); + } + window.location.reload(); + }, +}); + export const history = createBrowserHistory(); let reduxEnhance = applyMiddleware(routerMiddleware(history), thunk); if (