feat: ensure ui automatically updates regardless of version number

Task #53 completed by ClaudeQueue

ClaudeQueue
This commit is contained in:
Claude Worker
2026-06-30 21:14:06 +00:00
parent 2b195d3b6b
commit 7cf3517dd3
3 changed files with 79 additions and 34 deletions

View File

@@ -24,6 +24,7 @@ import { serveStatic } from 'hono/bun';
import { logger } from 'hono/logger';
import { spawnSync } from 'node:child_process';
import { createServer } from 'node:http';
import { readFileSync } from 'node:fs';
import { initDb, upsertUser, recordVideoAccess, getUserData } from './db.js';
const PORT = parseInt(process.env.PORT || '3000', 10);
@@ -333,6 +334,35 @@ app.get('/api/user/data', async (c) => {
}
});
// ============================================================================
// GET /sw.js — serve the service worker with BUILD_TAG injected
//
// The raw sw.js file contains the placeholder `__BUILD_TAG__` which is
// replaced here with the actual BUILD_TAG string so the SW's cache name
// tracks the deployment automatically — no manual version bump needed.
// Served with no-store cache headers so browsers always re-fetch it and
// pick up the substituted value rather than a browser-cached stale copy.
// ============================================================================
let _swSource = null;
app.get('/sw.js', (c) => {
if (!_swSource) {
try {
_swSource = readFileSync('./public/sw.js', 'utf8');
} catch {
return c.text('Service worker not found', 404);
}
}
// Inject the build tag: replace the placeholder with the real value.
const src = _swSource.replace(
"typeof __BUILD_TAG__ !== 'undefined' ? __BUILD_TAG__ : 'v1.0.3'",
JSON.stringify(BUILD_TAG)
);
return c.text(src, 200, {
'Content-Type': 'application/javascript; charset=utf-8',
'Cache-Control': 'no-store, no-cache, must-revalidate',
});
});
// ============================================================================
// Static files — serve the frontend/public directory
// Must come AFTER all /api routes so API takes priority