Is there a way to tell a browser to not cache a particular custom page? A custom module I'm building has a page callback that creates and Inbox listing. When a user clicks an item in the inbox, another page callback displays the message, and it updates the database to record that it's been read.

The problem is that when the user presses Back, they get back to their local cache of the inbox which suggests it has not been read. If they reload, it's OK.

Is there a way (in hook_menu?) to flag that a page should not be cached/expires immediately?
If I understand your expectation correctly, it's less of a Drupal and more of a browser thing. When the user presses the Back button, if you don't want the browser to show a copy of the web page that it might already have, the Cache-Control header in your initial response must have no-store. Drupal by default, doesn't have this. You can confirm this by checking the definition of this function.

Simply overwriting the Cache-Control header seems to fix this on Opera 16, Firefox 23 and Chrome 30. However, on Safari 6, it doesn't. Turns out that it might be a known issue. So, we might have to turn to a Javascript hack for that.

In summary, including the snippet below in your page callback should do the trick for you. I have assumed Drupal 7.

// Add no-store in Cache-Control.
drupal_add_http_header(
  'Cache-Control', 
  'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 
  FALSE
);
// Hack for Safari
drupal_add_js('jQuery(window).unload(function(){});', array('type' =&gt; 'inline'));


This seems to work on a fresh Drupal 7.22 install for an authenticated user. If the page in question has been asked to be stored in the Drupal cache or if you are using a contributed module that also messes with the response headers, this might not work as is for you.

Useful References


https://stackoverflow.com/questions/866822/why-both-no-cache-and-no-store-should-be-used-in-http-response
http://madhatted.com/2013/6/16/you-do-not-understand-browser-history
https://developer.mozilla.org/en-US/docs/HTTP_Caching_FAQ
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
https://drupal.org/node/197786#comment-1055633