Skip to content

Commit

Permalink
Merge pull request #172 from Inchoo/feature/mark-esi-documents
Browse files Browse the repository at this point in the history
Marke the pages with embedded ESI with X-Esi header
  • Loading branch information
vvuksan authored Apr 27, 2018
2 parents 3635c09 + 0c46020 commit 5dcfc43
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 4 deletions.
17 changes: 16 additions & 1 deletion Block/GeoIp/GetAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Fastly\Cdn\Model\Config;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Context;
use Magento\Framework\App\ResponseInterface as Response;

/**
* This is a just a place holder to insert the ESI tag for GeoIP lookup.
Expand All @@ -34,19 +35,27 @@ class GetAction extends AbstractBlock
*/
private $config;

/**
* @var Response
*/
private $response;

/**
* GetAction constructor.
*
* @param Config $config
* @param Context $context
* @param Response $response
* @param array $data
*/
public function __construct(
Config $config,
Context $context,
Response $response,
array $data = []
) {
$this->config = $config;
$this->response = $response;

parent::__construct($context, $data);
}
Expand All @@ -58,13 +67,19 @@ public function __construct(
*/
protected function _toHtml() // @codingStandardsIgnoreLine - required by parent class
{
if ($this->config->isGeoIpEnabled() == false) {
if ($this->config->isGeoIpEnabled() == false || $this->config->isFastlyEnabled() == false) {
return parent::_toHtml();
}

/** @var string $actionUrl */
$actionUrl = $this->getUrl('fastlyCdn/geoip/getaction');

// This page has an esi tag, set x-esi header if it is not already set
$header = $this->response->getHeader('x-esi');
if (empty($header)) {
$this->response->setHeader("x-esi", "1");
}

// HTTPS ESIs are not supported so we need to turn them into HTTP
return sprintf(
'<esi:include src=\'%s\' />',
Expand Down
3 changes: 0 additions & 3 deletions Model/Layout/LayoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ public function afterGenerateXml(\Magento\Framework\View\Layout $subject, $resul
$this->response->setHeader("fastly-page-cacheable", "NO");
}

// This page potentially has ESIs so as a first cut let's mark it as such
$this->response->setHeader("x-esi", "1");

return $result;
}

Expand Down
51 changes: 51 additions & 0 deletions Observer/MarkEsiBlock.php
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");
}
}
76 changes: 76 additions & 0 deletions Observer/MarkEsiPage.php
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");
}
}
}
}
11 changes: 11 additions & 0 deletions etc/frontend/events.xml
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>

0 comments on commit 5dcfc43

Please sign in to comment.