Catalyst - Customizing the view to stop IE from caching JSON
Posted in json, catalyst, ie, ajax Tue, 25 Jul 2006 15:49:00 GMT
Often times you'll want to customize your response specific to the View being used, e.g. setting extra headers. This can be done directly in your View class by creating a process method. All View base classes have a process method defined in Catalyst::View that gets called at rendering time. By adding a process method in your subclass and redispatching to the parent you can do some preliminary processing.
One use is preventing IE from caching JSON responses by setting the Cache-Control and Pragma HTTP response headers. You can do this in the application's JSON view as follows:
package MyApp::View::JSON; use strict; use warnings; use base qw/Catalyst::View::JSON/; sub process { my $self = shift; my $c = $_[0]; $c->res->header( 'Cache-Control' => 'no-store, no-cache, must-revalidate,'. 'post-check=0, pre-check=0, max-age=0' ); $c->res->header( 'Pragma' => 'no-cache' ); $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' ); $self->NEXT::process(@_); } 1;
Your process method will get called, set it's response headers and then use NEXT to call C::V::JSON's process method.