Skip to content

Commit

Permalink
NEXT-31669 - Fix esi caching
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSkroblin authored and keulinho committed Aug 15, 2024
1 parent edc1be7 commit 82fab73
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Core/Framework/Adapter/Kernel/EsiDecoration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\Adapter\Kernel;

use Shopware\Core\Framework\Log\Package;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\AbstractSurrogate;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpKernelInterface;

#[Package('core')]
class EsiDecoration extends Esi
{
/**
* @see AbstractSurrogate::handle()
* @see HttpCacheKernel::handle()
*/
public function handle(HttpCache $cache, string $uri, string $alt, bool $ignoreErrors): string
{
$subRequest = Request::create($uri, Request::METHOD_GET, [], $cache->getRequest()->cookies->all(), [], $cache->getRequest()->server->all());

// sw-fix-start
// We need to track symfony esi requests to handle them like a main request, otherwise, the request can not be cached
$subRequest->attributes->set('_sw_esi', true);
// sw-fix-end

try {
$response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);

if (!$response->isSuccessful() && $response->getStatusCode() !== Response::HTTP_NOT_MODIFIED) {
// @phpstan-ignore-next-line (no domain exception, symfony will patch this)
throw new \RuntimeException(\sprintf('Error when rendering "%s" (Status code is %d).', $subRequest->getUri(), $response->getStatusCode()));
}

return (string) $response->getContent();
} catch (\Exception $e) {
if ($alt) {
return $this->handle($cache, $alt, '', $ignoreErrors);
}

if (!$ignoreErrors) {
throw $e;
}
}

return '';
}
}
2 changes: 2 additions & 0 deletions src/Core/Framework/Adapter/Kernel/HttpCacheKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
// https://github.com/symfony/symfony/issues/51648#issuecomment-1717846894
if ($type === HttpKernelInterface::MAIN_REQUEST) {
$response = parent::handle($request, $type, $catch);
} elseif ($request->attributes->has('_sw_esi')) {
$response = parent::handle($request, $type, $catch);
} else {
$response = $this->getKernel()->handle($request, $type, $catch);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Framework/DependencyInjection/cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="esi" class="Shopware\Core\Framework\Adapter\Kernel\EsiDecoration" />

<service id="Shopware\Core\Framework\Adapter\Cache\Http\CacheControlListener" autoconfigure="true" autowire="true">
<argument>%shopware.http_cache.reverse_proxy.enabled%</argument>

Expand Down

0 comments on commit 82fab73

Please sign in to comment.