Drupal 8.
Please help to find the best solution.
I have nodes that should display created date. If node created today text should be - "Today" If yesterday - "Yesterday". In other scenarios should display just creation date - 22/02/1999.
I've created custom field formatter to achieve this. And everything ok except one thing - caches.
Due drupal caches the node that created today, tomorrow should display "Yesterday". But tomorrow it will still display "Today" until I clear the cache manually. After cache clearing, it will say "Yesterday", like it should.
Please help me to resolve this issue. I will be glad to avoid using
'#cache' => [
'max-age' => 0,
]
In my opinion, the best solution for your problem is to use
'#cache' => ['max-age' => 0]
for your custom field formatter because only that field will have to be re-rendered each time node hits. That doesn't affect the performance of node page much.But if you must avoid using cache max-age, you can try the following approach:
'node: <nid>'
for those nodes.