A long cache lifetime allows browsers to store static files such as images, CSS, JavaScript, fonts, and media files for extended periods. This reduces the number of requests sent to your server, improves page load times for returning visitors, and can improve website performance scores.
The key is to use long cache durations only for files that rarely change and to combine them with file versioning or cache busting techniques.
Why Long Cache Lifetimes Improve Performance
When a browser caches a file, it can reuse the local copy instead of downloading it again.
Benefits include:
- Faster page loads
- Reduced server load
- Lower bandwidth usage
- Improved user experience
- Better Core Web Vitals metrics
Static assets are ideal candidates because they often remain unchanged for long periods.
Which Files Should Be Cached Long-Term?
Long cache lifetimes are typically used for:
- Images
- CSS files
- JavaScript files
- Web fonts
- Videos
- PDF documents
- Icons
Files that change frequently, such as HTML pages, usually require shorter cache durations.
Using Cache-Control Headers
The modern way to control browser caching is with the Cache-Control header.
A common configuration for static assets is:
<FilesMatch "\.(css|js|jpg|jpeg|png|gif|webp|svg|woff|woff2)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
In this example:
public allows caching by browsers and intermediary caches
max-age=31536000 sets a one-year cache lifetime
immutable tells browsers the file is unlikely to change during that period
Apache .htaccess Example
On Apache servers, you can configure browser caching through .htaccess.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
This instructs browsers to cache matching files for up to one year.
Nginx Example
For Nginx servers, a similar configuration might look like:
location ~* \.(css|js|jpg|jpeg|png|gif|webp|svg|woff|woff2)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
This applies long-term caching to common static file types.
Why Cache Busting Is Important
Long cache lifetimes can create problems if a file changes but browsers continue using an older cached version.
The solution is cache busting.
Common methods include:
- Version numbers in filenames
- Hashes in filenames
- Query string versions
- Build-generated asset fingerprints
Examples:
style.v2.css
app.3f7c2a.js
logo.abc123.webp
When the filename changes, browsers download the new version immediately.
Things To Watch Out For
Long cache lifetimes work best when paired with a proper versioning strategy.
Potential issues include:
- Users seeing outdated files
- Broken layouts after updates
- Old JavaScript continuing to run
- Cache inconsistencies across devices
- Difficult troubleshooting during deployments
Without cache busting, a one-year cache duration can become a maintenance headache.
Recommended Strategy
For most modern websites:
- Cache HTML files for a short period or not at all
- Cache static assets for several months to one year
- Use versioned filenames for CSS and JavaScript
- Add the
immutable directive where appropriate
- Test cache behavior after deployments
This approach provides the performance benefits of aggressive caching while ensuring users receive updated files when changes are released.
Join The Discussion
What cache lifetimes do you use for your websites or applications? Have you implemented cache busting, versioned assets, or immutable caching strategies? Share your configurations, performance results, lessons learned, and troubleshooting tips to help others optimize static file caching effectively.