I'm developing a modal that allows the user to select a car model, and, when it is selected, it loads the different variations of rims that the user can fit in their car. It is working as intended so far. If the car is set, it gets saved on the cookies, with all the possible varations, and with that we avoid having to make an Ajax call everytime we load a page.
When we are in the product page, everything works fine. If there is a cookie with the possible varations, that gets loaded in the modal, and if we make an Ajax call, the variations get replaced with the ones of the new selected car.
However, when I am in the home page, the modal doesn't work properly. If there is a cookie with the possible varations, it gets ignored, and whenever I reload the home page, the car remains the first one which was selected after clearing the cache. If I clear the cache, it uses the cookie with the variations, but never changes the values, no matter what car we select after that.
I tried to do this to disable cache, with no success:
app/code/Vendor/Module/view/frontend/layout/default.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:View/Layout:etc/page_configuration.xsd"> <head> <css src="Vendor_Module::css/configCar.css" media="all"/> </head> <body> <referenceContainer name="main"> <block class="Vendor\Module\Block\ConfigCar" template="config-car.phtml" cacheable="false"/> </referenceContainer> </body>
And in the Block I added this:
app/code/Vendor/Module/Block/ConfigCar.php
public function getCacheLifetime(){ return null; }
But it's not working, it remains cached. Any suggestions on what can I do? I run out of ideas.
Thanks in advance!
Hello @mohsinkhanf66a ,
Maybe it's a browser cache ? or Varnish cache ?
if you are using Varnish, ignore the home page being cached.
just add some string parameter "?test" in your home page URL and check if the value changes. if so, ignore your homepage to be cached for browser.
Let me know the results. Hope it helps !
Solution 1 :
In your module layout file where you are called you template add cacheable="false" tag so you template content will not be cached .
<block class="Vendor\Module\Block\YourBlockFile" name="name of your block" template='your_template_file_name' cacheable="false"/>
Solution 2 :
Add getCacheLifetime() in your block
For example this phtml file call in cms home page
{{block class="Vendor\Module\Block\Index" template="Vendor_Module::myfile.phtml"}}
In your block
<?php namespace Vendor\Module\Block; class Index extends \Magento\Framework\View\Element\Template { public function __construct( ... \Magento\Framework\View\Element\Template\Context $context, ... ) { parent::__construct($context); } public function getCacheLifetime() { return null; } }
If you call phtml file with layout.xml you can disable cache by cacheable="false"
<block class="Vendor\Module\Block\Index" name="myblock" template="Vendor_Module::myfile.phtml" cacheable="false"/>
@MyLowesLife wrote:Hello @mohsinkhanf66a ,
Maybe it's a browser cache ? or Varnish cache ?
if you are using Varnish, ignore the home page being cached.
just add some string parameter "?test" in your home page URL and check if the value changes. if so, ignore your homepage to be cached for browser.
Let me know the results. Hope it helps !
Thank you for the Information, I will try to figure it out for more. And I will definitely Click on Kudos once i implement this and solve the problem.
Maybe it's a browser cache ? or Varnish cache ? If you are using Varnish, ignore the home page being cached. myccpay