Move registerSW out
This commit is contained in:
		
							parent
							
								
									57bd37ef2f
								
							
						
					
					
						commit
						0b918464c1
					
				
					 4 changed files with 35 additions and 27 deletions
				
			
		|  | @ -58,6 +58,7 @@ export const darkTheme = { | ||||||
|     MuiPaper: { |     MuiPaper: { | ||||||
|       styleOverrides: { |       styleOverrides: { | ||||||
|         root: { |         root: { | ||||||
|  |           // for the sidebar on narrow (xs) screens
 | ||||||
|           backgroundImage: "none", |           backgroundImage: "none", | ||||||
|         }, |         }, | ||||||
|       }, |       }, | ||||||
|  |  | ||||||
|  | @ -1,34 +1,9 @@ | ||||||
| import * as React from "react"; | import * as React from "react"; | ||||||
| import { createRoot } from "react-dom/client"; | import { createRoot } from "react-dom/client"; | ||||||
| // eslint-disable-next-line import/no-unresolved |  | ||||||
| import { registerSW } from "virtual:pwa-register"; |  | ||||||
| import App from "./components/App"; | import App from "./components/App"; | ||||||
|  | import registerSW from "./registerSW"; | ||||||
| 
 | 
 | ||||||
| // fetch new sw every hour, i.e. update app every hour while running | registerSW(); | ||||||
| const intervalMS = 60 * 60 * 1000; |  | ||||||
| 
 |  | ||||||
| // https://vite-pwa-org.netlify.app/guide/periodic-sw-updates.html |  | ||||||
| registerSW({ |  | ||||||
|   onRegisteredSW(swUrl, registration) { |  | ||||||
|     if (!registration) { |  | ||||||
|       return; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     setInterval(async () => { |  | ||||||
|       if (registration.installing || navigator?.onLine === false) return; |  | ||||||
| 
 |  | ||||||
|       const resp = await fetch(swUrl, { |  | ||||||
|         cache: "no-store", |  | ||||||
|         headers: { |  | ||||||
|           cache: "no-store", |  | ||||||
|           "cache-control": "no-cache", |  | ||||||
|         }, |  | ||||||
|       }); |  | ||||||
| 
 |  | ||||||
|       if (resp?.status === 200) await registration.update(); |  | ||||||
|     }, intervalMS); |  | ||||||
|   }, |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| const root = createRoot(document.querySelector("#root")); | const root = createRoot(document.querySelector("#root")); | ||||||
| root.render(<App />); | root.render(<App />); | ||||||
|  |  | ||||||
							
								
								
									
										31
									
								
								web/src/registerSW.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								web/src/registerSW.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,31 @@ | ||||||
|  | // eslint-disable-next-line import/no-unresolved
 | ||||||
|  | import { registerSW as viteRegisterSW } from "virtual:pwa-register"; | ||||||
|  | 
 | ||||||
|  | // fetch new sw every hour, i.e. update app every hour while running
 | ||||||
|  | const intervalMS = 60 * 60 * 1000; | ||||||
|  | 
 | ||||||
|  | // https://vite-pwa-org.netlify.app/guide/periodic-sw-updates.html
 | ||||||
|  | const registerSW = () => | ||||||
|  |   viteRegisterSW({ | ||||||
|  |     onRegisteredSW(swUrl, registration) { | ||||||
|  |       if (!registration) { | ||||||
|  |         return; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       setInterval(async () => { | ||||||
|  |         if (registration.installing || navigator?.onLine === false) return; | ||||||
|  | 
 | ||||||
|  |         const resp = await fetch(swUrl, { | ||||||
|  |           cache: "no-store", | ||||||
|  |           headers: { | ||||||
|  |             cache: "no-store", | ||||||
|  |             "cache-control": "no-cache", | ||||||
|  |           }, | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         if (resp?.status === 200) await registration.update(); | ||||||
|  |       }, intervalMS); | ||||||
|  |     }, | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  | export default registerSW; | ||||||
|  | @ -16,6 +16,7 @@ export default defineConfig(({ mode }) => ({ | ||||||
|     react(), |     react(), | ||||||
|     VitePWA({ |     VitePWA({ | ||||||
|       registerType: "autoUpdate", |       registerType: "autoUpdate", | ||||||
|  |       // see registerSW.js imported by index.jsx
 | ||||||
|       injectRegister: null, |       injectRegister: null, | ||||||
|       strategies: "injectManifest", |       strategies: "injectManifest", | ||||||
|       devOptions: { |       devOptions: { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue