#!/bin/bash # Simple and reliable cache-busting implementation SERVER_FILE="/home/uroma/obsidian-web-interface/server.js" echo "[CACHE-BUSTING] Creating backup..." cp "$SERVER_FILE" "${SERVER_FILE}.backup" # Step 1: Add cache-busting constants after SESSION_SECRET line echo "[CACHE-BUSTING] Adding configuration..." sed -i "/const SESSION_SECRET = 'obsidian-web-secret-'*/a\\\\ // ============================================================\\\\ // CACHE-BUSTING CONFIGURATION\\\\ // ============================================================\\\\ // Build timestamp forces browser to reload all assets on server restart\\\\ const BUILD_TIMESTAMP = Date.now();\\\\ const ASSET_VERSION = \\\\`v=\${BUILD_TIMESTAMP}\\\\`;\\\\ \\\\ console.log(\\\\\`[CACHE-BUSTING] Build timestamp initialized: \${BUILD_TIMESTAMP}\\\\\`);\\\\ console.log(\\\\\`[CACHE-BUSTING] All JavaScript files will be served with ?\${ASSET_VERSION}\\\\\`); " "$SERVER_FILE" # Step 2: Add middleware import after database import echo "[CACHE-BUSTING] Adding middleware import..." sed -i "/const { db } = require('.\\/services\\/database');/a\\\\ \\\\ // Cache-busting middleware\\\\ const { createCacheBustingMiddleware } = require('.\\/cache-bust-middleware'); " "$SERVER_FILE" # Step 3: Add middleware application after session middleware echo "[CACHE-BUSTING] Applying middleware..." # Find the session middleware closing and add our middleware perl -i -0pe 's/(name: '"'"'connect\.sid'"'"'\n }\)\)\n/\1\n\n\/\/ Apply cache-busting middleware to HTML responses\napp.use(createCacheBustingMiddleware(ASSET_VERSION));\n/' "$SERVER_FILE" # Step 4: Update static file configuration echo "[CACHE-BUSTING] Updating static file headers..." perl -i -0pe 's/\/\/ Serve static files \(must come after specific routes\)\n\/\/ Disable caching for JS files to prevent browser from serving stale code\napp\.use\('"'"'\/claude'"'"', express\.static\(path\.join\(__dirname, '"'"'public'"'"'\), \{\n etag: false,\n lastModified: false,\n setHeaders: \(res, filePath\) => \{\n if \(filePath\.endsWith\('"'"'\.js'"'"'\)\)\) \{\n res\.setHeader\('"'"'Cache-Control'"'"', '"'"'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'"'"'\);\n res\.setHeader\('"'"'Pragma'"'"', '"'"'no-cache'"'"'\);\n res\.setHeader\('"'"'Expires'"'"', '"'"'0'"'"'\);\n \}\n \}\n\}\);/\/\/ Serve static files (must come after specific routes)\n\/\/ Disable caching for JS files to prevent browser from serving stale code\n\/\/ CRITICAL: Cache-busting via query parameters (ASSET_VERSION) ensures fresh content\napp.use('\''\/claude'\'', express.static(path.join(__dirname, '\''public'\''), {\n etag: false,\n lastModified: false,\n setHeaders: (res, filePath) => {\n if (filePath.endsWith('\''\.js'\'') || filePath.endsWith('\''\.css'\'')) {\n res.setHeader('\''Cache-Control'\'', '\''no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'\'');\n res.setHeader('\''Pragma'\'', '\''no-cache'\'');\n res.setHeader('\''Expires'\'', '\''0'\'');\n res.removeHeader('\''ETag'\'');\n }\n }\n});/' "$SERVER_FILE" echo "[CACHE-BUSTING] Verifying syntax..." if node -c "$SERVER_FILE" 2>&1 | grep -q "SyntaxError"; then echo "[CACHE-BUSTING] ✗ Syntax error detected!" echo "[CACHE-BUSTING] Restoring backup..." cp "${SERVER_FILE}.backup" "$SERVER_FILE" exit 1 else echo "[CACHE-BUSTING] ✓ All modifications applied successfully!" fi