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

  1. Clear cache
  2. Visit page A with "Title A"
  3. 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

Command icon 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

anybody created an issue. See original summary.

anybody’s picture

Status: Active » Needs review
anybody’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

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!

anybody’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.