![]() ![]() * Page response subscriber to set appropriate headers on anonymous requests. Use Symfony\Component\HttpKernel\KernelEvents Use Symfony\Component\HttpKernel\Event\FilterResponseEvent Use Symfony\Component\EventDispatcher\EventSubscriberInterface Use Drupal\Core\Session\AccountInterface Use Drupal\Core\Cache\CacheableResponseInterface Use Drupal\Component\Datetime\TimeInterface Our goal is to have a way to say to Drupal, "On this page, please use the bubbled up max-age from the render cache in the response's headers." We do that by implementing an event subscriber service such as: namespace Drupal\foo\EventSubscriber An alternative approach could be to explore the Cache-Control Override module, but there are some limitations to how it handles things (more on this below). Cache contexts: Cache context is used when our renderable arrays depend on some context such as user role, theme or URL. bi ny chng ta s to mt cache context v chng ta l mt Drupal developers m, v chng ta thch code hoc mun ty chnh nhiu. The one we will use is similar to comment on because we want to be very precise about which pages we affect and those we don't. Trong bi Cache Context Part 1, chng ta ni v cache context l g v n khc g vi cache context trong Drupal core. How do we change the max-age in the response sent to the browser? We want 60s for this particular page. (✋ For the record, if you are curious, in this particular case, we are using a very large site-wide max-age because we are using the purge module to invalidate page cache, and this is the recommended max-age for configurations such as this one.) Drupal will use that system-wide max-age for all anonymous responses if we don't do anything to change that. Since that issue is rather big, here's a summary of some important aspects of it below.įirst of all, the max-age=31536000 we see in the example above is what was defined in " Configuration" -> "Development" -> "Performance": Cache maximum age (in other words, the config value system.performance:_age ). There is an issue about it on that you can follow if you are interested in this topic and haven't already done so. It turns out, as of today, that Drupal doesn't automatically reflect bubbled up render array max-ages to the response header max-age. Why does the max-age=31536000 if we deliberately set it to 60 in our block? Retrieve our remote data, and populate the $build render array.Īfter rebuilding our caches so Drupal knows about our block and placing the block on the page, we are ready to check if the 60s is now reflected on the response headers: Often, the implications will be minimal, if there are any at all. * Provides a block with time-sensitive content. Since block plugins implement \Drupal\Core\Cache\CacheableDependencyInterface, all we need to do seems to be to create this handy method ::getCacheMaxAge() in our block and we should be done! namespace Drupal\foo\Plugin\Block Uses cache context services (services tagged with ntext, and whose service ID has the cachecontext. The approach that seems most straightforward for us at this point is to create a custom block plugin and define its max-age to be the interval we want to keep the information cached in Drupal (one minute). Converts cache context tokens into cache keys. It cached computed output for a fixed period of time (e.g. Up until Drupal 8, Drupal has had one caching strategy called cache expiration. I don't use the core's REST module on purpose since I find it hard to configure to output exactly what I need. Cache tags are a game changer for your caching strategy in Drupal 8. ![]() debug(1) always gets called, I guess I just somehow have to tell Drupal that this JSON response's cache should relate to that route. If that's not the case, make sure you glance over the official documentation or watch one of the numerous videos about it, for example, this excellent presentation by Wim Leers at DrupalCon Vienna.) Thanks to this answer I know how to actually add the URL params as a cache context, but the. (✋ From now on, I will assume you have some basic level of understanding of Drupal's caching system and its relevant parts. The moment we start talking about time-based content dependencies, probably the first thing that comes to our minds is the max-age property of Drupal’s cache. Globally, there are two ways to write a changelog:1 2 3 4 5 6 7 8 9 10 11 12 13 14. Well, maybe a 1 min difference would be acceptable since that's the smallest interval we display on the site. I would be good if drupal by default come with a rule in. Therefore I have implemented a hook_menu_local_tasks_alter() in my_module/my_module.As you can imagine, this information is very dynamic, and we must ensure that what we display on the Drupal site is always the most up-to-date information we receive from the API. I have the following situation: I want to hide or show some local Tasks (Tabs) based on a field on the current user. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |