Magento – the reason for adding Pragma: no-cache to page requests by magento

cachecache-improvementperformance

When loading a Magento based request or page, in the headers of your request, will be a Pragma: no-cache , as well as settings in Cache-Control: for no-cache and must-revalidate.

In terms of performance, wouldn't it be more suitable allow for these settings to be done by Apache using mod_rewrite?

You could then disable it, via your web server.

This would mean loading a product based page would be loaded from the server each time, even if no new content was given.

As far as I am aware, this is defined by the file:
/app/code/core/Mage/Core/Model/Url/Rewrite/Request.php

Line 292, protected function _sendRedirectHeaders:

    /**
 * Add location header and disable browser page caching
 *
 * @param string $url
 * @param bool $isPermanent
 */
protected function _sendRedirectHeaders($url, $isPermanent = false)
{
    if ($isPermanent) {
        header('HTTP/1.1 301 Moved Permanently');
    }

    header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
    header('Pragma: no-cache');
    header('Location: ' . $url);
    exit;
}

Are there times when Pragma: No-cache should not be used?

Best Answer

In your code example the no cache is sent over a redirect. The reason why this is not cached is, if the redirect ceases to exist, you don't want the browser to keep redirecting the user anyway.

Since content in a webshop is highly dynamic (due to shopping carts, changing stock etc.) client side caching for the whole page is highly undesirable. Sending long expiration headers for images, css or any other media file is fine but the page content itself would be best left uncached. (warning: this is in my opinion. please correct me if I'm wrong)

Instead, to optimize performance, use a Full Page Cache mechanism like Varnish or a 3th party extension like Brim FPC to take away some (and in most cases a lot) of server load.

Related Topic