From 7379d0c54006ab99806c19c13abc9a8904b6dbd6 Mon Sep 17 00:00:00 2001 From: Protocolbot Date: Mon, 6 Jul 2026 13:36:08 -0600 Subject: [PATCH] fix: rewrite asset and API paths for subdirectory deployments index.html uses absolute /frontend/ paths for CSS and JS, and app.js hardcoded const API = '/api'. In subdirectory deployments these resolve to the wrong URL, causing unstyled HTML and broken API calls. - index.php: rewrite /frontend/ references in index.html with detected basePath - app.js: derive BASE_PATH from the script's own URL and prefix API calls --- frontend/app.js | 17 +++++++++++++++-- index.php | 24 +++++++++++++----------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/frontend/app.js b/frontend/app.js index b72f1a2..2dd0956 100755 --- a/frontend/app.js +++ b/frontend/app.js @@ -1,6 +1,19 @@ /* Protocol Droid — Frontend Application */ -const API = '/api'; +/* Detect base path from the script's own URL for subdirectory deployments */ +/* app.js is loaded from /frontend/app.js, so base = everything before /frontend/app.js */ +(function() { + var scripts = document.getElementsByTagName('script'); + for (var i = 0; i < scripts.length; i++) { + var src = scripts[i].src || ''; + var m = src.match(/^https?:\/\/[^/]+(\/.*)?\/frontend\/app\.js$/); + if (m) { + window.BASE_PATH = m[1] || ''; + break; + } + } +})(); +const API = (window.BASE_PATH || '') + '/api'; let currentUser = null; let siteConfig = {}; let allTags = []; @@ -835,4 +848,4 @@ function formatDate(s) { } // --- Boot --- -init(); \ No newline at end of file +init(); diff --git a/index.php b/index.php index b02b08d..f46614a 100755 --- a/index.php +++ b/index.php @@ -29,28 +29,30 @@ $file = __DIR__ . '/frontend' . $uri; $real_file = realpath($file); $frontend_dir = realpath(__DIR__ . '/frontend'); -if ($real_file === false || strpos($real_file, $frontend_dir) !== 0) { +// Helper: serve index.html with asset paths rewritten for subdirectory deployments +$serve_index = function() use ($basePath) { $index = __DIR__ . '/frontend/index.html'; if (file_exists($index)) { header('Content-Type: text/html'); - readfile($index); + $html = file_get_contents($index); + // Rewrite absolute /frontend/ paths to include the base path + if ($basePath) { + $html = str_replace('/frontend/', $basePath . '/frontend/', $html); + } + echo $html; exit; } http_response_code(404); echo 'Frontend not found'; exit; +}; + +if ($real_file === false || strpos($real_file, $frontend_dir) !== 0) { + $serve_index(); } if (!is_file($real_file)) { - $index = __DIR__ . '/frontend/index.html'; - if (file_exists($index)) { - header('Content-Type: text/html'); - readfile($index); - exit; - } - http_response_code(404); - echo 'Frontend not found'; - exit; + $serve_index(); } $ext = pathinfo($real_file, PATHINFO_EXTENSION);