zerolab
zerolab

Reputation: 836

How to serve different cached versions of a page depending on a cookie in Drupal?

The task is relatively straightforward:

A Drupal website displays a list of articles with thumbnails. Some visitors would like to view it without images by clicking on a button/link and have that preference saved.

e.g. http://patterntap.com/collections/index/

alt text

The problem is all visitors are anonymous and given certain traffic, page cache is enabled.

My idea was to use some simple JavaScript to set a cookie, refresh the page and depending on the cookie values (or its presence/absence) display or hide the images.

Except Drupal serves cached pages quite early and the only quick way to modify the cached version that I could find is by hacking includes/bootstrap.inc and add a custom class to the body classes then hide the images with css.

A very wrong approach, I know. But I wonder if there is a way to save different versions of a page and serve the correct version?

Edit:

Edit/solution: In the end went with Rimian's suggestion. But it is possible to accomplish the task using our own cache.inc implementation as seen in the Mobile Tools module. Specifically, by extending cache.inc and updating settings.php to include

$conf['page_cache_fastpath'] = FALSE;
$conf['cache_inc'] = 'path/to/my/module/my_module_cache.inc';

Upvotes: 2

Views: 691

Answers (2)

Icode4food
Icode4food

Reputation: 8694

I'm not sure I 100% understand what you are trying to do but here are my thoughts. One of your root problems is that you are trying to access what is essentially different content at the same uri.

If this is truly what you want to do, then Rimian's suggestion of checking out chache_get and chache_set may be worthwhile.

Personally, it seems cleaner to me to have your "with thumbnails" and "without thumbnails" be accessed via different uri's. Depending on exactly what you are wanting to accomplish, a GET variable my be an even better way to go. With either of these two options you would hide or show your thumbnails at the theme layer. Pages with different paths or get variables would get cached separately.

If you want the visitor to be able to switch views without a page reload, then jQuery and a cookie would probably suite your needs. This wouldn't require a page reload and switching back and forth would be quite simple.

Upvotes: 1

Rimian
Rimian

Reputation: 38418

So let me get this right. You wanna hide some images on a cached page if the user chooses to?

Why don't you write some jQuery or javascript and load that into your cached page with all the rest of the document?

Then, the client/browser would decide to run your script and hide images depending on some parameters you passed along with the request to that page or in the cookie? The script gets cached and only runs when you call it.

If you were hacking the bootstrap for something like that you'd really need to be rethinking what you were doing. Crazy! :)

Also take a look at cache_get and cache_set:

http://api.drupal.org/api/drupal/includes--cache.inc/6

Upvotes: 1

Related Questions