- Issue created by @sanderwind
When fast404 is enabled and - for example - a CacheableNotFoundHttpException is thrown for a fast404 path, the 404 response does not merge the cacheable metadata of the exception. This results in a 404 page being cached, but unable to invalidate using the cacheable metadata of the thrown exception.
Add a route:
example.txt_file:
path: '/example.txt'
defaults:
_controller: '\Drupal\example\Controller\ExampleController'
_disable_route_normalizer: 'TRUE'
requirements:
# Simple text file allowed to be viewed by everyone.
_access: 'TRUE'
Simple controller with an __invoke()
:
namespace Drupal\example\Controller;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Http\Exception\CacheableNotFoundHttpException;
class ExampleController extends ControllerBase {
public function __invoke() {
$cacheable_metadata = new CacheableMetadata();
$cacheable_metadata->addCacheTags(['example-tag']);
throw new CacheableNotFoundHttpException($cacheable_metadata);
}
}
Visiting the URL will not show the added `example-tag` in the response headers:
HTTP/1.1 404 Not Found
Cache-Tags: 4xx-response http_response config:user.role.anonymous
[ ... ]
The solution might be: adding my custom path to the exclude paths of fast404, but to be fair, I think fast404 should respect the cacheable metadata of the throwable too.
# web/core/lib/Drupal/Core/EventSubscriber/Fast404ExceptionHtmlSubscriber.php
class Fast404ExceptionHtmlSubscriber extends HttpExceptionSubscriberBase {
public function on404(ExceptionEvent $event) {
# Left out other code for simplicity.
// Make sure the cache context and tags from the exception are used too.
$throwable = $event->getThrowable();
if ($throwable instanceof CacheableDependencyInterface) {
$response->addCacheableDependency($throwable);
}
# [ ... ]
}
}
Writing tests to validate the HTTP response containing the cacheable metadata of the thrown exception.
Active
10.3 ✨
request processing system