Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Block/Adminhtml/Form/Field/BypassRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Elgentos\VarnishExtended\Block\Adminhtml\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

class BypassRoutes extends AbstractFieldArray
{
/**
* Prepare rendering the new field by adding all the needed columns
*/
protected function _prepareToRender(): void
{
$this->addColumn('param', ['label' => __('Bypass Routes'), 'class' => 'required-entry']);
$this->_addAfter = true;
$this->_addButtonLabel = __('Add Bypass Route');
}
}
20 changes: 20 additions & 0 deletions Block/Adminhtml/Form/Field/StatusCodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Elgentos\VarnishExtended\Block\Adminhtml\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

class StatusCodes extends AbstractFieldArray
{
/**
* Prepare rendering the new field by adding all the needed columns
*/
protected function _prepareToRender(): void
{
$this->addColumn('param', ['label' => __('Status codes'), 'class' => 'required-entry']);
$this->_addAfter = true;
$this->_addButtonLabel = __('Add status code');
}
}
62 changes: 41 additions & 21 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,27 @@

class Config extends PageCacheConfig
{
private ScopeConfigInterface $scopeConfig;

private Json $serializer;

public const XML_PATH_VARNISH_ENABLE_BFCACHE = 'system/full_page_cache/varnish/enable_bfcache';

public const XML_PATH_VARNISH_ENABLE_MEDIA_CACHE = 'system/full_page_cache/varnish/enable_media_cache';

public const XML_PATH_VARNISH_ENABLE_STATIC_CACHE = 'system/full_page_cache/varnish/enable_static_cache';

public const XML_PATH_VARNISH_TRACKING_PARAMETERS = 'system/full_page_cache/varnish/tracking_parameters';

public const XML_PATH_VARNISH_USE_XKEY_VMOD = 'system/full_page_cache/varnish/use_xkey_vmod';

public const XML_PATH_VARNISH_USE_SOFT_PURGING = 'system/full_page_cache/varnish/use_soft_purging';

public const XML_PATH_VARNISH_PASS_ON_COOKIE_PRESENCE = 'system/full_page_cache/varnish/pass_on_cookie_presence';
public const XML_PATH_VARNISH_BYPASS_ROUTES = 'system/full_page_cache/varnish/bypass_routes';
public const XML_PATH_VARNISH_CACHABLE_STATUS_CODES = 'system/full_page_cache/varnish/cachable_status_codes';
private ScopeConfigInterface $scopeConfig;
private Json $serializer;

public function __construct(
ReadFactory $readFactory,
ReadFactory $readFactory,
ScopeConfigInterface $scopeConfig,
StateInterface $cacheState,
Reader $reader,
VclGeneratorFactory $vclGeneratorFactory,
Json $serializer
) {
StateInterface $cacheState,
Reader $reader,
VclGeneratorFactory $vclGeneratorFactory,
Json $serializer
)
{
parent::__construct(
$readFactory,
$scopeConfig,
Expand Down Expand Up @@ -69,12 +64,12 @@ public function getTrackingParameters(): string

public function getUseXkeyVmod(): bool
{
return (bool) $this->scopeConfig->getValue(static::XML_PATH_VARNISH_USE_XKEY_VMOD);
return (bool)$this->scopeConfig->getValue(static::XML_PATH_VARNISH_USE_XKEY_VMOD);
}

public function getUseSoftPurging(): bool
{
return (bool) $this->scopeConfig->getValue(static::XML_PATH_VARNISH_USE_SOFT_PURGING);
return (bool)$this->scopeConfig->getValue(static::XML_PATH_VARNISH_USE_SOFT_PURGING);
}

public function getPassOnCookiePresence(): array
Expand All @@ -84,7 +79,7 @@ public function getPassOnCookiePresence(): array

public function getEnableBfcache(): bool
{
return (bool) $this->scopeConfig->getValue(static::XML_PATH_VARNISH_ENABLE_BFCACHE);
return (bool)$this->scopeConfig->getValue(static::XML_PATH_VARNISH_ENABLE_BFCACHE);
}

public function getSslOffloadedHeader()
Expand Down Expand Up @@ -125,11 +120,36 @@ public function getDesignExceptions()

public function getEnableMediaCache(): bool
{
return (bool) $this->scopeConfig->getValue(static::XML_PATH_VARNISH_ENABLE_MEDIA_CACHE);
return (bool)$this->scopeConfig->getValue(static::XML_PATH_VARNISH_ENABLE_MEDIA_CACHE);
}

public function getEnableStaticCache(): bool
{
return (bool) $this->scopeConfig->getValue(static::XML_PATH_VARNISH_ENABLE_STATIC_CACHE);
return (bool)$this->scopeConfig->getValue(static::XML_PATH_VARNISH_ENABLE_STATIC_CACHE);
}

public function getBypassRoutes(): array
{
$routes = (string)$this->scopeConfig->getValue(static::XML_PATH_VARNISH_BYPASS_ROUTES);

if (!json_decode($routes)) {
return [];
}

$routes = array_map(function ($param) {
return ["route" => $param['param']];
}, is_array($routes) ? $routes : json_decode($routes, true));
return $routes;
}

public function getCachableStatusCodes(): array
{

$status_codes = $this->scopeConfig->getValue(static::XML_PATH_VARNISH_CACHABLE_STATUS_CODES);


return array_map(function ($param) {
return ["status_code" => $param['param']];
}, is_array($status_codes) ? $status_codes : json_decode($status_codes, true));
}
}
2 changes: 1 addition & 1 deletion Model/PurgeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class PurgeCache extends \Magento\CacheInvalidate\Model\PurgeCache
{
const string HEADER_X_MAGENTO_PURGE_SOFT = 'X-Magento-Purge-Soft';
const HEADER_X_MAGENTO_PURGE_SOFT = 'X-Magento-Purge-Soft';

/**
* @var Config
Expand Down
7 changes: 5 additions & 2 deletions Model/Varnish/VCLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function generateVcl($version, $inputFile = null)

public function getVariables(): array
{
return [
$res = [
'host' => $this->backendHost,
'port' => $this->backendPort,
'access_list' => $this->getTransformedAccessList(),
Expand All @@ -54,8 +54,11 @@ public function getVariables(): array
'use_xkey_vmod' => (int) $this->varnishExtendedConfig->getUseXkeyVmod(),
'use_soft_purging' => (int) $this->varnishExtendedConfig->getUseSoftPurging(),
'pass_on_cookie_presence' => $this->varnishExtendedConfig->getPassOnCookiePresence(),
'design_exceptions_code' => $this->getRegexForDesignExceptions()
'design_exceptions_code' => $this->getRegexForDesignExceptions(),
'bypass_routes' => $this->varnishExtendedConfig->getBypassRoutes(),
'status_codes' => $this->varnishExtendedConfig->getCachableStatusCodes()
];
return $res;
}

/**
Expand Down
31 changes: 27 additions & 4 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
<section id="system">
<group id="full_page_cache">
<group id="varnish">
<comment><![CDATA[<strong>Note:</strong> When changing these settings, these are <strong>not applied automatically</strong> to any currently loaded VCL! Read the README of the <a href="https://github.com/elgentos/magento2-varnish-extended" target="_new">elgentos/magento2-varnish-extended extension on GitHub</a>.]]></comment>
<comment>
<![CDATA[<strong>Note:</strong> When changing these settings, these are <strong>not applied automatically</strong> to any currently loaded VCL! Read the README of the <a href="https://github.com/elgentos/magento2-varnish-extended" target="_new">elgentos/magento2-varnish-extended extension on GitHub</a>.]]></comment>
<field id="grace_period">
<comment>Specify grace period in seconds for config file generation. If field is empty default value 300 will be saved. This grace period will be used to serve cached content when the server is healthy. If the server is not healthy, cached content will be served for 1 day before failing.</comment>
<comment>Specify grace period in seconds for config file generation. If field is empty default value 300 will be saved. This grace period will be used to serve cached content when the server is healthy. If the server
is not healthy, cached content will be served for 1 day before failing.
</comment>
</field>
<field id="enable_bfcache" type="select" translate="label comment" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Enable Back/forward cache</label>
Expand Down Expand Up @@ -35,7 +38,8 @@

<field id="tracking_parameters" type="text" translate="label comment" sortOrder="33" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Tracking Parameters</label>
<comment><![CDATA[Specify tracking parameters to strip off of incoming requests to increase hit rate. See the <a href="https://github.com/mpchadwick/tracking-query-params-registry" target="_new">tracking query parameters registry</a> for a list of known parameters.]]></comment>
<comment>
<![CDATA[Specify tracking parameters to strip off of incoming requests to increase hit rate. See the <a href="https://github.com/mpchadwick/tracking-query-params-registry" target="_new">tracking query parameters registry</a> for a list of known parameters.]]></comment>
<frontend_model>Elgentos\VarnishExtended\Block\Adminhtml\Form\Field\TrackingParameters</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<depends>
Expand All @@ -56,7 +60,7 @@
<label>Use Xkey vmod</label>
<comment><![CDATA[Varnish Xkey vmod is installed. See <a target="_blank" href="https://github.com/varnish/varnish-modules/blob/master/src/vmod_xkey.vcc">Varnish VMOD xkey</a>.]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</field>
<field id="use_soft_purging" type="select" translate="label comment" sortOrder="36" showInDefault="1">
<label>Use soft purging</label>
<comment>Use soft purging instead of hard purging. This requires Xkey vmod to be installed</comment>
Expand All @@ -65,6 +69,25 @@
<field id="system/full_page_cache/varnish/use_xkey_vmod">1</field>
</depends>
</field>
<field id="bypass_routes" type="text" translate="label comment" sortOrder="37" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Bypass Routes</label>
<comment><![CDATA[Add some routes to bypass varnish, e.g. /foobar]]></comment>
<frontend_model>Elgentos\VarnishExtended\Block\Adminhtml\Form\Field\BypassRoutes</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<depends>
<field id="caching_application">1</field>
</depends>
</field>
<field id="cachable_status_codes" type="text" translate="label comment" sortOrder="38" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Cachable status codes</label>
<comment><![CDATA[This list defines all status codes which should be cached. Everything else won't be cached]]></comment>
<frontend_model>Elgentos\VarnishExtended\Block\Adminhtml\Form\Field\StatusCodes</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<depends>
<field id="caching_application">1</field>
</depends>
</field>

</group>
</group>
</section>
Expand Down
4 changes: 4 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
</tracking_parameters>
<use_xkey_vmod>0</use_xkey_vmod>
<use_soft_purging>0</use_soft_purging>
<cachable_status_codes>
<param_all_ok><param>200</param></param_all_ok>
<param_not_found><param>404</param></param_not_found>
</cachable_status_codes>
</varnish>
</full_page_cache>
</system>
Expand Down
13 changes: 9 additions & 4 deletions etc/varnish6.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ sub vcl_recv {
return (pass);
}

{{for item in bypass_routes}}
if (req.url ~ "{{var item.route}}") {
return (pass);
}
{{/for}}

return (hash);
}

Expand Down Expand Up @@ -241,10 +247,9 @@ sub vcl_backend_response {
set beresp.do_esi = true;
}

# Cache HTTP 200 responses
# TODO MAKE CONFIGURABLE whether or not 404's should be cached
if (beresp.status != 200 && beresp.status != 404) {
#if (beresp.status != 200) {
# Un-Cache everything except configured HTTP codes. Defaults to 200 and 404
if ({{for item in status_codes}} beresp.status != {{var item.status_code}} && {{/for}} true)
{
set beresp.ttl = 120s;
set beresp.uncacheable = true;
return (deliver);
Expand Down
Loading