chore: Improve startup speed of containers

This commit is contained in:
Dries Augustyns
2025-12-29 14:23:21 +01:00
parent 98b95b2ab2
commit f26ef53c8c
4 changed files with 185 additions and 99 deletions
+118
View File
@@ -0,0 +1,118 @@
#!/bin/sh
set -e
# Optimized URL Replacement Script
# Uses pre-generated manifests to avoid expensive find+grep operations at runtime
replace_urls_in_app() {
local app=$1
local app_dir=$2
echo "📝 Replacing URLs in $app build files..."
# Define placeholder URLs (built into the app at compile time)
local PLACEHOLDER_API="https://api.useplunk.com"
local PLACEHOLDER_DASHBOARD="https://app.useplunk.com"
local PLACEHOLDER_LANDING="https://www.useplunk.com"
local PLACEHOLDER_WIKI="https://docs.useplunk.com"
# Use pre-generated manifest instead of scanning all files
local manifest_file="$app_dir/.next/url-manifest.txt"
if [ ! -f "$manifest_file" ]; then
echo " ⚠️ Warning: Manifest file not found at $manifest_file"
echo " This suggests the build process didn't complete correctly."
echo " Falling back to runtime file scanning (slower)..."
# Fallback to old method if manifest doesn't exist
local temp_file_list="/tmp/replace_urls_${app}.txt"
find "$app_dir/.next" -type f \( -name "*.js" -o -name "*.json" -o -name "*.html" -o -name "*.rsc" \) \
-exec grep -l -E "$PLACEHOLDER_API|$PLACEHOLDER_DASHBOARD|$PLACEHOLDER_LANDING|$PLACEHOLDER_WIKI" {} \; \
2>/dev/null > "$temp_file_list" || true
manifest_file="$temp_file_list"
fi
# Count files from manifest
local file_count=$(wc -l < "$manifest_file" | tr -d ' ')
echo " 📊 Processing $file_count files from manifest"
if [ "$file_count" -gt 0 ]; then
# Process files using xargs for better performance
# Use -P 4 to process up to 4 files in parallel
# This significantly speeds up processing of many files
cat "$manifest_file" | xargs -P 4 -I {} sh -c '
file="{}"
if [ -f "$file" ]; then
sed -e "s|'"$PLACEHOLDER_API"'|'"$API_URI"'|g" \
-e "s|'"$PLACEHOLDER_DASHBOARD"'|'"$DASHBOARD_URI"'|g" \
-e "s|'"$PLACEHOLDER_LANDING"'|'"$LANDING_URI"'|g" \
-e "s|'"$PLACEHOLDER_WIKI"'|'"$WIKI_URI"'|g" \
"$file" > "${file}.tmp" && mv "${file}.tmp" "$file"
fi
'
echo " ✅ Successfully replaced URLs in $file_count files"
else
echo " ️ No files found with placeholder URLs"
fi
# Process sitemap and robots.txt from manifest
echo " 🗺️ Processing sitemap and robots.txt..."
local sitemap_manifest="$app_dir/.next/sitemap-manifest.txt"
if [ -f "$sitemap_manifest" ]; then
while IFS= read -r sitemap_file; do
if [ -f "$sitemap_file" ]; then
local rel_path="${sitemap_file#$app_dir/}"
sed -e "s|$PLACEHOLDER_API|$API_URI|g" \
-e "s|$PLACEHOLDER_DASHBOARD|$DASHBOARD_URI|g" \
-e "s|$PLACEHOLDER_LANDING|$LANDING_URI|g" \
-e "s|$PLACEHOLDER_WIKI|$WIKI_URI|g" \
"$sitemap_file" > "${sitemap_file}.tmp" && mv "${sitemap_file}.tmp" "$sitemap_file"
fi
done < "$sitemap_manifest"
else
# Fallback to finding sitemaps at runtime
find "$app_dir/public" -type f \( -name "sitemap*.xml" -o -name "robots.txt" \) 2>/dev/null | while IFS= read -r sitemap_file; do
if [ -f "$sitemap_file" ]; then
sed -e "s|$PLACEHOLDER_API|$API_URI|g" \
-e "s|$PLACEHOLDER_DASHBOARD|$DASHBOARD_URI|g" \
-e "s|$PLACEHOLDER_LANDING|$LANDING_URI|g" \
-e "s|$PLACEHOLDER_WIKI|$WIKI_URI|g" \
"$sitemap_file" > "${sitemap_file}.tmp" && mv "${sitemap_file}.tmp" "$sitemap_file"
fi
done
fi
# Special handling for wiki: also replace URLs in openapi.local.json
if [ "$app" = "wiki" ]; then
local openapi_file="$app_dir/openapi.local.json"
if [ -f "$openapi_file" ]; then
echo " 🔄 Replacing URLs in openapi.local.json"
sed -e "s|$PLACEHOLDER_API|$API_URI|g" \
-e "s|$PLACEHOLDER_DASHBOARD|$DASHBOARD_URI|g" \
-e "s|$PLACEHOLDER_LANDING|$LANDING_URI|g" \
-e "s|$PLACEHOLDER_WIKI|$WIKI_URI|g" \
"$openapi_file" > "${openapi_file}.tmp" && mv "${openapi_file}.tmp" "$openapi_file"
echo " ✅ Updated OpenAPI spec with runtime URLs"
fi
fi
# Generate .env file for server-side Next.js standalone
cat > "$app_dir/.env" << EOF
# Runtime environment configuration
# Generated at container startup from environment variables
API_URI=${API_URI}
DASHBOARD_URI=${DASHBOARD_URI}
LANDING_URI=${LANDING_URI}
WIKI_URI=${WIKI_URI}
EOF
echo "✅ URL replacement complete for $app"
}
# Export the function so it can be sourced by other scripts
if [ "$1" != "" ]; then
# If called directly with arguments, execute the function
replace_urls_in_app "$1" "$2"
fi