Skip to content

Commit 49bc150

Browse files
committed
MAGETWO-91388: Enable caching of AJAX requests on Varnish/Fastly
1 parent 80469a6 commit 49bc150

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

app/code/Magento/Swatches/Controller/Ajax/Media.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,26 @@ class Media extends \Magento\Framework\App\Action\Action
2424
*/
2525
private $swatchHelper;
2626

27+
/**
28+
* @var \Magento\PageCache\Model\Config
29+
*/
30+
protected $config;
31+
2732
/**
2833
* @param Context $context
2934
* @param \Magento\Catalog\Model\ProductFactory $productModelFactory
3035
* @param \Magento\Swatches\Helper\Data $swatchHelper
36+
* @param \Magento\PageCache\Model\Config $config
3137
*/
3238
public function __construct(
3339
Context $context,
3440
\Magento\Catalog\Model\ProductFactory $productModelFactory,
35-
\Magento\Swatches\Helper\Data $swatchHelper
41+
\Magento\Swatches\Helper\Data $swatchHelper,
42+
\Magento\PageCache\Model\Config $config
3643
) {
3744
$this->productModelFactory = $productModelFactory;
3845
$this->swatchHelper = $swatchHelper;
46+
$this->config = $config;
3947

4048
parent::__construct($context);
4149
}
@@ -48,14 +56,22 @@ public function __construct(
4856
public function execute()
4957
{
5058
$productMedia = [];
59+
60+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
61+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
62+
63+
/** @var \Magento\Framework\App\ResponseInterface $response */
64+
$response = $this->getResponse();
65+
5166
if ($productId = (int)$this->getRequest()->getParam('product_id')) {
5267
$productMedia = $this->swatchHelper->getProductMediaGallery(
5368
$this->productModelFactory->create()->load($productId)
5469
);
70+
$resultJson->setHeader('X-Magento-Tags', 'catalog_product_' . $productId);
71+
72+
$response->setPublicHeaders($this->config->getTtl());
5573
}
5674

57-
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
58-
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
5975
$resultJson->setData($productMedia);
6076
return $resultJson;
6177
}

app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,12 +1029,14 @@ define([
10291029
mediaCallData.isAjax = true;
10301030
$widget._XhrKiller();
10311031
$widget._EnableProductMediaLoader($this);
1032-
$widget.xhr = $.get(
1033-
$widget.options.mediaCallback,
1034-
mediaCallData,
1035-
mediaSuccessCallback,
1036-
'json'
1037-
).done(function () {
1032+
$widget.xhr = $.ajax({
1033+
url: $widget.options.mediaCallback,
1034+
cache: true,
1035+
type: 'GET',
1036+
dataType: 'json',
1037+
data: mediaCallData,
1038+
success: mediaSuccessCallback
1039+
}).done(function () {
10381040
$widget._XhrKiller();
10391041
});
10401042
}

0 commit comments

Comments
 (0)