magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Varnish/ESI breaks on product pages when skus have slashes

Open AirmanAJK opened this issue 9 years ago • 3 comments

Create a product with a forward slash in the sku field (ie my/product)

With Varnish and the full_page cache enabled, visit the product page.

Expected result: The page should show as normal.

Actual result: The top category nav is missing.

This is because the handles for the ESI URL are not properly encoded. Here is what is actually written in the server response where the ESI block gets replaced:

404 Not Found

Not Found

The requested URL /page_cache/block/esi/blocks/["catalog.topnav"]/handles/["default","catalog_product_view","catalog_product_view_id_6226","catalog_product_view_sku_fluke_124b/na_new","catalog_product_view_type_simple"]/ was not found on this server.


Apache/2.4.18 (Ubuntu) Server at xxxx Port 80

Notice the handle "catalog_product_view_sku_fluke_124b/na_new".

If I manually replace the slash with %252f (a double encoded forward slash), the response is what it should be: a bunch of top nav category HTML markup.

I traced the problem to \vendor\magento\module-page-cache\Observer\ProcessLayoutRenderElement.php at the function _wrapEsi().

The result of $layout->getUpdate()->getHandles() should be properly encoded to handle slashes.

However, I've had nothing but problems upon problems upon problems with the full page cache ESI nonsense. I'm just going to override this observer so it will never use edge side includes as I don't want them anyway (we only change our website once a day, and manually reindex/clear caches when we do). I'm posting this here to possibly improve the site for users who want to continue to subject themselves to the terror that is Magento Varnish ESI.

Cheers.

AirmanAJK avatar Feb 18 '17 03:02 AirmanAJK

This seemed to do the trick (url encoding each handle separately):

protected function _wrapEsi(
        \Magento\Framework\View\Element\AbstractBlock $block,
        \Magento\Framework\View\Layout $layout
    ) {
		$handles = $layout->getUpdate()->getHandles();
		$handlesEncoded = [];
		foreach($handles as $handle)
			$handlesEncoded[] = urlencode($handle);
		
        $url = $block->getUrl(
            'page_cache/block/esi',
            [
                'blocks' => json_encode([$block->getNameInLayout()]),
                'handles' => json_encode($handlesEncoded)
            ]
        );
        // Varnish does not support ESI over HTTPS must change to HTTP
        $url = substr($url, 0, 5) === 'https' ? 'http' . substr($url, 5) : $url;
        return sprintf('<esi:include src="%s" />', $url);
    }

AirmanAJK avatar Feb 18 '17 03:02 AirmanAJK

Ready to be moved to PR in progress.

mcspronko avatar Mar 03 '17 00:03 mcspronko

@AirmanAJK Fix for this issue was delivered into the develop branch with the PR #8769 Thank you for reporting.

ishakhsuvarov avatar Mar 24 '17 08:03 ishakhsuvarov