#!/bin/bash # Comprehensive Cache-Busting Implementation set -e SERVER_FILE="/home/uroma/obsidian-web-interface/server.js" echo "[CACHE-BUSTING] Applying modifications to $SERVER_FILE" # Create backup cp "$SERVER_FILE" "${SERVER_FILE}.cache-bust-backup" # 1. Add cache-busting configuration after SESSION_SECRET (line 36) sed -i '/const SESSION_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" # 2. Add middleware import after database import (line 12) sed -i "/const { db } = require('.\\/services\\/database');/a\\\ // Cache-busting middleware\\ const { createCacheBustingMiddleware } = require('./cache-bust-middleware');" "$SERVER_FILE" # 3. Add middleware application after session middleware # Find the line with "name: 'connect.sid'" and add middleware after the closing }); sed -i "/name: 'connect.sid'/,/});/{ /});/a\\ \\ // Apply cache-busting middleware to HTML responses\\ app.use(createCacheBustingMiddleware(ASSET_VERSION)); }" "$SERVER_FILE" # 4. Update static file configuration to remove ETag and enhance cache headers sed -i '/\/\/ Serve static files (must come after specific routes)/,/etag: false/,/setHeaders: (res, filePath) => {/,/}$/{ /\/\// Disable caching for JS files to prevent browser from serving stale code/c\ // Disable caching for JS files to prevent browser from serving stale code\ // CRITICAL: Cache-busting via query parameters (ASSET_VERSION) ensures fresh content /if (filePath.endsWith('\''js'\''))/c\ // Disable caching for all JavaScript and CSS files\ if (filePath.endsWith('\''js'\'') || filePath.endsWith('\''css'\'')) { /res.setHeader('\''Pragma'\'', '\''no-cache'\'');/a\ res.removeHeader('\''ETag'\''); }' "$SERVER_FILE" echo "[CACHE-BUSTING] Modifications applied successfully!" echo "[CACHE-BUSTING] Testing syntax..." if node -c "$SERVER_FILE" 2>&1; then echo "[CACHE-BUSTING] ✓ Syntax is valid" else echo "[CACHE-BUSTING] ✗ Syntax error detected, restoring backup" cp "${SERVER_FILE}.cache-bust-backup" "$SERVER_FILE" exit 1 fi