Skip to content

Correct display for "fastly-page-cacheable" debug header #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
18 changes: 10 additions & 8 deletions Model/Layout/LayoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ public function afterGenerateElements(\Magento\Framework\View\Layout $subject):
}
}

/*
* Surface the cacheability of a page. This may expose things like page blocks being set to
* cacheable = false which makes the whole page uncacheable
*/
if ($subject->isCacheable()) {
$this->response->setHeader("fastly-page-cacheable", "YES");
} else {
$this->response->setHeader("fastly-page-cacheable", "NO");
if (empty($this->response->getHeader('fastly-page-cacheable'))) {
/*
* Surface the cacheability of a page. This may expose things like page blocks being set to
* cacheable = false which makes the whole page uncacheable
*/
if ($subject->isCacheable()) {
$this->response->setHeader("fastly-page-cacheable", "YES");
} else {
$this->response->setHeader("fastly-page-cacheable", "NO");
}
}
}

Expand Down
42 changes: 29 additions & 13 deletions Test/Unit/Model/Layout/LayoutPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function testAfterGenerateElements($cacheState, $layoutIsCacheable, $cach
$staleErrorTtl = 0, $cacheControl = 'max-age=86400, public, s-maxage=86400'): void
{
$headerName = 'cache-control';
$cacheableHeaderName = 'fastly-page-cacheable';

$this->layoutMock->expects($this->any()
)->method('isCacheable'
Expand All @@ -132,11 +133,18 @@ public function testAfterGenerateElements($cacheState, $layoutIsCacheable, $cach
if ($layoutIsCacheable && $cacheState && $cacheType == \Fastly\Cdn\Model\Config::FASTLY && $ttl > 0) {
if (!empty($cacheControl)) {
$cacheControlHeader = new GenericHeader($headerName, $cacheControl);

$this->responseMock->expects($this->once()
)->method('getHeader'
)->with($headerName
)->will($this->returnValue($cacheControlHeader));
$cacheableHeader = new GenericHeader($cacheableHeaderName, 'YES');

$this->responseMock->expects($this->exactly(2))
->method('getHeader')
->withConsecutive(
[$headerName],
[$cacheableHeaderName]
)
->willReturnOnConsecutiveCalls(
$this->returnValue($cacheControlHeader),
$this->returnValue($cacheableHeader)
);

$this->configMock->expects($this->once()
)->method('getStaleTtl'
Expand All @@ -157,19 +165,27 @@ public function testAfterGenerateElements($cacheState, $layoutIsCacheable, $cach
$value = sprintf(', stale-while-revalidate=%s', $staleTtl);
}

$this->responseMock->expects($this->exactly(2)
// change to once since 'fastly-page-cacheable' is already set in this mock, so method won't set it again
$this->responseMock->expects($this->once()
)->method('setHeader');
//->with($headerName, $cacheControl . $value, true);

} else {

$this->responseMock->expects($this->once()
)->method('getHeader'
)->with($headerName
)->will($this->returnValue(false));

$this->responseMock->expects($this->once()
)->method('setHeader');
$this->responseMock->expects($this->exactly(2))
->method('getHeader')
->withConsecutive(
[$headerName],
[$cacheableHeaderName]
)
->willReturnOnConsecutiveCalls(
$this->returnValue(false),
$this->returnValue('NO')
);

// Removed because 'cache-control' is not set in this case and 'fastly-page-cacheable' is already set
// in mock, so second setHeader is skipped
//$this->responseMock->expects($this->once())->method('setHeader');
}
} else {
$this->responseMock->expects($this->once()
Expand Down