magneto-varnish icon indicating copy to clipboard operation
magneto-varnish copied to clipboard

Prevent cache after POST request redirection

Open lphilippo opened this issue 12 years ago • 1 comments

Hello,

I have been using this module for a while and there remains one issue that I couldn't solve yet.

On each page of my site I have a Newsletter signup form. After submitting this form, a POST request is performed to "/newsletter/subscriber/new" and then followed by a GET request back to the current page.

As obviously I do not want to prevent caching for each page where this form is available, I unfortunately cannot recognize this exact scenario in my VCL file. The "302 Moved Temporarily" header, which the POST request returns, causes that the Referer value for the final GET request is actually the current page URL instead of "/newsletter/subscriber/new" (which I hoped for).

Do you have any suggestions? Although I do understand that it's probably more a Varnish question, the only (non-ideal) alternatives I can think of is setting a COOKIE with a very short lifetime or adding a no-cache GET parameter to the URL.

Thanks

lphilippo avatar Apr 29 '12 10:04 lphilippo

The solution turned out to be easier than I thought. The changes are below, instead of a pull request.

In Model/Observer.php multiple scenarios are excluded, the last check has been added:

if ($helper->quoteHasItems() || $helper->isCustomerLoggedIn() || $helper->hasCompareItems() || $helper->hasMessages()) {

In Data/Cacheable.php the related function is defined as:

public function hasMessages() {
    $messageCount = Mage::getSingleton('core/session')->getMessages(FALSE)->count();
    return ($messageCount > 0);
}

Indeed the nocache COOKIE is shortly set (by the POST request) and then removed by the following GET request.

lphilippo avatar Apr 29 '12 13:04 lphilippo