Reuse Cached 404s

Installs: 
1
Open Bugs: 
0
Development Status: 
Seeking co-maintainer(s)
Under active development

Drupal has a 404 caching problem.

Drupal's page cache is a fantastic and simple way to improve performance in production. If an anonymous user goes to http://yoursite.com/somepage twice, the second page load is a lot faster because Drupal doesn't spend time re-rendering it; it simply serves it from the cache.

But the problem is that pages are cached by their path. That means that the server has to do all the work to render the 404 page each time a new path is not found. This can be particularly noticeable if your 404 page is a node, view, or other rendered page.

This module takes over the caching process when a page is not found and makes it a little smarter. It's been tested with nodes, but it should work with any 404 page that is served through the menu system. It is compatible with multilingual sites. It caches the 404 once per language.

If you are already using Fast 404 (or Drupal 7's built-in version), you don't really need this module, and compatibility with those configurations hasn't been tested. It's a good alternative though when you need 404 pages with some dynamic content from your site and don't want to have to update HTML files when your site structure changes.

Installation

This module, if enabled, relies on the same setting as the page cache (http://yoursite.com/admin/config/development/performance, "Cache pages for anonymous users").

  1. Download and enable the module
  2. Ensure the page cache is enabled

To clear Reuse Cached 404s's own cache, clear the cache as normal. It uses the cache_page table as well.

A slightly more technical explanation

Reuse Cached 404s intercepts the page delivery process using hook_page_delivery_callback_alter().

If it detects a 404 response (in other words, if $page_callback_result is MENU_NOT_FOUND), one of two things happen:

  • If this is the first 404 in this language, it caches it in a place it will be able to find it later and in the normal Drupal location (so that it will still be served from cache normally if it's visited again)
  • If it's a new path that is 404'ing but the module has a cached 404 in the same language, it copies that cached copy to the normal Drupal location and then serves it.

Cached 404s are keyed by language. The $cid is "reuse_cached_404:404:{$language->language}"

Sponsor

Sponsored by Project Ricochet.

Releases: 
7.x-1.0
7.x-1.x-dev
Release Dates: 
09/16/2013
10/01/2013