Skip to content

Commit 21d5e9b

Browse files
committed
Add MAGE_RUN_TYPE+MAGE_RUN_CODE to FPC identifier
Currently Magento 2.2.6 full page cache does not work correctly, when using SetEnv or similar techniques on unchanged URLs So the MAGE_RUN_TYPE and MAGE_RUN_CODE should be added to the FPC identifier to allow for such use cases. A common case is Magento 2 behind a reverse NGINX proxy. This commit adds the MAGE_RUN_TYPE and MAGE_RUN_CODE to the cache idenfier, if present. It is done inline with the design exceptions. Also the existing cache idenfier is change a little to accomondate the new information properly. Of course this will invalidate existing page caches with design exceptions, but as for any deploy the page cache usually is invalidated, this is not a problem.
1 parent 890bf89 commit 21d5e9b

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

app/code/Magento/PageCache/Model/App/CacheIdentifierPlugin.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\PageCache\Model\App;
88

9+
use Magento\Store\Model\StoreManager;
10+
911
/**
1012
* Class CachePlugin
1113
* Should add design exceptions o identifier for built-in cache
@@ -40,10 +42,23 @@ public function __construct(
4042
public function afterGetValue(\Magento\Framework\App\PageCache\Identifier $identifier, $result)
4143
{
4244
if ($this->config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->config->isEnabled()) {
45+
$identifierPrefix = '';
46+
4347
$ruleDesignException = $this->designExceptions->getThemeByRequest($this->request);
4448
if ($ruleDesignException !== false) {
45-
return $ruleDesignException . $result;
49+
$identifierPrefix .= 'DESIGN' . '=' . $ruleDesignException . '|';
50+
}
51+
52+
if ($runType = $this->request->getServerValue(StoreManager::PARAM_RUN_TYPE)) {
53+
$identifierPrefix .= StoreManager::PARAM_RUN_TYPE . '=' . $runType . '|';
4654
}
55+
56+
if ($runCode = $this->request->getServerValue(StoreManager::PARAM_RUN_CODE)) {
57+
$identifierPrefix .= StoreManager::PARAM_RUN_CODE . '=' . $runCode . '|';
58+
}
59+
60+
return $identifierPrefix . $result;
61+
4762
}
4863
return $result;
4964
}

app/code/Magento/PageCache/Test/Unit/App/CacheIdentifierPluginTest.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\PageCache\Test\Unit\App;
77

88
use Magento\PageCache\Model\Config;
9+
use Magento\Store\Model\Store;
10+
use Magento\Store\Model\StoreManager;
911

1012
/**
1113
* Class CacheIdentifierPluginTest
@@ -56,7 +58,7 @@ protected function setUp()
5658
}
5759

5860
/**
59-
* Test of adding design exceptions to the kay of cache hash
61+
* Test of adding design exceptions + run code to the key of cache hash
6062
*
6163
* @param string $cacheType
6264
* @param bool $isPageCacheEnabled
@@ -95,7 +97,68 @@ public function afterGetValueDataProvider()
9597
'Built-in + PageCache enabled' => [Config::BUILT_IN, true, null, false, false],
9698
'Built-in, PageCache enabled, no user-agent exceptions' =>
9799
[Config::BUILT_IN, true, 'aa123aa', false, 'aa123aa'],
98-
'Built-in, PageCache enabled, with design exception' => [Config::BUILT_IN, true, 'aa123aa', '7', '7aa123aa']
100+
'Built-in, PageCache enabled, with design exception' => [Config::BUILT_IN, true, 'aa123aa', '7', 'DESIGN=7|aa123aa']
99101
];
100102
}
103+
104+
/**
105+
* Tests that different stores cause different identifiers
106+
* (property based testing approach)
107+
*/
108+
public function testAfterGetValueRunParamsCauseDifferentIdentifiers()
109+
{
110+
$identifierMock = $this->createMock(\Magento\Framework\App\PageCache\Identifier::class);
111+
112+
$this->pageCacheConfigMock->expects($this->any())
113+
->method('getType')
114+
->willReturn(Config::BUILT_IN);
115+
$this->pageCacheConfigMock->expects($this->any())
116+
->method('isEnabled')
117+
->willReturn(true);
118+
119+
$defaultRequestMock = clone $this->requestMock;
120+
$defaultRequestMock->expects($this->any())
121+
->method('getServerValue')
122+
->willReturnCallback(function ($param) {
123+
if ($param == StoreManager::PARAM_RUN_TYPE) {
124+
return 'store';
125+
}
126+
if ($param == StoreManager::PARAM_RUN_CODE) {
127+
return 'default';
128+
}
129+
});
130+
131+
$nullSha1 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
132+
133+
$defaultPlugin = new \Magento\PageCache\Model\App\CacheIdentifierPlugin(
134+
$this->designExceptionsMock,
135+
$defaultRequestMock,
136+
$this->pageCacheConfigMock
137+
);
138+
139+
$defaultStoreResult = $defaultPlugin->afterGetValue($identifierMock, $nullSha1);
140+
141+
$otherRequestMock = clone $this->requestMock;
142+
$otherRequestMock->expects($this->any())
143+
->method('getServerValue')
144+
->willReturnCallback(function ($param) {
145+
if ($param == StoreManager::PARAM_RUN_TYPE) {
146+
return 'store';
147+
}
148+
if ($param == StoreManager::PARAM_RUN_CODE) {
149+
return 'klingon';
150+
}
151+
});
152+
153+
$otherPlugin = new \Magento\PageCache\Model\App\CacheIdentifierPlugin(
154+
$this->designExceptionsMock,
155+
$otherRequestMock,
156+
$this->pageCacheConfigMock
157+
);
158+
$otherStoreResult = $otherPlugin->afterGetValue($identifierMock, $nullSha1);
159+
160+
$this->assertNotEquals($nullSha1, $defaultStoreResult);
161+
$this->assertNotEquals($nullSha1, $otherStoreResult);
162+
$this->assertNotEquals($defaultStoreResult, $otherStoreResult);
163+
}
101164
}

lib/internal/Magento/Framework/App/PageCache/Identifier.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function getValue()
5656
$this->request->get(\Magento\Framework\App\Response\Http::COOKIE_VARY_STRING)
5757
?: $this->context->getVaryString()
5858
];
59+
5960
return sha1($this->serializer->serialize($data));
6061
}
6162
}

0 commit comments

Comments
 (0)