configFactory = $config_factory; $this->cacheTagsInvalidator = $cache_tags_invalidator; } /** * {@inheritdoc} */ protected static function getPriority() { // A very high priority so that it can take precedent over anything else, // and thus be fast. return 200; } /** * {@inheritdoc} */ protected function getHandledFormats() { return ['html']; } /** * Handles a 404 error for HTML. * * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event * The event to process. */ public function on404(ExceptionEvent $event) { $request = $event->getRequest(); $config = $this->configFactory->get('system.performance'); $exclude_paths = $config->get('fast_404.exclude_paths'); if ($config->get('fast_404.enabled') && $exclude_paths && !preg_match($exclude_paths, $request->getPathInfo())) { $fast_paths = $config->get('fast_404.paths'); if ($fast_paths && preg_match($fast_paths, $request->getPathInfo())) { $fast_404_html = strtr($config->get('fast_404.html'), ['@path' => Html::escape($request->getUri())]); $response = new HtmlResponse($fast_404_html, Response::HTTP_NOT_FOUND); // Some routes such as system.files conditionally throw a // NotFoundHttpException depending on URL parameters instead of just the // route and route parameters, so add the URL cache context to account // for this. $cacheable_metadata = new CacheableMetadata(); $cacheable_metadata->setCacheContexts(['url']); $cacheable_metadata->addCacheTags(['4xx-response']); $response->addCacheableDependency($cacheable_metadata); $event->setResponse($response); } } } /** * Invalidates 4xx-response cache tag if fast 404 config is changed. * * @param \Drupal\Core\Config\ConfigCrudEvent $event * The configuration event. */ public function onConfigSave(ConfigCrudEvent $event): void { $saved_config = $event->getConfig(); if ($saved_config->getName() === 'system.performance' && $event->isChanged('fast_404')) { $this->cacheTagsInvalidator->invalidateTags(['4xx-response']); } } /** * {@inheritdoc} */ public static function getSubscribedEvents(): array { $events = parent::getSubscribedEvents(); $events[ConfigEvents::SAVE] = 'onConfigSave'; return $events; } }