Magento – how to set no-cache HTTP headers in controller in magento 2

full-page-cachehttp-headermagento2session

I have read in Magento 2 Dev Docs for Public content we can set no-cache HTTP headers for Controllers that don’t use layouts(last line in page).

Controllers that don’t use layouts should set no-cache HTTP headers.

I have a controller which use data from session object and when we enable FPC it doesn't work.

So I'm thinking to set no-cache HTTP headers for that controller may get data from the session object.

Let me know if I'm wrong somewhere.

Thanks

Best Answer

If you look in

vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor

Line 85 onwards:

$response = $this->_responseFactory->create();
$response->setRedirect($redirectUrl, $redirectCode);
$response->setNoCacheHeaders();
return $response;

That may help. Good luck!

Related Topic