Problem/Motivation
When not using the Drupal "Page title" block directly, but within a layout builder region, we came across a case where the generated title is cached from the first visited page.
Steps to reproduce
- Clear cache
- Visit page A with "Title A"
- Visit page B wih "Title B" but see the cached "Title A" displayed
Probably this only happens in cases where the Page title block is wrapped into other cached page elements, like for example Layout builder.
In the affected project, we have the following messy workaround present to be able to override the title at all using this module:
{#
To make modules like metatag_page_heading work, we need to print
the page title block using a region. Otherwise the page_title preprocess
hook will not work.
See page.html.twig for details.
#}
{% block field %}
{% set display_field_tag = false %}
{% set display_items_wrapper_tag = false %}
{% set display_item_tag = false %}
{{ parent() }}
{% endblock %}
{% block field_item_value %}
{{ drupal_region('page_title') }}
{# {{ item.content }} #}
{% endblock field_item_value %}which seems to make this issue visible because the title is only calculated once and then returned from cache.
Interestingly in other (newer) projects metatag_page_heading_preprocess_page_title() is called on every page load, where I'd have expected it to be called only once per URL or entity... so looks like our fix won't make anything worse!
Proposed resolution
We'll for now add cache contexts that resolve the problem. Still the solution seems a bit unclean and I'm not totally sure if the issue is within this module or is in a different layer and the title block should already have these cache contexts...
// Without adding page context here, it may happen that the title is cached across (different) pages:
// @todo: If possible we should get this information from the metatags module through $bubbleable_metadata
// but we couldn't find a way yet to do that properly. So for now we use these two most relavant:
$variables['#cache']['contexts'][] = 'url';
$variables['#cache']['contexts'][] = 'languages';
Remaining tasks
Reopen this or create a follow-up if anyone has a cleaner solution!
User interface changes
None
API changes
None
Data model changes
None
Issue fork metatag_page_heading-3552958
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
anybodyComment #4
anybodyInterestingly, in other (newer) projects
metatag_page_heading_preprocess_page_title()is called on every page load, where I'd have expected it to be called only once per URL or entity... so looks like our fix won't make anything worse!Comment #5
anybody