Issue #2562757 by Wim Leers: Various small cacheability fixes that are blocking SmartCache
parent
e3d5340a1b
commit
ae7ec6c585
|
@ -52,6 +52,11 @@ class ConfirmFormHelper {
|
||||||
'#title' => $form->getCancelText(),
|
'#title' => $form->getCancelText(),
|
||||||
'#attributes' => ['class' => ['button']],
|
'#attributes' => ['class' => ['button']],
|
||||||
'#url' => $url,
|
'#url' => $url,
|
||||||
|
'#cache' => [
|
||||||
|
'contexts' => [
|
||||||
|
'url.query_args:destination',
|
||||||
|
],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,9 @@ use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps the caching logic for the render caching system.
|
* Wraps the caching logic for the render caching system.
|
||||||
|
*
|
||||||
|
* @todo Refactor this out into a generic service capable of cache redirects,
|
||||||
|
* and let RenderCache use that. https://www.drupal.org/node/2551419
|
||||||
*/
|
*/
|
||||||
class RenderCache implements RenderCacheInterface {
|
class RenderCache implements RenderCacheInterface {
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterfa
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected function blockAccess(AccountInterface $account) {
|
protected function blockAccess(AccountInterface $account) {
|
||||||
return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed() : AccessResult::forbidden();
|
return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
namespace Drupal\book\Tests;
|
namespace Drupal\book\Tests;
|
||||||
|
|
||||||
use Drupal\Component\Utility\SafeMarkup;
|
use Drupal\Component\Utility\SafeMarkup;
|
||||||
|
use Drupal\Core\Cache\Cache;
|
||||||
use Drupal\Core\Entity\EntityInterface;
|
use Drupal\Core\Entity\EntityInterface;
|
||||||
use Drupal\simpletest\WebTestBase;
|
use Drupal\simpletest\WebTestBase;
|
||||||
use Drupal\user\RoleInterface;
|
use Drupal\user\RoleInterface;
|
||||||
|
@ -124,11 +125,12 @@ class BookTest extends WebTestBase {
|
||||||
|
|
||||||
// Enable the debug output.
|
// Enable the debug output.
|
||||||
\Drupal::state()->set('book_test.debug_book_navigation_cache_context', TRUE);
|
\Drupal::state()->set('book_test.debug_book_navigation_cache_context', TRUE);
|
||||||
|
Cache::invalidateTags(['book_test.debug_book_navigation_cache_context']);
|
||||||
|
|
||||||
$this->drupalLogin($this->bookAuthor);
|
$this->drupalLogin($this->bookAuthor);
|
||||||
|
|
||||||
// On non-node route.
|
// On non-node route.
|
||||||
$this->drupalGet('');
|
$this->drupalGet($this->adminUser->urlInfo());
|
||||||
$this->assertRaw('[route.book_navigation]=book.none');
|
$this->assertRaw('[route.book_navigation]=book.none');
|
||||||
|
|
||||||
// On non-book node route.
|
// On non-book node route.
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
* Implements hook_page_attachments().
|
* Implements hook_page_attachments().
|
||||||
*/
|
*/
|
||||||
function book_test_page_attachments(array &$page) {
|
function book_test_page_attachments(array &$page) {
|
||||||
|
$page['#cache']['tags'][] = 'book_test.debug_book_navigation_cache_context';
|
||||||
if (\Drupal::state()->get('book_test.debug_book_navigation_cache_context', FALSE)) {
|
if (\Drupal::state()->get('book_test.debug_book_navigation_cache_context', FALSE)) {
|
||||||
drupal_set_message(\Drupal::service('cache_contexts_manager')->convertTokensToKeys(['route.book_navigation'])->getKeys()[0]);
|
drupal_set_message(\Drupal::service('cache_contexts_manager')->convertTokensToKeys(['route.book_navigation'])->getKeys()[0]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -399,7 +399,7 @@ class PageCacheTest extends WebTestBase {
|
||||||
// that implementation.
|
// that implementation.
|
||||||
\Drupal::state()->set('page_cache_bypass_form_immutability', TRUE);
|
\Drupal::state()->set('page_cache_bypass_form_immutability', TRUE);
|
||||||
\Drupal::moduleHandler()->resetImplementations();
|
\Drupal::moduleHandler()->resetImplementations();
|
||||||
\Drupal::cache('render')->deleteAll();
|
Cache::invalidateTags(['rendered']);
|
||||||
|
|
||||||
$this->drupalGet('page_cache_form_test_immutability');
|
$this->drupalGet('page_cache_form_test_immutability');
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
namespace Drupal\path\Tests;
|
namespace Drupal\path\Tests;
|
||||||
|
|
||||||
|
use Drupal\Core\Cache\Cache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add, edit, delete, and change alias and verify its consistency in the
|
* Add, edit, delete, and change alias and verify its consistency in the
|
||||||
* database.
|
* database.
|
||||||
|
@ -57,6 +59,8 @@ class PathAliasTest extends PathTestBase {
|
||||||
|
|
||||||
// Visit the alias for the node and confirm a cache entry is created.
|
// Visit the alias for the node and confirm a cache entry is created.
|
||||||
\Drupal::cache('data')->deleteAll();
|
\Drupal::cache('data')->deleteAll();
|
||||||
|
// @todo Remove this once https://www.drupal.org/node/2480077 lands.
|
||||||
|
Cache::invalidateTags(['rendered']);
|
||||||
$this->drupalGet(trim($edit['alias'], '/'));
|
$this->drupalGet(trim($edit['alias'], '/'));
|
||||||
$this->assertTrue(\Drupal::cache('data')->get('preload-paths:' . $edit['source']), 'Cache entry was created.');
|
$this->assertTrue(\Drupal::cache('data')->get('preload-paths:' . $edit['source']), 'Cache entry was created.');
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,6 +75,7 @@ class SearchController extends ControllerBase {
|
||||||
|
|
||||||
// Build the form first, because it may redirect during the submit,
|
// Build the form first, because it may redirect during the submit,
|
||||||
// and we don't want to build the results based on last time's request.
|
// and we don't want to build the results based on last time's request.
|
||||||
|
$build['#cache']['contexts'][] = 'url.query_args:keys';
|
||||||
if ($request->query->has('keys')) {
|
if ($request->query->has('keys')) {
|
||||||
$keys = trim($request->get('keys'));
|
$keys = trim($request->get('keys'));
|
||||||
$plugin->setSearch($keys, $request->query->all(), $request->attributes->all());
|
$plugin->setSearch($keys, $request->query->all(), $request->attributes->all());
|
||||||
|
|
|
@ -60,8 +60,8 @@ class ConfigCacheTag implements EventSubscriberInterface {
|
||||||
$this->cacheTagsInvalidator->invalidateTags(['route_match', 'rendered']);
|
$this->cacheTagsInvalidator->invalidateTags(['route_match', 'rendered']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Global theme settings.
|
// Theme configuration and global theme settings.
|
||||||
if ($event->getConfig()->getName() === 'system.theme.global') {
|
if (in_array($event->getConfig()->getName(), ['system.theme', 'system.theme.global'])) {
|
||||||
$this->cacheTagsInvalidator->invalidateTags(['rendered']);
|
$this->cacheTagsInvalidator->invalidateTags(['rendered']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,11 @@ class BatchTestMultiStepForm extends FormBase {
|
||||||
'#value' => 'Submit',
|
'#value' => 'Submit',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// This is a POST form with multiple steps that does not transition from one
|
||||||
|
// step to the next via POST requests, but via GET requests, because it uses
|
||||||
|
// Batch API to advance through the steps.
|
||||||
|
$form['#cache']['max-age'] = 0;
|
||||||
|
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ class TestControllers {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEntityLanguage(NodeInterface $node) {
|
public function testEntityLanguage(NodeInterface $node) {
|
||||||
return ['#markup' => $node->label()];
|
$build = ['#markup' => $node->label()];
|
||||||
|
\Drupal::service('renderer')->addCacheableDependency($build, $node);
|
||||||
|
return $build;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@ class ConfirmFormHelperTest extends UnitTestCase {
|
||||||
|
|
||||||
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
|
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
|
||||||
$this->assertSame($cancel_text, $link['#title']);
|
$this->assertSame($cancel_text, $link['#title']);
|
||||||
|
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,6 +50,7 @@ class ConfirmFormHelperTest extends UnitTestCase {
|
||||||
->will($this->returnValue($cancel_route));
|
->will($this->returnValue($cancel_route));
|
||||||
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
|
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
|
||||||
$this->assertEquals(Url::fromRoute($route_name), $link['#url']);
|
$this->assertEquals(Url::fromRoute($route_name), $link['#url']);
|
||||||
|
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,6 +66,7 @@ class ConfirmFormHelperTest extends UnitTestCase {
|
||||||
->will($this->returnValue($expected));
|
->will($this->returnValue($expected));
|
||||||
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
|
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
|
||||||
$this->assertEquals($expected, $link['#url']);
|
$this->assertEquals($expected, $link['#url']);
|
||||||
|
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -86,6 +89,7 @@ class ConfirmFormHelperTest extends UnitTestCase {
|
||||||
->will($this->returnValue($cancel_route));
|
->will($this->returnValue($cancel_route));
|
||||||
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
|
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
|
||||||
$this->assertSame($cancel_route, $link['#url']);
|
$this->assertSame($cancel_route, $link['#url']);
|
||||||
|
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -112,6 +116,7 @@ class ConfirmFormHelperTest extends UnitTestCase {
|
||||||
|
|
||||||
$link = ConfirmFormHelper::buildCancelLink($form, new Request($query));
|
$link = ConfirmFormHelper::buildCancelLink($form, new Request($query));
|
||||||
$this->assertSame($url, $link['#url']);
|
$this->assertSame($url, $link['#url']);
|
||||||
|
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue