🚀 JavaScript Cache Busting Solutions
1. Query Parameter Cache Busting (Quickest Fix)
<!-- Manual versioning --> <script
src="your-script.js?v=2.1.0"></script> <!-- Timestamp-based --> <script src="your-script.js?t=20240916123045"></script>
<!-- Dynamic timestamp (server-side) --> <script src="your-script.js?t=<?= filemtime('your-script.js') ?>"></script>
✅ Pros: - Immediate implementation
- No file renaming
needed
- Works with any server
❌ Cons: - Manual version management
- Easy to forget to update
2. Automatic Cache Busting Script (Recommended)
<script>
// Automatically append file modification time as cache buster
function loadScriptWithCacheBusting(src) { return new Promise((resolve, reject) => {
// Use current timestamp as cache buster
const cacheBuster = Date.now(); const script = document.createElement('script'); script.src = `${src}?v=${cacheBuster}`; script.onload =
resolve; script.onerror = reject; document.head.appendChild(script);
});
}
// Load your script
loadScriptWithCacheBusting('your-script.js') .then(() => console.log('Script loaded successfully')) .catch(err => console.error('Failed to load
script:', err));
</script>
✅ Pros: - Automatic cache busting
- No manual
version updates
- Always loads latest version
❌ Cons: - Scripts load on every page load
- Loses benefits of
caching
3. Smart Cache Busting with Version Check
<script>
// Check for script updates and reload if necessary
class SmartCacheBuster { constructor(scriptPath, currentVersion = '1.0.0') { this.scriptPath = scriptPath; this.currentVersion = currentVersion;
this.storageKey = `script_version_${scriptPath}`;
}
async loadScript() { const storedVersion = localStorage.getItem(this.storageKey); let cacheBuster = '';
// If version changed or no version stored, use cache buster
if (storedVersion !== this.currentVersion) { cacheBuster = `?v=${this.currentVersion}&t=${Date.now()}`; localStorage.setItem(this.storageKey,
this.currentVersion);
}
return this.loadScriptElement(`${this.scriptPath}${cacheBuster}`);
}
loadScriptElement(src) { return new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = src;
script.onload = resolve; script.onerror = reject; document.head.appendChild(script);
});
}
}
// Usage
const cacheBuster = new SmartCacheBuster('your-script.js', '2.1.0'); cacheBuster.loadScript(); </script>
4. Cloudflare Page Rules Configuration
# Cloudflare Page Rule for JS files
Pattern: yoursite.com/*.js Settings: - Browser Cache TTL: 30 minutes - Cache Level: Bypass (for development) - Edge Cache TTL: 2 hours
# Or use Cache-Control headers
Cache-Control: public, max-age=1800, must-revalidate
5. Meta Tag Cache Control
<!-- Add to your HTML head --> <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"> <meta
http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="0"> <!-- For specific resources --> <script>
// Force reload of specific scripts
if (performance.navigation.type === 1) {
// Page was refreshed, reload scripts with cache buster
document.querySelectorAll('script[src*="your-script"]').forEach(script => { script.src = script.src.split('?')[0] + '?reload=' + Date.now();
});
}
</script>
🎯 Quick Implementation Guide
For Immediate Fix (5 minutes):
- Change your script tag to:
<script src="your-script.js?v=1.1"></script> - Increment the version number
each time you update the file
- Clear Cloudflare cache once after the change
For Long-term Solution (30 minutes):
- Implement the automatic cache busting script above
- Set up Cloudflare Page
Rules for shorter cache times on JS files
- Consider using a build tool that automatically handles versioning