Skip to content

Commit

Permalink
HTMX support (#1629)
Browse files Browse the repository at this point in the history
* Prevent duplicate debugbar on HTMX request with a target

* Adding tests for HTMX requests
  • Loading branch information
rojtjo authored Jun 21, 2024
1 parent 3b2de38 commit f6ad331
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,10 @@ protected function isDebugbarRequest()
*/
protected function isJsonRequest(Request $request)
{
// If XmlHttpRequest or Live, return true
if ($request->isXmlHttpRequest() || $request->headers->has('X-Livewire')) {
// If XmlHttpRequest, Live or HTMX, return true
if ($request->isXmlHttpRequest() ||
$request->headers->has('X-Livewire') ||
($request->headers->has('Hx-Request') && $request->headers->has('Hx-Target'))) {
return true;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/DebugbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,25 @@ public function testItDoesntInjectOnJson()
$this->assertFalse(Str::contains($crawler->content(), 'debugbar'));
$this->assertEquals(200, $crawler->getStatusCode());
}

public function testItDoesntInjectsOnHxRequestWithHxTarget()
{
$crawler = $this->get('web/html', [
'Hx-Request' => 'true',
'Hx-Target' => 'main',
]);

$this->assertFalse(Str::contains($crawler->content(), 'debugbar'));
$this->assertEquals(200, $crawler->getStatusCode());
}

public function testItInjectsOnHxRequestWithoutHxTarget()
{
$crawler = $this->get('web/html', [
'Hx-Request' => 'true',
]);

$this->assertTrue(Str::contains($crawler->content(), 'debugbar'));
$this->assertEquals(200, $crawler->getStatusCode());
}
}

0 comments on commit f6ad331

Please sign in to comment.