Shawn Duncan Explains How Drupal 11.3 Renders HTMX Responses
Developers using HTMX with Drupal 11.3 can request a reduced HTML response instead of the full page output. Shawn Duncan, Drupal contributor, explains the rendering process in Rendering Content for HTMX. He focuses on two ways to invoke HtmxRenderer: the _htmx_route route option and the drupal_htmx wrapper format.
Duncan traces the route-option path through HtmxContentViewSubscriber, which listens for the kernel.view event at a higher priority than Drupal's standard MainContentViewSubscriber. When a qualifying route requests an HTML response, the subscriber passes the render array to HtmxRenderer and supplies the resulting response before the standard subscriber processes the event. The wrapper-format path instead lets MainContentViewSubscriber select the same renderer when _wrapper_format is set to drupal_htmx. Drupal varies caching by that parameter, allowing the same path to maintain separate representations for full-page and HTMX requests.
The reduced document contains status messages, the main content, and placeholders for required CSS and JavaScript assets, without the surrounding regions of a standard Drupal page. Drupal's HtmlResponseAttachmentsProcessor replaces those placeholders, while the HTMX integration JavaScript handles the assets on the requesting page. Duncan says work for Drupal 12 includes more automatic handling of status-message swaps and that the JavaScript integration is expected to change with developments in HTMX 4. Those changes are future-facing and are not part of the Drupal 11 behaviour covered in the walkthrough.

