3

I have a custom controller for infinite scroll and filtered attributes which simply returns a json response. When modifying a category or product these cached responses are not cleared however.

Looking at the varnish headers the below header is missing for my responses:

X-Magento-Tags: store,cms_b,cat_c,cat_c_79,cat_c_109,cat_c_143,cat_c_182,cat_c_702,cat_c_257,cat_c_304,cat_c_342,cat_c_521,cat_c_533,cms_b_print_header,cms_b_usp,cms_b_social_links,cms_b_payment_methods,cat_c_202,cat_c_p_202,cat_p_17803,cat_p,cat_p_10755

I believe these are how Magento knows to purge from Varnish cache when modifications are made to any of above however am not sure how to make sure these are set within my controller?

There seems to be 2 files I can currently identify that seem to modify this header:

Magento\PageCache\Model\Controller\Result\BuiltinPlugin
Magento\PageCache\Model\Layout\LayoutPlugin

The layoutplugin class seems to just be related to blocks currently on page to add tags however neither seems to be present in response.

I can set the tags like below:

$result->setHeader('X-Magento-Tags', implode(',', $tags));

However need to know an easy way of getting the appropriate tags if possible for that category and products present on that page or how to just get the default method of setting these to kick into action here.

1 Answer 1

1

I'd say this really depends on your controller. \Magento\PageCache\Model\Layout\LayoutPlugin::afterGetOutput, which only works for layout responses, gives a very good idea: It combines all "identities" / cache tags from all blocks present on the page.

I also found \Magento\Swatches\Controller\Ajax\Media::execute pretty interesting: It returns JSON data for a specific product and sets the X-Magento-Tags like this:

$resultJson->setHeader('X-Magento-Tags', implode(',', $product->getIdentities()));

In your case, since your response seems to relate to one specific category, it might be enough to use $category->getIdentities().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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