6

I just need to remove single block phtml from FPC cache. How can I do that without using cacheable = "false". Because cacheable = "false" has an effect on the whole page.

I am using a .phtml file in the static block and that static block call into header.

  • do you want to on product page? – Sunil Patel Feb 19 '18 at 11:59
  • no , i call this block on header.phtml so it means use for all page – Camit1dk Feb 19 '18 at 12:02
  • then you need to make header.container to cache false – Sunil Patel Feb 19 '18 at 13:45
  • Sunil, how can i make this ? is it not affect whole pages ? – Camit1dk Feb 19 '18 at 15:27
  • Non-cacheable elements are rendered, in Magento 2, using knockout.js. The best practice is to implement the functionality from the phtml file into a knockout.js template... Otherwise, I'm not sure you can disable cache for that specific block. – Alex Constantinescu Feb 24 '18 at 11:56
0

You can create a preference or a plugin on the block class to override the block cache functions: getCacheLifetime(), hasCacheLifetime(), get/hasCacheTags(), has/getCacheKeyInfo(), getData().

If getCacheLifetime returns null than it will disable current block's cache.

Take a look at the block class to see which cache methods it uses and search in parent block classes (Abstract, Template, ..) to see which function you need to override in your plugin/preference class.

cacheable = "false" has only one meaning. It will disable full page cache type on that page type / action.

If there is something wrong with the parameters that form cache key info, than you can vary block cache by overriding getCacheKeyInfo function.

If there is customer specific data inside that block than you can't use either block cache or have it in full page cache. The only viable solution is to use AJAX. Magento 2 has private content: http://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching/private-content.html

Other solution is to use AJAX requests to fill parts of pages like in product page reviews and other. https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Review/view/frontend/web/js/process-reviews.js

The last and the least is the XML layout declaration ttl="3600". When Magento 2 is configured to work with Varnish, it will create am ESI policy for the output of the block. Something similar does when it uses builtin page cache instead of Varnish. Magento uses this type of cache hole punching in one place: in top menu. I personally don't recommend using "ttl" cache hole punching because it adds an extra request to the server. If you use it too many times on a page than it may decrease performance.

  • I just want to one phtml remove from the cache . and this html file call into header container. any idea please let me know – Camit1dk Feb 22 '18 at 10:34
  • Try to do a plugin on the block class that sets the block not to use cache. In the plugin the function aroundCacheLifetime should return null. Otherwise if it contains customer session data than implement the block/template with private content (section.xml, data provider class and a small knockout JS component). It's easier than it looks. There is an easy and good example here: devdocs.magento.com/guides/v2.1/extension-dev-guide/cache/… – obscure Feb 22 '18 at 11:33
0

The best approach is to handle the block content via javascript.

Depending on what you want to achieve you can use

Public Content

or

Private Content

You can get more information on http://devdocs.magento.com/guides/v2.0/extension-dev-guide/cache/page-caching.html

Feel free to add any related questions in the comments.

  • I just want to one phtml remove from the cache . and this html file cal into header container. any idea please let me know – Camit1dk Feb 22 '18 at 10:28
0

You can achieve this by two methods.

1) You have to implement a hole punching functionality for a specific block.
-> I read this article about creating a hole punching by using identities https://trellis.co/blog/magento2-fpc-hole-punching/ so you can check that link, it might be useful or you can buy any third party extension amasty fpc,mirasvit fpc.

2) Other solution, you can display that particular content part by ajax after the whole page load or during.

0

make your most of the code in javascript

even if you are getting some array objects from php give it to javascript variable then put your logic in javascript, append prepend the html with javascript . if you can provide some snippet code from .phtml file which was late you think for disabling cache, it will be helpfull. i did same thing in my phtml file, where caching issue was very problematic.

-5

Simple insert param cacheable in the block:

<block class="Block\Class" name="blockname" cacheable="false" />

  • This will disable cache of that block, not disable cache of the whole page. – Minh Feb 22 '18 at 4:10
  • 1
    minh, Are you sure that its not disable cache for whole page ? – Camit1dk Feb 22 '18 at 6:10
  • sure, I've tested. Sometimes, we do this for user profile page without cached. So you can give it a try. – Minh Feb 22 '18 at 7:35
  • 1
    Minh , I Tried that it not working , whole page remove from cache after using this – Camit1dk Feb 22 '18 at 9:31
  • 1
    Do not use this! This will disable caching for the whole page. Which in your case would fully disable FPC on every page the header is used. – Ben Crook Feb 22 '18 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.