Improve the exception from \Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber::wrapControllerExecutionInRenderContext by linking to an example of how to use executeInRenderContext

Created on 6 July 2023, 12 months ago

Problem/Motivation

When returning a CacheableResponse from a controller that has cacheability metadata but isn't collecting it all correctly, you can receive this error.

The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early.

It comes from \Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber::wrapControllerExecutionInRenderContext

Steps to reproduce

Add a controller that renders something and returns a CacheableResponse

Proposed resolution

Improve the error message by linking to a docs page with concrete instructions on how to use ::executeInRenderContext

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
RenderΒ  β†’

Last updated about 21 hours ago

Created by

πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

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

Comments & Activities

  • Issue created by @larowlan
  • πŸ‡«πŸ‡·France fgm Paris, France

    One reason why it's such an issue is that this rendering may be entirely hidden from developers building APIs without touching the render system.

    One such case would be having to prepare a URL and needing it as a string for inclusion in a cacheable JSON response or a redirect. This goes through Url::toString(TRUE) (to obtain the cacheability metadata for the route) which will eventually invoke MetadataBubblingUrlGenerator::generateFromRoute, introducing the renderer for what appears to be a non-rendered response.
    This is entirely non-obvious before digging into the execution trace, and mystifying even after finding it: without digging into the arcane of rendering context, there appears to be no reason to even touch the renderer just to get a URL from a route.

Production build 0.69.0 2024