Cache Exclude
A simple module that allows administrators to selectively exclude specific paths and content types from Drupal's page cache.
cacheexclude
Install
composer require 'drupal/cacheexclude:^2.1'
composer require 'drupal/cacheexclude:^2.0'
composer require 'drupal/cacheexclude:8.x-1.0'
Overview
Cache Exclude provides a straightforward solution for managing page caching exceptions in Drupal. While Drupal's page cache improves performance by caching pages for anonymous users, some pages with dynamic, random, or rotating content need to bypass this cache to function correctly.
This module allows site administrators to define which pages should never be cached, either by specifying URL paths (with wildcard support) or by selecting content types. When a visitor accesses an excluded page, the module uses Drupal's page cache kill switch to prevent caching, ensuring that dynamic content remains truly dynamic.
The module integrates seamlessly with Drupal's caching system and includes migration support for sites upgrading from Drupal 7.
Features
- Exclude specific URL paths from page caching using a simple textarea interface
- Support for wildcard patterns in path exclusions (e.g., /blog/* to exclude all blog subpages)
- Exclude entire content types from page caching with checkbox selection
- Works with both internal paths and path aliases
- Uses Drupal's native page cache kill switch for reliable cache bypassing
- Includes Drupal 7 to Drupal 9/10/11 migration support for seamless upgrades
- Automatically flushes all caches when settings are saved to ensure immediate effect
Use Cases
Excluding pages with dynamic content blocks
If your front page displays a random quote, featured product, or rotating banner that changes on each page load, you can add <front> to the exclusion list. This ensures anonymous visitors always see fresh content rather than a cached version.
Excluding personalized landing pages
For marketing campaigns with personalized content based on URL parameters or server-side logic, add the landing page paths (e.g., /landing/*) to prevent caching and ensure each visitor sees the intended personalized content.
Excluding event or news content types
If your Article or Event content type frequently displays time-sensitive information or real-time data, select these content types in the settings to ensure all nodes of that type are never cached.
Excluding pages with third-party integrations
Pages that embed real-time feeds, stock tickers, or other dynamic third-party content can be excluded by path to ensure the embedded content is always current.
Migrating from Drupal 7
When upgrading from Drupal 7, the module automatically migrates your existing cache exclusion settings. Run the cacheexclude_settings migration to transfer your path and node type exclusions to the new site.
Tips
- Use path aliases in the exclusion list when possible, as the module checks both internal paths and aliases for matches
- The front page can be excluded using the special <front> token rather than its actual path
- Excluding too many pages can negatively impact site performance - only exclude pages that truly need dynamic content
- For development and debugging, check the X-Drupal-Cache response header: 'UNCACHEABLE (response policy)' indicates the page was excluded from cache
- When migrating from Drupal 7, ensure the d7_node_type migration runs before cacheexclude_settings to properly map content types
Technical Details
Admin Pages 1
/admin/config/system/cacheexclude
Configure which pages and content types should be excluded from Drupal's page cache. Pages matching the configured criteria will not be cached for anonymous users, allowing dynamic content to function properly.
Troubleshooting 4
Ensure you are testing as an anonymous user (logged-out). Page cache only applies to anonymous users. Also verify that the page cache module is enabled and that the path format is correct (must start with a leading slash).
Ensure you're using the correct wildcard syntax. Use * as a wildcard (e.g., /blog/* matches /blog/post-1, /blog/post-2, etc.). The pattern must match either the internal path or the path alias.
Verify that the node is actually of the content type you selected. Check the content type machine name in the node edit form or via Drush. Also ensure the cacheexclude module is enabled and the settings were saved.
Cache Exclude automatically flushes all caches when settings are saved. If changes still don't take effect, manually clear caches using drush cr or the admin UI at /admin/config/development/performance.
Security Notes 3
- Cache exclusion settings require the 'administer site configuration' permission, which is a powerful administrative permission
- Excluding pages from cache may expose the site to increased server load from malicious requests - use exclusions judiciously
- The module does not introduce any security vulnerabilities but reducing cache coverage may make the site more susceptible to traffic spikes