Leverage hx-swap-oob for assets

Created on 28 March 2024, 8 months ago
Updated 8 May 2024, 7 months ago

Problem/Motivation

When content is returned via an HTMX supported route, the assets attached to the response need a means to be added to the page. Drupal core does this by the following classes working in concert:

  • Drupal\Core\Ajax\AjaxResponseAttachmentsProcessor
  • Drupal\Core\EventSubscriber\AjaxResponseSubscriber
  • Drupal\Core\Ajax\CommandWithAttachedAssetsInterface
  • Drupal\Core\Ajax\CommandWithAttachedAssetsTrait

Proposed resolution

  1. Extend Drupal\Core\EventSubscriber\HtmlResponseSubscriber with AssetSwappingRepsonseSubscriber, and set the weight higher so that it runs before HtmlResponseSubscriber.
  2. Override processAssetLibraries
    1. Check the request for an HX-Request header.
    2. If the header exists, add attributes to new assets
      • CSS and Head JS: hx-swap-oob="beforeend:head"
      • Bottom JS: hx-swap-oob="beforeend:body"
    3. Use a JS event handler to respond to htmx:configRequest and add the additional parameter for existing libraries as is done in Drupal.Ajax.beforeSerialize.

Remaining tasks

πŸ“Œ Task
Status

Fixed

Version

1.0

Component

Drupal Code

Created by

πŸ‡ΊπŸ‡ΈUnited States fathershawn New York

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