From 529b1bad89a640a6575055b6ad81810e13d518b9 Mon Sep 17 00:00:00 2001 From: Jeet Debnath Date: Tue, 7 Apr 2026 10:35:50 +0530 Subject: [PATCH] auto replace cache on new deploy --- public/sw.js | 2 +- public/vite.svg | 1 - vite.config.ts | 18 +++++++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) delete mode 100644 public/vite.svg diff --git a/public/sw.js b/public/sw.js index bd6abf5..6aa9cb5 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,4 +1,4 @@ -const CACHE = 'gj-v1' +const CACHE = 'gj-__BUILD_TIME__' self.addEventListener('install', (e) => { e.waitUntil( diff --git a/public/vite.svg b/public/vite.svg deleted file mode 100644 index e7b8dfb..0000000 --- a/public/vite.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index d4b962d..50b65ec 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,9 +1,25 @@ import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' +import fs from 'fs' +import path from 'path' + +function swBuildTimePlugin() { + return { + name: 'sw-build-time', + closeBundle() { + const swPath = path.resolve(__dirname, 'dist/sw.js') + if (fs.existsSync(swPath)) { + const content = fs.readFileSync(swPath, 'utf-8') + const timestamp = Date.now().toString() + fs.writeFileSync(swPath, content.replace('__BUILD_TIME__', timestamp)) + } + }, + } +} // https://vite.dev/config/ export default defineConfig({ - plugins: [react()], + plugins: [react(), swBuildTimePlugin()], server: { port: 8000, strictPort: false,