core/core.services.yml | 2 +- .../Core/EventSubscriber/SmartCacheSubscriber.php | 4 ++-- .../Drupal/Core/Render/MainContent/HtmlRenderer.php | 19 ++++++++++++++++++- core/modules/page_cache/src/Tests/PageCacheTest.php | 2 +- core/modules/path/src/Tests/PathAliasTest.php | 4 ++++ core/modules/system/system.routing.yml | 2 ++ 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index b2cf6a6..cb72fd4 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -907,7 +907,7 @@ services: - { name: event_subscriber } main_content_renderer.html: class: Drupal\Core\Render\MainContent\HtmlRenderer - arguments: ['@title_resolver', '@plugin.manager.display_variant', '@event_dispatcher', '@module_handler', '@renderer', '@render_cache'] + arguments: ['@title_resolver', '@plugin.manager.display_variant', '@event_dispatcher', '@module_handler', '@renderer', '@render_cache', '%renderer.config%'] tags: - { name: render.main_content_renderer, format: html } main_content_renderer.ajax: diff --git a/core/lib/Drupal/Core/EventSubscriber/SmartCacheSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/SmartCacheSubscriber.php index 130c1cb..466e05f 100644 --- a/core/lib/Drupal/Core/EventSubscriber/SmartCacheSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/SmartCacheSubscriber.php @@ -100,8 +100,8 @@ public function __construct(RouteMatchInterface $route_match, CacheContextsManag $this->cacheContextsManager = $cache_contexts_manager; $this->smartContextsCache = $contexts_cache; $this->smartHtmlCache = $html_cache; - $this->requestPolicy = \Drupal::service('smart_cache_request_policy'); - $this->responsePolicy = \Drupal::service('smart_cache_response_policy'); + $this->requestPolicy = $request_policy; + $this->responsePolicy = $response_policy; $this->rendererConfig = $renderer_config; } diff --git a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php index 542d337..8cbe587 100644 --- a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php +++ b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Render\MainContent; use Drupal\Component\Plugin\PluginManagerInterface; +use Drupal\Core\Cache\Cache; use Drupal\Core\Controller\TitleResolverInterface; use Drupal\Core\Display\PageVariantInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -76,6 +77,13 @@ class HtmlRenderer implements MainContentRendererInterface { protected $renderCache; /** + * The renderer configuration array. + * + * @var array + */ + protected $rendererConfig; + + /** * Constructs a new HtmlRenderer. * * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver @@ -90,14 +98,17 @@ class HtmlRenderer implements MainContentRendererInterface { * The renderer service. * @param \Drupal\Core\Render\RenderCacheInterface $render_cache * The render cache service. + * @param array $renderer_config + * The renderer configuration array. */ - public function __construct(TitleResolverInterface $title_resolver, PluginManagerInterface $display_variant_manager, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $module_handler, RendererInterface $renderer, RenderCacheInterface $render_cache) { + public function __construct(TitleResolverInterface $title_resolver, PluginManagerInterface $display_variant_manager, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $module_handler, RendererInterface $renderer, RenderCacheInterface $render_cache, array $renderer_config) { $this->titleResolver = $title_resolver; $this->displayVariantManager = $display_variant_manager; $this->eventDispatcher = $event_dispatcher; $this->moduleHandler = $module_handler; $this->renderer = $renderer; $this->renderCache = $render_cache; + $this->rendererConfig = $renderer_config; } /** @@ -140,6 +151,12 @@ public function renderResponse(array $main_content, Request $request, RouteMatch } $content = $this->renderCache->getCacheableRenderArray($html); + // Also associate the required cache contexts. Since we only "soft-render" + // the HTML above, and don't use a ::renderRoot() call which would add the + // required cache contexts automatically, we have to make sure manually that + // the HTML response varies by the required cache contexts. + $content['#cache']['contexts'] = Cache::mergeContexts($content['#cache']['contexts'], $this->rendererConfig['required_cache_contexts']); + // Also associate the "rendered" cache tag. This allows us to invalidate the // entire render cache, regardless of the cache bin. $content['#cache']['tags'][] = 'rendered'; diff --git a/core/modules/page_cache/src/Tests/PageCacheTest.php b/core/modules/page_cache/src/Tests/PageCacheTest.php index e97a1b4..18a6dfd 100644 --- a/core/modules/page_cache/src/Tests/PageCacheTest.php +++ b/core/modules/page_cache/src/Tests/PageCacheTest.php @@ -399,7 +399,7 @@ public function testFormImmutability() { // that implementation. \Drupal::state()->set('page_cache_bypass_form_immutability', TRUE); \Drupal::moduleHandler()->resetImplementations(); - \Drupal::cache('render')->deleteAll(); + Cache::invalidateTags(['rendered']); $this->drupalGet('page_cache_form_test_immutability'); diff --git a/core/modules/path/src/Tests/PathAliasTest.php b/core/modules/path/src/Tests/PathAliasTest.php index e4d07e2..651c11f 100644 --- a/core/modules/path/src/Tests/PathAliasTest.php +++ b/core/modules/path/src/Tests/PathAliasTest.php @@ -7,6 +7,8 @@ namespace Drupal\path\Tests; +use Drupal\Core\Cache\Cache; + /** * Add, edit, delete, and change alias and verify its consistency in the * database. @@ -57,6 +59,8 @@ function testPathCache() { // Visit the alias for the node and confirm a cache entry is created. \Drupal::cache('data')->deleteAll(); + // @todo Remove this once https://www.drupal.org/node/2480077 lands. + Cache::invalidateTags(['rendered']); $this->drupalGet(trim($edit['alias'], '/')); $this->assertTrue(\Drupal::cache('data')->get('preload-paths:' . $edit['source']), 'Cache entry was created.'); } diff --git a/core/modules/system/system.routing.yml b/core/modules/system/system.routing.yml index 9d806bb..2bc24a0 100644 --- a/core/modules/system/system.routing.yml +++ b/core/modules/system/system.routing.yml @@ -454,6 +454,8 @@ system.db_update: _title: 'Drupal database update' _controller: '\Drupal\system\Controller\DbUpdateController::handle' op: 'info' + options: + _admin_route: TRUE requirements: _access_system_update: 'TRUE'