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:

  1. Browsers: The user local cache.
  2. 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

  1. Static Assets: For resources like images, CSS, JavaScript, fonts, and videos that rarely change. Example:
  2. Cache-Control: max-age=31536000, public
  3. 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.
  4. CDN Integration: Ensures that CDNs can cache resources effectively to reduce load on the origin server.

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>

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";
}

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.";
?>

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.');
});

Verifying Cache Behavior

  1. Using cURL: Run the following command to inspect the Cache-Control header:
  2. curl -I https://yourdomain.com/resource

    Example output:

    Cache-Control: max-age=31536000, public
  3. 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.
  4. Using Online Tools: Use tools like WebPageTest or GTmetrix to analyze caching behavior.

Best Practices for max-age with public

  1. 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.
  2. Avoid public for Sensitive Content: Do not use public for resources containing user-specific or private information.
  3. Use CDN for Delivery: Combine Cache-Control headers with a Content Delivery Network (CDN) to reduce latency and improve scalability.
  4. Test Regularly: Ensure your caching strategy aligns with your website performance goals and user needs.

Combining max-age with Other Directives

  1. immutable: Indicates the resource will not change during its lifetime, avoiding unnecessary revalidation. Example:
  2. Cache-Control: max-age=31536000, public, immutable
  3. must-revalidate: Forces revalidation after the max-age period expires. Example:
  4. Cache-Control: max-age=3600, public, must-revalidate
  5. s-maxage: Overrides max-age for shared caches (e.g., CDNs). Example:
  6. Cache-Control: max-age=600, s-maxage=3600, public

Benefits of max-age with public

  1. Improves Performance: Reduces server load and speeds up resource delivery by allowing shared caches to serve the resource.
  2. Optimizes Bandwidth Usage: Cached resources reduce redundant data transfer.
  3. 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.