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
- Enable Dynamic Page Cache, Navigation, Responsive Preview and Responsive Preview Navigation.
- Log in as a user with the "Use the Navigation" and "Access responsive preview" permissions.
- Visit a node page that Dynamic Page Cache stores (
X-Drupal-Dynamic-Cache: MISSon the first load,HITon reload). - Load the same URL with
?responsive_preview=enabled: the Navigation sidebar and top bar are shown, and the response is aHIT.
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.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 3635049-4.patch | 3.32 KB | joevagyok |
Issue fork responsive_preview-3635049
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 #2
joevagyok commentedComment #4
joevagyok commentedAdding a patch file based on hte MR for composer patching.
Comment #5
joevagyok commentedComment #6
ishfaqafridi85 commentedFor 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.
Comment #7
mrpauldriver commentedConfirm the patch works nicely.