Skip to content

Commit 39bbde0

Browse files
author
Joan He
committed
Merge remote-tracking branch 'origin/MAGETWO-42061-caching-for-home-page' into develop
2 parents 294d44a + 4a770bb commit 39bbde0

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

app/code/Magento/PageCache/etc/varnish3.vcl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ sub vcl_fetch {
8080
set beresp.do_gzip = true;
8181
}
8282

83-
# cache only successfully responses
84-
if (beresp.status != 200) {
83+
# cache only successfully responses and 404s
84+
if (beresp.status != 200 && beresp.status != 404) {
8585
set beresp.ttl = 0s;
8686
return (hit_for_pass);
8787
} elsif (beresp.http.Cache-Control ~ "private") {

app/code/Magento/PageCache/etc/varnish4.vcl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ sub vcl_backend_response {
7171
set beresp.do_gzip = true;
7272
}
7373

74-
# cache only successfully responses
75-
if (beresp.status != 200) {
74+
# cache only successfully responses and 404s
75+
if (beresp.status != 200 && beresp.status != 404) {
7676
set beresp.ttl = 0s;
7777
set beresp.uncacheable = true;
7878
return (deliver);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public function process(\Magento\Framework\App\Response\Http $response)
6464
if (preg_match('/public.*s-maxage=(\d+)/', $response->getHeader('Cache-Control')->getFieldValue(), $matches)) {
6565
$maxAge = $matches[1];
6666
$response->setNoCacheHeaders();
67-
if ($response->getHttpResponseCode() == 200 && ($this->request->isGet() || $this->request->isHead())) {
67+
if (($response->getHttpResponseCode() == 200 || $response->getHttpResponseCode() == 404)
68+
&& ($this->request->isGet() || $this->request->isHead())
69+
) {
6870
$tagsHeader = $response->getHeader('X-Magento-Tags');
6971
$tags = $tagsHeader ? explode(',', $tagsHeader->getFieldValue()) : [];
7072

lib/internal/Magento/Framework/App/Test/Unit/PageCache/KernelTest.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ public function loadProvider()
9898
];
9999
}
100100

101-
public function testProcessSaveCache()
101+
/**
102+
* @param $httpCode
103+
* @dataProvider testProcessSaveCacheDataProvider
104+
*/
105+
public function testProcessSaveCache($httpCode, $at)
102106
{
103-
$httpCode = 200;
104-
105107
$cacheControlHeader = \Zend\Http\Header\CacheControl::fromString(
106108
'Cache-Control: public, max-age=100, s-maxage=100'
107109
);
@@ -116,21 +118,37 @@ public function testProcessSaveCache()
116118
$this->returnValue($cacheControlHeader)
117119
);
118120
$this->responseMock->expects(
119-
$this->once()
121+
$this->any()
120122
)->method(
121123
'getHttpResponseCode'
122-
)->will(
123-
$this->returnValue($httpCode)
124-
);
125-
$this->requestMock->expects($this->once())->method('isGet')->will($this->returnValue(true));
126-
$this->responseMock->expects($this->once())->method('setNoCacheHeaders');
127-
$this->responseMock->expects($this->at(3))->method('getHeader')->with('X-Magento-Tags');
128-
$this->responseMock->expects($this->at(4))->method('clearHeader')->with($this->equalTo('Set-Cookie'));
129-
$this->responseMock->expects($this->at(5))->method('clearHeader')->with($this->equalTo('X-Magento-Tags'));
130-
$this->cacheMock->expects($this->once())->method('save');
124+
)->willReturn($httpCode);
125+
$this->requestMock->expects($this->once())
126+
->method('isGet')
127+
->willReturn(true);
128+
$this->responseMock->expects($this->once())
129+
->method('setNoCacheHeaders');
130+
$this->responseMock->expects($this->at($at[0]))
131+
->method('getHeader')
132+
->with('X-Magento-Tags');
133+
$this->responseMock->expects($this->at($at[1]))
134+
->method('clearHeader')
135+
->with($this->equalTo('Set-Cookie'));
136+
$this->responseMock->expects($this->at($at[2]))
137+
->method('clearHeader')
138+
->with($this->equalTo('X-Magento-Tags'));
139+
$this->cacheMock->expects($this->once())
140+
->method('save');
131141
$this->kernel->process($this->responseMock);
132142
}
133143

144+
public function testProcessSaveCacheDataProvider()
145+
{
146+
return [
147+
[200, [3, 4, 5]],
148+
[404, [4, 5, 6]]
149+
];
150+
}
151+
134152
/**
135153
* @dataProvider processNotSaveCacheProvider
136154
* @param string $cacheControlHeader
@@ -167,13 +185,11 @@ public function processNotSaveCacheProvider()
167185
return [
168186
['private, max-age=100', 200, true, false],
169187
['private, max-age=100', 200, false, false],
170-
['private, max-age=100', 404, true, false],
171188
['private, max-age=100', 500, true, false],
172189
['no-store, no-cache, must-revalidate, max-age=0', 200, true, false],
173190
['no-store, no-cache, must-revalidate, max-age=0', 200, false, false],
174191
['no-store, no-cache, must-revalidate, max-age=0', 404, true, false],
175192
['no-store, no-cache, must-revalidate, max-age=0', 500, true, false],
176-
['public, max-age=100, s-maxage=100', 404, true, true],
177193
['public, max-age=100, s-maxage=100', 500, true, true],
178194
['public, max-age=100, s-maxage=100', 200, false, true]
179195
];

0 commit comments

Comments
 (0)