feat: implement CDN URL support for asset loading and add replacement script

This commit is contained in:
Victor Santos
2025-12-12 11:45:12 -03:00
parent 9fae065e24
commit 288df1adfd
5 changed files with 42 additions and 9 deletions

View File

@@ -10,7 +10,13 @@ fi
echo "Replacing pre-baked value.."
# Replace in JS files in assets directory
find assets -type f -name "*.js" |
while read file; do
sed -i "s|$ORIGINAL|$REPLACEMENT|g" "$file"
done
# Replace in index.html (for asset references)
if [ -f "index.html" ]; then
sed -i "s|$ORIGINAL|$REPLACEMENT|g" "index.html"
fi

View File

@@ -29,14 +29,14 @@ export default defineConfig(({ mode }) => {
"0.0.1"
).replaceAll(".", "-");
// Optional CDN URL for static assets (e.g., https://cdn.example.com)
// When set, all static assets will be served from this URL instead of the same origin.
// This is useful for serving assets from a CDN subdomain while keeping API calls on the main domain.
// If not set, assets are served from the same origin (backward compatible).
// CDN URL for static assets in /assets/* only.
// Docker: Set CDN_URL env var at runtime (placeholder replaced at container startup).
// Direct build: Use --build-arg CDN_URL=https://... or VITE_CDN_URL env var.
// Default: Empty = same-origin asset loading.
const cdnUrl = env.VITE_CDN_URL || "";
return {
base: cdnUrl || "/",
base: "/",
server: {
allowedHosts,
host: true,
@@ -59,6 +59,15 @@ export default defineConfig(({ mode }) => {
}
}
},
experimental: {
// Only apply CDN URL to files in /assets/* directory
renderBuiltUrl(filename) {
if (filename.startsWith("assets/")) {
return `${cdnUrl}/${filename}`;
}
return `/${filename}`;
}
},
plugins: [
tsconfigPaths(),
nodePolyfills({