diff --git a/next.config.ts b/next.config.ts index ad9d8c4..2e9099e 100644 --- a/next.config.ts +++ b/next.config.ts @@ -3,7 +3,7 @@ import type { NextConfig } from 'next' const nextConfig: NextConfig = { reactStrictMode: true, experimental: {}, - turbopack: { root: __dirname } + turbopack: { root: __dirname }, } export default nextConfig diff --git a/package.json b/package.json index d62b7b9..70a752a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "dev": "next dev", - "build": "next build", + "build": "node scripts/generate-version.js && next build", "start": "next start", "lint": "eslint" }, diff --git a/public/sw.js b/public/sw.js index cdbf02a..99cd753 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,5 +1,5 @@ -const CACHE_NAME = 'greenhome-v2'; -const STATIC_CACHE_NAME = 'greenhome-static-v2'; +const CACHE_NAME = 'greenhome-1766074058129'; +const STATIC_CACHE_NAME = 'greenhome-static-1766074058129'; // Static assets to cache on install const STATIC_FILES_TO_CACHE = [ @@ -32,7 +32,8 @@ self.addEventListener('install', (event) => { } catch (error) { console.log('Some static files failed to cache:', error); } - await self.skipWaiting(); + // Don't skip waiting automatically - let user decide when to update + // await self.skipWaiting(); })() ); }); @@ -53,6 +54,20 @@ self.addEventListener('activate', (event) => { ); }); +// Listen for messages from clients (e.g., when user clicks update button) +self.addEventListener('message', (event) => { + if (event.data && event.data.type === 'SKIP_WAITING') { + self.skipWaiting().then(() => { + // Notify all clients about update + return self.clients.matchAll(); + }).then((clients) => { + clients.forEach((client) => { + client.postMessage({ type: 'SW_UPDATED' }); + }); + }); + } +}); + self.addEventListener('fetch', (event) => { const { request } = event; const url = new URL(request.url); diff --git a/public/version.json b/public/version.json new file mode 100644 index 0000000..0a045f7 --- /dev/null +++ b/public/version.json @@ -0,0 +1,3 @@ +{ + "version": "1766074058129" +} \ No newline at end of file diff --git a/scripts/generate-version.js b/scripts/generate-version.js new file mode 100644 index 0000000..38dfacd --- /dev/null +++ b/scripts/generate-version.js @@ -0,0 +1,38 @@ +const fs = require('fs'); +const path = require('path'); + +const version = Date.now().toString(); +const versionFile = path.join(__dirname, '../public/version.json'); + +// Ensure public directory exists +const publicDir = path.dirname(versionFile); +if (!fs.existsSync(publicDir)) { + fs.mkdirSync(publicDir, { recursive: true }); +} + +fs.writeFileSync(versionFile, JSON.stringify({ version }, null, 2)); + +// Also write to .env.local for Next.js to use +const envFile = path.join(__dirname, '../.env.local'); +const envContent = `NEXT_PUBLIC_APP_VERSION=${version}\n`; +fs.writeFileSync(envFile, envContent); + +// Update cache name in sw.js based on version +const swPath = path.join(__dirname, '../public/sw.js'); +if (fs.existsSync(swPath)) { + let swContent = fs.readFileSync(swPath, 'utf8'); + // Update cache names with version - using more specific regex + const cacheNameRegex = /const CACHE_NAME\s*=\s*['"][^'"]*['"]/; + const staticCacheNameRegex = /const STATIC_CACHE_NAME\s*=\s*['"][^'"]*['"]/; + + if (cacheNameRegex.test(swContent)) { + swContent = swContent.replace(cacheNameRegex, `const CACHE_NAME = 'greenhome-${version}'`); + } + if (staticCacheNameRegex.test(swContent)) { + swContent = swContent.replace(staticCacheNameRegex, `const STATIC_CACHE_NAME = 'greenhome-static-${version}'`); + } + fs.writeFileSync(swPath, swContent); +} + +console.log(`Version generated: ${version}`); + diff --git a/src/app/daily-report/page.tsx b/src/app/daily-report/page.tsx index d607c51..0749e6c 100644 --- a/src/app/daily-report/page.tsx +++ b/src/app/daily-report/page.tsx @@ -482,23 +482,43 @@ function DailyReportContent() { {/* Tabs */}