Problem/Motivation

With Responsive Preview Navigation enabled, a page and its preview (?responsive_preview=enabled) get the same Dynamic Page Cache entry. Whichever is cached first is served for both:

  • Normal page cached first: the preview shows the Navigation sidebar and top bar.
  • Preview cached first: the normal page has no Navigation sidebar or top bar until the cache is cleared.

Pages that Dynamic Page Cache cannot store are not affected.

ResponsivePreview::preprocessHtml() removes the admin UI based on the query parameter, but does not add a cache context for it. With Toolbar this works because the toolbar item has the url cache context and Toolbar is rendered inline, so the context bubbles up to the page. Navigation renders the sidebar and top bar as placeholders (NavigationRenderer::buildNavigation() and ::buildTopBar()), so the cacheability of the ResponsiveIcons top bar item never reaches the page.

Tested on Drupal 11.4.10 with Responsive Preview 2.3.2.

Steps to reproduce

  1. Enable Dynamic Page Cache, Navigation, Responsive Preview and Responsive Preview Navigation.
  2. Log in as a user with the "Use the Navigation" and "Access responsive preview" permissions.
  3. Visit a node page that Dynamic Page Cache stores (X-Drupal-Dynamic-Cache: MISS on the first load, HIT on reload).
  4. Load the same URL with ?responsive_preview=enabled: the Navigation sidebar and top bar are shown, and the response is a HIT.

The other way around: clear the cache, load the page with ?responsive_preview=enabled first, then without it. The normal page is a HIT and has no Navigation.

Proposed resolution

In preprocessHtml(), add the cache context on every page for users who can preview, not only in preview mode:

  public function preprocessHtml(array &$variables): void {
    if (!$this->currentUser->hasPermission('access responsive preview')) {
      return;
    }

    // The navigation top bar is a placeholder, so its cacheability does not
    // reach the page. Vary every page by the preview mode here instead.
    $variables['#cache']['contexts'] = Cache::mergeContexts(
      $variables['#cache']['contexts'] ?? [],
      ['url.query_args:' . static::RESPONSIVE_PREVIEW_QUERY_PARAMETER],
    );

    // Remove the admin toolbar/navigation when in same page preview mode.
    if ($this->requestStack->getCurrentRequest()->query->get(static::RESPONSIVE_PREVIEW_QUERY_PARAMETER) === static::RESPONSIVE_PREVIEW_ENABLED
    ) {
      // Unchanged.
    }
  }

With this change, the normal page and the preview are each cached once, and later HITs return the right one, in both orders.

Remaining tasks

  • MR with the fix.
  • Functional test with Navigation enabled: load a cacheable node page and its preview, in both orders, and check for the Navigation markup in each response.

User interface changes

None.

API changes

None.

Data model changes

None.

CommentFileSizeAuthor
#4 3635049-4.patch3.32 KBjoevagyok
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

joevagyok created an issue. See original summary.

joevagyok’s picture

Issue summary: View changes

joevagyok’s picture

StatusFileSize
new3.32 KB

Adding a patch file based on hte MR for composer patching.

joevagyok’s picture

Status: Active » Needs review
ishfaqafridi85’s picture

Status: Needs review » Reviewed & tested by the community

For this contribution,
I used an AI agent to review and test MR !45 on Drupal 11.4.11 / PHP 8.3.30. The issue reproduced before the fix in both cache loading orders. Afterward, all 10 functional tests passed (311 assertions), including sidebar/top-bar visibility and permission checks. No blocking functional issue was found.

I am going to change the status to RTBC.

mrpauldriver’s picture

Confirm the patch works nicely.