1

I tried this link link

Not working for me.

Any other way to achieve the hole punching.

  • 1
    @H_Parekh.please check user question again? – Amit Bera Jun 11 '18 at 12:42
  • @H_Parekh The link you provide is for Magento 1. The OP is asking about Magento 2 – ProcessEight Jun 11 '18 at 12:52
  • Yes, I tried in Magento 2. – Renga Jun 11 '18 at 12:53
  • 1
    You are asking about hole punching and answering about implement cache – Shoaib Munir Mar 19 at 6:11
2

Finally, I found the solution for this.

Magetno 2 + Varnish = dynamic content

We can achieve hole punch without using

  1. Cacheable false
  2. isPrivateScope
  3. Ajax

In getVaryString method from Magento\Framework\App\Http\Context.

public function getVaryString()
{
    $data = $this->getData();
    if (!empty($data)) {
        if (!empty($this->cookieManager->getCookie('type_po')) && $this->cookieManager->getCookie('type_po') == 1) {
                array_push($data, "type", 'access_po');
        }
            ksort($data);
            return sha1(serialize($data));
    }
    return null;
}

Method return vary string to be used as a part of page cache identifier.

If The Page Cache Identifier already exists, the page render from varnish cache otherwise page rendering newly.

1

I feel, the answer whatever you have posted is not relevant to your question. Mainly Hole punching is used to make some specific block to show non-cached information which is part of cached page.

Please be clear on your question & answer.

And you can check "sections on magento 2" for achieving hole-punching functionalities.

0

After going crazy to find a solution for it, finally I got a working solution for Magento 2.3.2 + Varnish + GeoIP. To accomplish it you can use the Cache Context and Page Variations which are very simple to implement.

First create a Plugin into your module for Magento\Framework\App\Http\Context.

File: app/code/Vendor/YourModule/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\Http\Context">
        <plugin name="atwix_country_cache_context_plugin"
            type="Vendor\YourModule\Plugin\CustomerCountryCacheContextPlugin" />    
    </type>
</config>

Then the Plugin implementation

File: Vendor/YourModule/Plugin/CustomerCountryCacheContextPlugin.php

/**
 * Plugin on \Magento\Framework\App\Http\Context
 */
class CustomerCountryCacheContextPlugin
{
    public function __construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;
    }
    /**
     * \Magento\Framework\App\Http\Context::getVaryString is used by Magento to retrieve unique identifier for selected context,
     * so this is a best place to declare custom context variables
     */
    function beforeGetVaryString(\Magento\Framework\App\Http\Context $subject)
    {
        $country = $this->yourWayToGetTheCountry();
        $defaultCountryContext = 'Brazil';
        $subject->setValue('CONTEXT_AGE', $country, $defaultCountryContext);
    }
}

And the last step is to add the X-Magento-Vary cookie to transfer context on the HTTP layer in your Varnish.

sub vcl_hash {
    if (req.http.cookie ~ "X-Magento-Vary=") {
        hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
    }
    ... more ...
}

For more info visit:

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching/public-content.html#configure-page-variations

https://www.atwix.com/magento-2/cache-context-and-page-variations-in-magento-2/

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.