Score:1

Cache two sets of nodes

fr flag

I have a website where I have some blocks that have one content on all nodes, but different content on nodes with certain NID.

Right now, I have condition in block template based on node ID and blocks have route cache context and node id tags and this works perfectly.

However, this makes a new block cache entry for every NID/route, but in fact, there are only two block variants.

How would one optimize this in the most drupal-ish way so there are only two variants of the block cached?

in flag
The route context is most likely causing it to vary by route. You'll have to provide your own `cache.context`-tagged service so you can customize how Drupal makes cache variations of that block based on your own custom logic.
id flag
Show us your code.
Score:0
fr flag

As suggested in the comment, the solution to this problem is to provide its own cache context that checks if the current node id matches a list of nodes, and returns a different context string if this is true or not.

Then use this cache context on own block.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.