The combination of Cache-Control: max-age=<seconds>, public is used to specify how long a resource should be cached and make it accessible for caching by all caches, including:
- Browsers: The user local cache.
- Intermediaries: Shared caches like Content Delivery Networks (CDNs), reverse proxies, and ISPs.
What Does It Do?
- max-age=<seconds>: Defines how long (in seconds) the resource can be cached before it is considered stale. Example: max-age=3600 means the resource can be cached for 1 hour.
- public: Indicates the resource is cacheable by any cache, including shared caches (e.g., CDN, proxy servers).
When to Use max-age with public
- Static Assets: For resources like images, CSS, JavaScript, fonts, and videos that rarely change. Example:
- Non-Sensitive Dynamic Content: For dynamic content that can be safely cached by intermediaries and browsers. Example: API responses or shared dashboards with public data.
- CDN Integration: Ensures that CDNs can cache resources effectively to reduce load on the origin server.
Cache-Control: max-age=31536000, public
Examples of max-age with public
| Content Type | Header Example | Notes |
|---|---|---|
| Static Images | Cache-Control: max-age=31536000, public | Cache for 1 year (typically versioned assets). |
| CSS and JavaScript | Cache-Control: max-age=86400, public | Cache for 1 day (frequent updates). |
| Static HTML Pages | Cache-Control: max-age=3600, public | Cache for 1 hour (short-lived, infrequent updates). |
| API Responses (Public Data) | Cache-Control: max-age=600, public | Cache for 10 minutes. |
How to Set Cache-Control: max-age=<seconds>, public
In Apache
Add the following to your .htaccess file or Apache configuration to set cache headers for specific file types:
<IfModule mod_headers.c>
<FilesMatch "\.(jpg|jpeg|png|gif|svg|css|js|woff|woff2)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
</IfModule>
<FilesMatch "\.(jpg|jpeg|png|gif|svg|css|js|woff|woff2)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
</IfModule>
Restart Apache to apply changes:
sudo systemctl restart apache2
In Nginx
Add the cache headers to your Nginx configuration file for specific file types:
location ~* \.(jpg|jpeg|png|gif|svg|css|js|woff|woff2)$ {
add_header Cache-Control "max-age=31536000, public";
}
add_header Cache-Control "max-age=31536000, public";
}
Reload Nginx:
sudo systemctl reload nginx
In PHP
For dynamically generated resources, set the headers programmatically using PHP:
<?php
// Cache for 1 hour
header("Cache-Control: max-age=3600, public");
// Content
echo "This resource is cached for 1 hour.";
?>
// Cache for 1 hour
header("Cache-Control: max-age=3600, public");
// Content
echo "This resource is cached for 1 hour.";
?>
In Node.js (Express Framework)
Set cache headers for API responses or dynamic content:
app.get('/public-resource', (req, res) => {
res.set('Cache-Control', 'max-age=3600, public');
res.send('This is a publicly cacheable resource.');
});
res.set('Cache-Control', 'max-age=3600, public');
res.send('This is a publicly cacheable resource.');
});
Verifying Cache Behavior
- Using cURL: Run the following command to inspect the Cache-Control header:
- Browser Developer Tools:
- Open Developer Tools (F12) in your browser.
- Go to the Network tab.
- Reload the page and inspect the response headers for the resource.
- Using Online Tools: Use tools like WebPageTest or GTmetrix to analyze caching behavior.
curl -I https://yourdomain.com/resource
Example output:
Cache-Control: max-age=31536000, public
Best Practices for max-age with public
- Version Static Resources: Use versioned filenames (e.g., style.v1.css or app.v2.js) to force cache refresh when the file is updated. Example: Cache-Control: max-age=31536000, public — Even if cached for 1 year, a new version (e.g., style.v2.css) invalidates the old one.
- Avoid public for Sensitive Content: Do not use public for resources containing user-specific or private information.
- Use CDN for Delivery: Combine Cache-Control headers with a Content Delivery Network (CDN) to reduce latency and improve scalability.
- Test Regularly: Ensure your caching strategy aligns with your website performance goals and user needs.
Combining max-age with Other Directives
- immutable: Indicates the resource will not change during its lifetime, avoiding unnecessary revalidation. Example:
- must-revalidate: Forces revalidation after the max-age period expires. Example:
- s-maxage: Overrides max-age for shared caches (e.g., CDNs). Example:
Cache-Control: max-age=31536000, public, immutable
Cache-Control: max-age=3600, public, must-revalidate
Cache-Control: max-age=600, s-maxage=3600, public
Benefits of max-age with public
- Improves Performance: Reduces server load and speeds up resource delivery by allowing shared caches to serve the resource.
- Optimizes Bandwidth Usage: Cached resources reduce redundant data transfer.
- Enhances User Experience: Faster page loads improve retention and satisfaction.
By setting Cache-Control: max-age=<seconds>, public for your resources, you can efficiently leverage caching mechanisms to boost performance and scalability.


