Skip to content

Commit c22a578

Browse files
authored
Merge pull request #36 from Unity-Lab-AI/develop
Feat: Add cache-busting for local JS/CSS files
2 parents cab1145 + 55541cd commit c22a578

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

cache-bust.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ function findHtmlFiles(dir) {
5151
}
5252

5353
/**
54-
* Add cache-busting query parameters to external resources
54+
* Add cache-busting query parameters to resources
5555
*/
56-
function addQueryParamsToExternalResources(html) {
57-
// Add cache-busting to CDN resources that don't have versioning
56+
function addQueryParamsToResources(html) {
57+
// Add cache-busting to external CDN resources that don't have versioning
5858
html = html.replace(
5959
/(<script[^>]+src=["'])(https:\/\/[^"']+)(["'][^>]*>)/g,
6060
(match, prefix, url, suffix) => {
@@ -77,6 +77,30 @@ function addQueryParamsToExternalResources(html) {
7777
}
7878
);
7979

80+
// Add cache-busting to LOCAL JS files (non-hashed ones like visitor-tracking.js, age-verification.js)
81+
html = html.replace(
82+
/(<script[^>]+src=["'])([^"']+\.js)(["'][^>]*>)/g,
83+
(match, prefix, url, suffix) => {
84+
// Skip if already has query params, is external (https://), or is a Vite-hashed asset
85+
if (url.includes('?') || url.startsWith('http') || url.includes('/assets/') || url.includes('-')) {
86+
return match;
87+
}
88+
return `${prefix}${url}?v=${BUILD_HASH}${suffix}`;
89+
}
90+
);
91+
92+
// Add cache-busting to LOCAL CSS files (non-hashed ones)
93+
html = html.replace(
94+
/(<link[^>]+href=["'])([^"']+\.css)(["'][^>]*>)/g,
95+
(match, prefix, url, suffix) => {
96+
// Skip if already has query params, is external (https://), or is a Vite-hashed asset
97+
if (url.includes('?') || url.startsWith('http') || url.includes('/assets/') || url.includes('-')) {
98+
return match;
99+
}
100+
return `${prefix}${url}?v=${BUILD_HASH}${suffix}`;
101+
}
102+
);
103+
80104
return html;
81105
}
82106

@@ -95,8 +119,8 @@ function processHtmlFile(filePath) {
95119
console.warn(` ⚠️ No <head> tag found in ${filePath}`);
96120
}
97121

98-
// Add cache-busting to external resources
99-
html = addQueryParamsToExternalResources(html);
122+
// Add cache-busting to all resources (external and local)
123+
html = addQueryParamsToResources(html);
100124

101125
// Add build info comment
102126
const buildComment = `\n<!-- Built: ${BUILD_TIMESTAMP} | Hash: ${BUILD_HASH} -->\n`;
@@ -139,7 +163,8 @@ function main() {
139163
console.log(' 2. ✅ Pragma and Expires headers');
140164
console.log(' 3. ✅ Build timestamp and hash in meta tags');
141165
console.log(' 4. ✅ Query parameters added to external CDN resources');
142-
console.log(' 5. ✅ Vite content-hashed assets (built-in)');
166+
console.log(' 5. ✅ Query parameters added to local JS/CSS files');
167+
console.log(' 6. ✅ Vite content-hashed assets (built-in)');
143168
console.log('');
144169
console.log('🚀 Your site is now fully protected against aggressive caching!');
145170
}

0 commit comments

Comments
 (0)