AJAX placeholders starts a session and breaks page caching for anonymous users

Created on 5 June 2025, 29 days ago

Problem/Motivation

The AJAX placeholder module uses the shared tempstore to store the callbacks for things it wants to render. When using AJAX placeholders for anonymous users, the shared tempstore creates a session in \Drupal\Core\TempStore\SharedTempStoreFactory::get():

    // Use the currently authenticated user ID or the active user ID unless
    // the owner is overridden.
    if (!isset($owner)) {
      $owner = $this->currentUser->id();
      if ($this->currentUser->isAnonymous()) {
        $owner = $this->requestStack->getSession()->get('core.tempstore.shared.owner', Crypt::randomBytesBase64());
      }
    }

The created session then breaks the Drupal page cache, since Drupal\Core\PageCache\RequestPolicy\NoSessionOpen automatically denies the page cache for all users that have a session.

Steps to reproduce

  1. Enable the page cache module and AJAX placeholder module
  2. Visit a page and verify the page can be served from the page cache.
  3. Render an element on a page using an ajax_placeholder element
  4. The page that was previously served from the cache, is now no longer cached.

Proposed resolution

Since the session is only created when no owner is passed to \Drupal\Core\TempStore\SharedTempStoreFactory::get(), we should pass an owner ID.

Remaining tasks

Write a patch
Commit

User interface changes

None

API changes

None

Data model changes

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇳🇱Netherlands seanB Netherlands

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024