The Cache-Control header directives no-store and no-cache are used to control how and when content is cached. These directives are typically paired with max-age to fine-tune caching behavior for sensitive or dynamic content.
Key Differences Between no-store and no-cache
- no-store:
- Prevents caching entirely.
- The resource is not stored in any cache (browser or intermediary) and must always be fetched directly from the server.
- Best for highly sensitive content, such as login pages, financial transactions, or medical records.
- no-cache:
- Allows caching but requires the client to revalidate the resource with the server before using it.
- Best for dynamic content that changes frequently but can still benefit from conditional requests.
When to Use max-age with no-store or no-cache
With no-store
- Use no-store when the content is too sensitive to be cached or stored.
- Pairing it with max-age is redundant, as no-store overrides max-age.
- Example:
- Typical use cases:
- Login pages.
- One-time tokens or session data.
- Highly sensitive API responses.
With no-cache
- Use no-cache for dynamic content that requires revalidation before being served from the cache.
- Pair it with max-age=0 to explicitly state that the resource expires immediately and must be validated before use.
- Example:
- Typical use cases:
- Frequently updated data (e.g., stock prices or news).
- HTML content that might vary by user.
Examples
Login Page (No Caching Allowed)
For a login page where caching is prohibited:
Dynamic API Response
For an API response that requires revalidation:
Content That Expires in 5 Minutes
For a resource that is valid for 5 minutes but requires revalidation afterward:
How to Set These Headers
In Apache
Add the following to your .htaccess file or Apache configuration:
For no-store:
<FilesMatch "\.(html|php)$">
Header set Cache-Control "no-store"
</FilesMatch>
</IfModule>
For no-cache:
<FilesMatch "\.(html|php)$">
Header set Cache-Control "max-age=0, no-cache"
</FilesMatch>
</IfModule>
Restart Apache:
In Nginx
Modify the Nginx configuration to set the headers:
For no-store:
add_header Cache-Control "no-store";
}
For no-cache:
add_header Cache-Control "max-age=0, no-cache";
}
Reload Nginx:
In PHP
Set caching headers dynamically for PHP-generated content:
For no-store:
header("Cache-Control: no-store");
?>
For no-cache:
header("Cache-Control: max-age=0, no-cache");
?>
In Node.js (Express Framework)
Set headers dynamically in your Node.js application:
For no-store:
res.set('Cache-Control', 'no-store');
res.send('Sensitive data with no caching allowed.');
});
For no-cache:
res.set('Cache-Control', 'max-age=0, no-cache');
res.send('Dynamic content requiring revalidation.');
});
Verifying Cache-Control Behavior
Using cURL
Run the following command to check the headers:
Expected Output:
or:
Using Browser Developer Tools
- Open Developer Tools (F12).
- Go to the Network tab.
- Reload the page and inspect the headers.
Using Online Tools
- Use tools like WebPageTest or GTmetrix to analyze caching behavior.
Best Practices
- Use no-store for Sensitive Data: Prevent sensitive information from being cached anywhere.
- Use no-cache for Frequently Updated Content: Combine with validation headers like ETag or Last-Modified to reduce unnecessary downloads.
- Pair with HTTPS: Always serve sensitive or dynamic content over HTTPS to ensure secure transmission.
- Monitor Cache Behavior: Regularly test headers to ensure they are working as intended.
Example Scenarios
| Scenario | Header | Notes |
|---|---|---|
| Login Page | Cache-Control: no-store | Prevents caching entirely. |
| Search Results | Cache-Control: max-age=0, no-cache | Forces revalidation before serving from cache. |
| One-Time Token | Cache-Control: no-store | Tokens should never be cached for security reasons. |
| Frequently Updated API Responses | Cache-Control: max-age=0, no-cache | Ensures API clients fetch the latest version. |
By understanding and using Cache-Control: max-age with no-store or no-cache, you can precisely control how and when your content is cached, ensuring security and performance.


