Enable cacheability context in Shortcode plugins

Created on 2 August 2025, 17 days ago

Problem/Motivation

Currently, the Shortcode module does not provide a way for shortcode plugins to expose cacheable metadata such as cache contexts, cache tags, or max-age.
This leads to rendered content not varying properly by user, language, or other contexts when a shortcode output depends on them.
This can cause incorrect content to be served from cache, especially on multilingual or user-specific pages.

Steps to reproduce

Create a shortcode plugin that outputs user-specific or language-specific content.
Place the shortcode in a node body and view the page as an anonymous user.
Log in as a different user or switch languages.
Observe that the output is cached and does not vary based on the expected context.

Proposed resolution

Text filters already support cacheability metadata through FilterProcessResult, which extends BubbleableMetadata.
Shortcode module should allow shortcode plugins to opt-in to cacheable metadata by implementing CacheableDependencyInterface.
The Shortcode module would then:

- Aggregate cacheable metadata from each shortcode plugin instance.
- Merge it into the render pipeline to ensure proper cache invalidation and variation.

Remaining tasks

- Update Shortcode plugin system to check if a plugin implements CacheableDependencyInterface.
- Collect and merge cache tags, contexts, and max-age during shortcode processing.
- Add test coverage for cache variation with shortcodes.
- Update documentation for shortcode plugin developers.

User interface changes

None. This is a backend-only change that affects rendering and caching.

API changes

- Shortcode plugins can optionally implement CacheableDependencyInterface to provide cache metadata.
- Shortcode processing service will merge metadata into the parent render array.

Data model changes

Feature request
Status

Active

Version

2.0

Component

Miscellaneous

Created by

🇳🇴Norway zaporylie

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

Comments & Activities

  • Issue created by @zaporylie
  • @zaporylie opened merge request.
  • 🇳🇴Norway zaporylie

    MR is open and default implementation added for some of the basic shortcodes, including the [random] one that if [random cache=0 /] is used the shortcode will be rendered on every page load (maxAge = 0)

Production build 0.71.5 2024