Problem/Motivation

Looks like htmx:beforeRequest is never triggered, and requestAssetsLoaded isn't populated, causing htmx:beforeSwap to fail with Uncaught TypeError: can't access property "then", requestAssetsLoaded.get(...) is undefined.

Steps to reproduce

Add the following controller:

# htmx_example.routing.yml
htmx_example.htmx_response:
  path: '/htmx-example/htmx'
  defaults:
    _controller: '\Drupal\htmx_example\Controller\HtmxExampleController::htmx'
  options:
    _htmx_route: 'TRUE'
  requirements:
    _permission: 'access content'

# src/Controller/HtmlExampleController.php

declare(strict_types=1);

namespace Drupal\htmx_example\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Htmx\Htmx;
use Drupal\Core\Url;

final class HtmxExampleController extends ControllerBase {

  public function htmx(): array {
    return [
      '#type' => 'markup',
      '#markup' => 'Some content',
    ];
  }

}

and a render array like:

$htmx = new Htmx();
$htmx->get(new Url('htmx_example.htmx_response'))
  ->trigger('load');

$build = [
  '#type' => 'html_tag',
  '#tag' => 'div',
];
$htmx->applyTo($build);

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

tuutti created an issue. See original summary.

tuutti’s picture

Title: htmx:beforeSwap fails on GET requests » htmx:beforeSwap fails with TypeError requestAssetsLoaded.get(...) is undefined
Issue summary: View changes

Actually, this seems to have nothing to do with GET vs. POST requests. The original code appeared to work after switching to POST, but that didn't actually fix the issue.