Skip to content

Commit 3bd1180

Browse files
committed
TASK: Make the fusion autoconfiguration opt in via header X-FullPageCache-EnableFusionAutoconfiguration
The header is set for Neos.Neos:Page automatically once the enabled setting for Flowpack.FullPageCache is set. This prevents the package from interfering with custom controllers and also enables custom controllers to control the caching behavior via the headers `X-FullPageCache-Enabled`, `X-FullPageCache-Lifetime` and `X-FullPageCache-Tags` or when fusion rendering is used by activating `X-FullPageCache-EnableFusionAutoconfiguration` Resolves: #15
1 parent 23dfe7e commit 3bd1180

6 files changed

Lines changed: 37 additions & 16 deletions

File tree

Classes/Middleware/CacheHeaderMiddleware.php renamed to Classes/Middleware/FusionAutoconfigurationMiddleware.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
use Flowpack\FullPageCache\Aspects\ContentCacheAspect;
1212
use Flowpack\FullPageCache\Cache\MetadataAwareStringFrontend;
1313

14-
class CacheHeaderMiddleware implements MiddlewareInterface
14+
class FusionAutoconfigurationMiddleware implements MiddlewareInterface
1515
{
16+
public const HEADER_ENABLED = 'X-FullPageCache-EnableFusionAutoconfiguration';
1617

1718
/**
1819
* @Flow\Inject
@@ -35,11 +36,17 @@ class CacheHeaderMiddleware implements MiddlewareInterface
3536
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
3637
{
3738
if (!$this->enabled || !$request->hasHeader(RequestCacheMiddleware::HEADER_ENABLED)) {
38-
return $next->handle($request);
39+
return $next->handle($request)->withoutHeader(self::HEADER_ENABLED);
3940
}
4041

4142
$response = $next->handle($request);
4243

44+
if (!$response->hasHeader(self::HEADER_ENABLED)) {
45+
return $response;
46+
} else {
47+
$response = $response->withoutHeader(self::HEADER_ENABLED);
48+
}
49+
4350
list($hasUncachedSegments, $tags, $lifetime) = $this->getFusionCacheInformations();
4451

4552
if ($response->hasHeader('Set-Cookie') || $hasUncachedSegments) {

Classes/Middleware/PublicCacheHeaderMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3232

3333
$response = $next->handle($request);
3434

35-
if ($response->hasHeader("Cache-Control")) {
35+
if ($response->hasHeader("Cache-Control") || !$response->hasHeader(RequestCacheMiddleware::HEADER_ENABLED)) {
3636
return $response;
3737
}
3838

Configuration/Objects.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Flowpack\FullPageCache\Middleware\CacheHeaderMiddleware:
1+
Flowpack\FullPageCache\Middleware\FusionAutoconfigurationMiddleware:
22
properties:
33
contentCache:
44
object:

Configuration/Settings.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ Neos:
3838
'fullPageRequestCache':
3939
middleware: 'Flowpack\FullPageCache\Middleware\RequestCacheMiddleware'
4040
position: 'after trustedProxies'
41-
'fullPagePublicCacheHeader':
41+
'fullPageCachePublicHeader':
4242
middleware: 'Flowpack\FullPageCache\Middleware\PublicCacheHeaderMiddleware'
43-
position: 'before fullPageCacheHeader'
44-
'fullPageCacheHeader':
45-
middleware: 'Flowpack\FullPageCache\Middleware\CacheHeaderMiddleware'
46-
position: 'after fullPageRequestCache'
43+
position: 'after fullPageRequestCache 20'
44+
'fullPageCacheFusionAutoconfiguration':
45+
middleware: 'Flowpack\FullPageCache\Middleware\FusionAutoconfigurationMiddleware'
46+
position: 'after fullPageRequestCache 10'
47+
Neos:
48+
fusion:
49+
autoInclude:
50+
Flowpack.FullPageCache: true

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,21 @@ How it works
5656

5757
The package defines three http middlewares:
5858

59-
- RequestCacheMiddleware: If a request is cacheble the cache is asked first and only if no response is found the
60-
reqeust is passed down the middleware chain. The cache lifetime and tags are determined from the
59+
- `fullPageRequestCache`: If a request is cacheble the cache is asked first and only if no response is found the
60+
request is passed down the middleware chain. The cache lifetime and tags are determined from the
6161
`X-FullPageCache-Enabled`, `X-FullPageCache-Lifetime` and `X-FullPageCache-Tags` that are set by upstream middlewares
6262
or controllers.
6363

64-
- PublicCacheHeaderMiddleware: Set `ETag` and `CacheControl` Headers based on the `X-FullPageCache-Enabled` and the
64+
- `fullPageCachePublicHeader` : Set `ETag` and `CacheControl` Headers based on the `X-FullPageCache-Enabled` and the
6565
`X-FullPageCache-Lifetime` headers taking the `maxPublicCacheTime` into account.
6666

67-
- CacheHeaderMiddleware: Connects to the fusion cache and extracts tags plus the allowed lifetime which is then
68-
stored in the response headers `X-FullPageCache-Enabled`, `X-FullPageCache-Lifetime` and `X-FullPageCache-Tags`
67+
- `fullPageCacheFusionAutoconfiguration`: Connects to the fusion cache and extracts tags plus the allowed lifetime which is then
68+
stored in the response headers `X-FullPageCache-Enabled`, `X-FullPageCache-Lifetime` and `X-FullPageCache-Tags`.
69+
This component expects a header `X-FullPageCache-EnableFusionAutoconfiguration` which is set automatically for `Neos.Neos:Page`.
6970

70-
Controllers that want to control the caching behavior directly can set the headers `X-FullPageCache-Enabled`,
71-
`X-FullPageCache-Lifetime` and `X-FullPageCache-Tags` directly.
71+
Custom controllers that want to control the caching behavior directly can set the headers `X-FullPageCache-Enabled`,
72+
`X-FullPageCache-Lifetime` and `X-FullPageCache-Tags` directly while fusion based controllers can enable the autoconfiguration
73+
by setting the header `X-FullPageCache-EnableFusionAutoconfiguration`.
7274

7375
Warning
7476
-------
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
prototype(Neos.Neos:Page) {
2+
httpResponseHead {
3+
headers {
4+
'X-FullPageCache-EnableFusionAutoconfiguration' = ''
5+
'X-FullPageCache-EnableFusionAutoconfiguration'.@if.isEnabled = ${Configuration.setting('Flowpack.FullPageCache.enabled') == true}
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)