-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from Inchoo/feature/mark-esi-documents
Marke the pages with embedded ESI with X-Esi header
- Loading branch information
Showing
5 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Fastly\Cdn\Observer; | ||
|
||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\App\ResponseInterface as Response; | ||
use Fastly\Cdn\Model\Config as FastlyConfig; | ||
|
||
class MarkEsiBlock implements ObserverInterface | ||
{ | ||
/** | ||
* @var FastlyConfig | ||
*/ | ||
private $fastlyConfig; | ||
|
||
/** | ||
* @var Response | ||
*/ | ||
private $response; | ||
|
||
/** | ||
* MarkEsiPage constructor. | ||
* @param Response $response | ||
* @param FastlyConfig $fastlyConfig | ||
*/ | ||
public function __construct( | ||
Response $response, | ||
FastlyConfig $fastlyConfig | ||
) { | ||
$this->fastlyConfig = $fastlyConfig; | ||
$this->response = $response; | ||
} | ||
|
||
/** | ||
* Set x-esi header on ESI response request | ||
* If omitted, causes issues with embedded esi tags | ||
* | ||
* @param \Magento\Framework\Event\Observer $observer | ||
* @return void | ||
*/ | ||
public function execute(Observer $observer) // @codingStandardsIgnoreLine - required, but not needed | ||
{ | ||
if ($this->fastlyConfig->isFastlyEnabled() != true) { | ||
return; | ||
} | ||
|
||
// If not set, causes issues with embedded ESI | ||
$this->response->setHeader("x-esi", "1"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Fastly\Cdn\Observer; | ||
|
||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\View\Element\AbstractBlock; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\App\ResponseInterface as Response; | ||
use Magento\PageCache\Model\Config; | ||
use Fastly\Cdn\Model\Config as FastlyConfig; | ||
|
||
class MarkEsiPage implements ObserverInterface | ||
{ | ||
/** | ||
* Application config object | ||
* | ||
* @var \Magento\PageCache\Model\Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var FastlyConfig | ||
*/ | ||
private $fastlyConfig; | ||
|
||
/** | ||
* @var Response | ||
*/ | ||
private $response; | ||
|
||
/** | ||
* MarkEsiPage constructor. | ||
* @param Config $config | ||
* @param Response $response | ||
* @param FastlyConfig $fastlyConfig | ||
*/ | ||
public function __construct( | ||
Config $config, | ||
Response $response, | ||
FastlyConfig $fastlyConfig | ||
) { | ||
$this->config = $config; | ||
$this->fastlyConfig = $fastlyConfig; | ||
$this->response = $response; | ||
} | ||
|
||
/** | ||
* Set x-esi header if block contains ttl attribute | ||
* | ||
* @param Observer $observer | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
if ($this->fastlyConfig->isFastlyEnabled() != true) { | ||
return; | ||
} | ||
|
||
$event = $observer->getEvent(); | ||
$name = $event->getElementName(); | ||
|
||
/** @var \Magento\Framework\View\Layout $layout */ | ||
$layout = $event->getLayout(); | ||
|
||
/** @var AbstractBlock $block */ | ||
$block = $layout->getBlock($name); | ||
|
||
if ($block instanceof AbstractBlock) { | ||
$blockTtl = $block->getTtl(); | ||
if (isset($blockTtl)) { | ||
// This page potentially has ESIs so as a first cut let's mark it as such | ||
$this->response->setHeader("x-esi", "1"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
<event name="core_layout_render_element"> | ||
<observer name="fastly_mark_esi_page" instance="Fastly\Cdn\Observer\MarkEsiPage" /> | ||
</event> | ||
|
||
<event name="controller_action_postdispatch_magento_pagecache_block_esi"> | ||
<observer name="fastly_mark_esi_block" instance="Fastly\Cdn\Observer\MarkEsiBlock" /> | ||
</event> | ||
</config> |