Timeline for How do I correctly setup caching for my custom block showing content depending on the current node?
Current License: CC BY-SA 4.0
18 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 10, 2020 at 19:13 | comment | added | 4uk4 | @leymannx , yes, but for placeholdering to work the online status needs to be in a lazy builder callback. For blocks this is default, but not for entities, see BlockViewBuilder::viewMultiple. | |
| Mar 10, 2020 at 17:47 | comment | added | norman.lol | Another off-topic: Would you consider it a good tactic to max age 0 a piece of markup placed via a pseudo field to display user online statuses? git.drupalcode.org/project/user_status_online/-/blob/8.x-1.4/… – From what I know now it should be OK, shouldn't it? | |
| Mar 10, 2020 at 17:43 | comment | added | norman.lol | Ah OK, so thanks to renderer.config's auto_placeholder_conditions the auto-placeholdering kicks in and interrupts upbubbling of that very element/subtree that has defined ['#cache']['max-age'] = 0 and the rest of the page/tree can follow its own logic. | |
| Mar 10, 2020 at 17:28 | comment | added | 4uk4 | @leymannx, for blocks which change very often this is actually a good idea, because this meets the default auto-placeholdering conditions and the page can be cached without the block, see drupal.org/docs/8/api/render-api/auto-placeholdering | |
| Mar 10, 2020 at 17:17 | comment | added | norman.lol | Off-topic: You often see people recommending ['#cache']['max-age'] = 0 to fix minor issues. But isn't this then bubbling up until completely disabling the cache of the current page? | |
| Dec 3, 2019 at 13:18 | history | edited | 4uk4 | CC BY-SA 4.0 | added 351 characters in body |
| Dec 3, 2019 at 9:20 | history | edited | 4uk4 | CC BY-SA 4.0 | added 226 characters in body |
| Nov 14, 2017 at 10:04 | comment | added | 4uk4 | Yes, @leymannx, it's as simple as this. This threads seems to overthinking the issue. | |
| Nov 14, 2017 at 9:34 | comment | added | norman.lol | For me return [ '#markup' => render($output), '#cache' => [ 'contexts' => ['url'] ] ]; works super fine for per URL caching. | |
| Apr 28, 2016 at 13:48 | comment | added | Alex | thanks for your help, it didnt quite work for me though, but its almost the same solution als Vagners | |
| Apr 28, 2016 at 13:23 | comment | added | Alex | isnt that super weird? you would think the entity does this itself? i am trying... | |
| Apr 28, 2016 at 13:22 | history | edited | 4uk4 | CC BY-SA 3.0 | added 169 characters in body |
| Apr 28, 2016 at 13:22 | comment | added | 4uk4 | OK, then the node does not provide the contexts we need. You can try to set it manually, see the edit in my answer. | |
| Apr 28, 2016 at 13:16 | comment | added | Alex | this doesnt work. the block always outputs the first visited node's id | |
| Apr 28, 2016 at 12:50 | comment | added | avpaderno♦ | @Vagner You can set the cache of a Block object; The BlockBase class has even the necessary methods. | |
| Apr 28, 2016 at 12:48 | comment | added | 4uk4 | #markup can be cached the same as any other render element. In this case it is not the markup, but the block, which is cached and here is the problem. You can't solve it with cache tags, because they only get invalidated, if the node is changed in the database. | |
| Apr 28, 2016 at 12:33 | comment | added | Vagner | I don't think that can set cache of Block object. '#markup' is just a Render Element object and there is no reason to set cache context or tag. The block object that needs to rebuild when cache is invalidate. | |
| Apr 28, 2016 at 12:13 | history | answered | 4uk4 | CC BY-SA 3.0 |