threejs-test/build-cache-bust.sh
Juan Sebastian Montoya 98ec60641d
All checks were successful
Build and Publish Docker Image / Publish to Registry (push) Successful in 8s
Build and Publish Docker Image / Deploy to Portainer (push) Successful in 2s
Hotfix/Cache bust all js files (#21)
Reviewed-on: #21
Co-authored-by: Juan Sebastian Montoya <juansmm@outlook.com>
Co-committed-by: Juan Sebastian Montoya <juansmm@outlook.com>
2025-11-26 19:40:41 -05:00

46 lines
1.8 KiB
Bash
Executable file

#!/bin/sh
# Simple cache busting script - adds version query params to all JS imports
# Only runs during Docker build, not during local development
# This keeps development simple (no tools needed) while ensuring production cache busting
VERSION="${1:-dev}"
SRC_DIR="${2:-/usr/share/nginx/html}"
echo "Adding cache busting (v=${VERSION}) to JS imports in ${SRC_DIR}/src..."
# Find all JS files and add version query param to relative imports
find "$SRC_DIR/src" -type f -name "*.js" | while read -r file; do
# Skip if file doesn't exist
[ ! -f "$file" ] && continue
# Process file with sed to add version query params
# Pattern: from './path.js' or from "../path.js" or import('./path.js')
# Only match relative imports (./ or ../) ending in .js
# Skip if already has query params (contains ?)
# Handle single quote imports: from './path.js'
sed -i.bak \
-e "s|from '\\(\\.[./][^']*\\.js\\)'|from '\\1?v=${VERSION}'|g" \
-e "s|from '\\(\\.[./][^']*\\.js\\)?[^']*'|from '\\1?v=${VERSION}'|g" \
"$file"
# Handle double quote imports: from "./path.js"
sed -i.bak \
-e "s|from \"\\(\\.[./][^\"]*\\.js\\)\"|from \"\\1?v=${VERSION}\"|g" \
-e "s|from \"\\(\\.[./][^\"]*\\.js\\)?[^\"]*\"|from \"\\1?v=${VERSION}\"|g" \
"$file"
# Handle dynamic imports: import('./path.js')
sed -i.bak \
-e "s|import('\\(\\.[./][^']*\\.js\\)')|import('\\1?v=${VERSION}')|g" \
-e "s|import('\\(\\.[./][^']*\\.js\\)?[^']*')|import('\\1?v=${VERSION}')|g" \
-e "s|import(\"\\(\\.[./][^\"]*\\.js\\)\")|import(\"\\1?v=${VERSION}\")|g" \
-e "s|import(\"\\(\\.[./][^\"]*\\.js\\)?[^\"]*\")|import(\"\\1?v=${VERSION}\")|g" \
"$file"
# Remove backup file
rm -f "${file}.bak"
done
echo "Cache busting complete! All JS imports now include ?v=${VERSION}"