Fixed frontend URL error by injecting config.js and runtime-config.js before Caddy starts

This commit is contained in:
Andreas Düren 2025-03-18 20:03:16 +01:00
parent e329b54b8b
commit e093bfc571

View File

@ -1037,6 +1037,66 @@ echo "==> Created Caddy config at /app/data/caddy/Caddyfile"
# Start Caddy server
echo "==> Starting Caddy server"
# First inject the config.js script tags into all HTML files
echo "==> Injecting config.js into web application HTML files"
# Function to inject the script tag
inject_script_tag() {
file="$1"
if [ -f "$file" ]; then
echo "==> Injecting config.js into $file"
# Make a backup just in case
cp "$file" "${file}.bak"
# Insert the script tag right after the opening head tag
sed -i 's/<head>/<head>\n <script src="\/config.js" type="text\/javascript"><\/script>/' "$file"
else
echo "==> WARNING: Could not find $file to inject config script"
fi
}
# Inject into all the web apps
inject_script_tag "/app/web/photos/index.html"
inject_script_tag "/app/web/accounts/index.html"
inject_script_tag "/app/web/auth/index.html"
inject_script_tag "/app/web/cast/index.html"
# Also create a runtime-config.js file with properly escaped content
cat > /app/web/photos/runtime-config.js <<EOT
// Runtime configuration for Ente
window.ENTE_CONFIG = {
API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
};
// Next.js environment variables
window.process = window.process || {};
window.process.env = window.process.env || {};
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.NEXT_PUBLIC_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
console.log('Ente runtime config loaded from runtime-config.js');
console.log('API_URL:', window.ENTE_CONFIG.API_URL);
console.log('PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
EOT
# Copy the runtime-config.js to each app directory
for app_dir in /app/web/photos /app/web/accounts /app/web/auth /app/web/cast; do
cp /app/web/photos/runtime-config.js "$app_dir/"
# Update the HTML to include this file too
index_file="$app_dir/index.html"
if [ -f "$index_file" ]; then
echo "==> Adding runtime-config.js to $index_file"
sed -i 's/<\/head>/<script src="runtime-config.js" type="text\/javascript"><\/script>\n <\/head>/' "$index_file"
fi
done
# Ensure all files are readable
chmod -R 644 /app/web/*/runtime-config.js
echo "==> Config injected into all web applications"
caddy start --config /app/data/caddy/Caddyfile --adapter caddyfile &
CADDY_PID=$!
echo "==> Caddy started with PID $CADDY_PID"