<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Dev411 Blog: Catalyst - Customizing the view to stop IE from caching JSON</title>
    <link>http://www.dev411.com/blog/2006/07/25/catalyst-customizing-the-view-stop-ie-from-caching-json</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>John Wang on Technology</description>
    <item>
      <title>Catalyst - Customizing the view to stop IE from caching JSON</title>
      <description>&lt;p&gt;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 &lt;span class="fix"&gt;process&lt;/span&gt; method. All View base classes have a process method defined in &lt;span class="fix"&gt;Catalyst::View&lt;/span&gt; that gets called at rendering time. By adding a &lt;span class="fix"&gt;process&lt;/span&gt; method in your subclass and redispatching to the parent you can do some preliminary processing.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;pre&gt;package MyApp::View::JSON;
use strict; use warnings;
use base qw/Catalyst::View::JSON/;

sub process {
  my $self = shift;
  my $c = $_[0];
  $c-&gt;res-&gt;header( 'Cache-Control' =&gt;
    'no-store, no-cache, must-revalidate,'.
    'post-check=0, pre-check=0, max-age=0'
  );
  $c-&gt;res-&gt;header( 'Pragma' =&gt; 'no-cache' );
  $c-&gt;res-&gt;header( 'Expires' =&gt; 'Thu, 01 Jan 1970 00:00:00 GMT' );
  $self-&gt;NEXT::process(@_);
}

1;&lt;/pre&gt;

&lt;p&gt;Your &lt;span class="fix"&gt;process&lt;/span&gt; method will get called, set it's response headers and then use &lt;a href="http://search.cpan.org/~dconway/NEXT-0.60/lib/NEXT.pm" class="fix"&gt;NEXT&lt;/a&gt; to call C::V::JSON's &lt;span class="fix"&gt;process&lt;/span&gt; method.&lt;/p&gt;</description>
      <pubDate>Tue, 25 Jul 2006 10:49:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:885f059fcc2a7604158abbca1b8b8be6</guid>
      <author>John Wang</author>
      <link>http://www.dev411.com/blog/2006/07/25/catalyst-customizing-the-view-stop-ie-from-caching-json</link>
      <category>json</category>
      <category>catalyst</category>
      <category>ie</category>
      <category>ajax</category>
    </item>
  </channel>
</rss>
