chore: Improve startup speed of containers
This commit is contained in:
@@ -16,8 +16,9 @@ fi
|
||||
|
||||
# Run database migrations
|
||||
echo "🗄️ Running database migrations..."
|
||||
cd /app
|
||||
if ! yarn workspace @plunk/db migrate:prod; then
|
||||
# Call Prisma binary directly for fastest startup (avoids Yarn workspace resolution and npx overhead)
|
||||
# The Prisma client and schema are already available from the build
|
||||
if ! /app/node_modules/.bin/prisma migrate deploy --schema=/app/packages/db/prisma/schema.prisma; then
|
||||
echo ""
|
||||
echo "❌ ERROR: Database migration failed!"
|
||||
echo " The container cannot start with failed migrations."
|
||||
@@ -71,104 +72,11 @@ echo " LANDING_DOMAIN=www.example.com"
|
||||
echo " WIKI_DOMAIN=docs.example.com"
|
||||
echo " USE_HTTPS=true"
|
||||
|
||||
# Replace URLs in built files with runtime configuration
|
||||
# This approach is simpler and more reliable than window.__ENV__
|
||||
replace_urls_in_app() {
|
||||
local app=$1
|
||||
local app_dir=$2
|
||||
# Source the optimized URL replacement script
|
||||
. /app/docker/replace-urls-optimized.sh
|
||||
|
||||
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"
|
||||
|
||||
# Find and replace URLs in all built files
|
||||
# Process .next directory (contains all built output: static/, server/, standalone bundles)
|
||||
# Include .js, .json, .html files to catch all possible occurrences
|
||||
echo " 🔍 Scanning for files with placeholder URLs..."
|
||||
|
||||
# Create a temporary file list of files that need processing
|
||||
local temp_file_list="/tmp/replace_urls_${app}.txt"
|
||||
|
||||
# Search in .next directory for all file types that might contain URLs
|
||||
# Include: .js (bundles), .json (manifests), .html (static pages), .rsc (React Server Components)
|
||||
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
|
||||
|
||||
# Count and process files
|
||||
local file_count=$(wc -l < "$temp_file_list" | tr -d ' ')
|
||||
echo " 📊 Found $file_count files containing placeholder URLs"
|
||||
|
||||
if [ "$file_count" -gt 0 ]; then
|
||||
while IFS= read -r file; do
|
||||
# Show relative path for better readability
|
||||
local rel_path="${file#$app_dir/}"
|
||||
echo " 🔄 $rel_path"
|
||||
|
||||
# Replace all URLs - compatible with both GNU and BSD sed
|
||||
# Create temp file, replace, then move
|
||||
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"
|
||||
done < "$temp_file_list"
|
||||
|
||||
echo " ✅ Successfully replaced URLs in $file_count files"
|
||||
else
|
||||
echo " ⚠️ No files found with placeholder URLs"
|
||||
fi
|
||||
|
||||
rm -f "$temp_file_list"
|
||||
|
||||
# Replace URLs in sitemap.xml and robots.txt (generated by next-sitemap)
|
||||
echo " 🗺️ Processing sitemap and robots.txt..."
|
||||
|
||||
# Find all sitemap files (sitemap.xml, sitemap-0.xml, etc.)
|
||||
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
|
||||
local rel_path="${sitemap_file#$app_dir/}"
|
||||
echo " 🔄 $rel_path"
|
||||
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
|
||||
|
||||
# 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
|
||||
|
||||
# Also 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"
|
||||
}
|
||||
|
||||
# Replace URLs in all Next.js apps
|
||||
# Replace URLs in all Next.js apps using optimized function
|
||||
# This uses pre-generated manifests from build time instead of scanning all files
|
||||
replace_urls_in_app "web" "/app/apps/web/.next/standalone/apps/web"
|
||||
replace_urls_in_app "landing" "/app/apps/landing/.next/standalone/apps/landing"
|
||||
replace_urls_in_app "wiki" "/app/apps/wiki/.next/standalone/apps/wiki"
|
||||
|
||||
Reference in New Issue
Block a user