Skip to content

Fix Blaze Debug Bar Injection for Multi-Line Body Tags#133

Merged
ganyicz merged 2 commits intolivewire:mainfrom
faraweilyas:faraweilyas/improve-regex-match
Mar 12, 2026
Merged

Fix Blaze Debug Bar Injection for Multi-Line Body Tags#133
ganyicz merged 2 commits intolivewire:mainfrom
faraweilyas:faraweilyas/improve-regex-match

Conversation

@faraweilyas
Copy link
Contributor

Problem

Blaze debug bar was injecting its HTML content inside Alpine.js x-data attributes instead of after the <body> tag, causing malformed HTML and broken JavaScript objects.

Root Cause: The regex /<body[^>]*>/i in DebuggerMiddleware.php only matches single-line body tags. our admin layout uses multi-line body tags with complex Alpine.js attributes:

<body @keydown.window.prevent.cmd.k="..." 
      :class="..." 
      x-data="{...}" 
      x-init="...">

This caused the debug bar to inject inside the x-data attribute value, breaking the JavaScript object syntax.

Solution

Updated regex to handle multi-line body tags with quoted attributes:

'/<body(?:[^>"\']*|"[^"]*"|\'[^\'"]*\')*>/si'

Impact

  • Fixes debug bar injection for pages with complex body tag attributes
  • No breaking changes to existing functionality
  • Improves developer experience with accurate performance profiling

@github-actions
Copy link
Contributor

github-actions bot commented Mar 12, 2026

Benchmark Results

Attempt Blade Blaze Improvement
#1 349.04ms 16.63ms 95.2%
#2 351.14ms 16.73ms 95.2%
#3 348.82ms 16.48ms 95.3%
#4 348.96ms 16.78ms 95.2%
#5 350.97ms 16.83ms 95.2%
#6 354.68ms 16.95ms 95.2%
#7 355.17ms 16.66ms 95.3%
#8 354.46ms 16.96ms 95.2%
#9 349.45ms 16.81ms 95.2%
#10 350.55ms 16.92ms 95.2%
Snapshot 349.37ms 16.88ms 95.2%
Result 350.76ms (~) 16.80ms (~) 95.2% (~)

Median of 10 attempts, 5000 iterations x 10 rounds, 46.39s total

@ganyicz
Copy link
Collaborator

ganyicz commented Mar 12, 2026

Updated to implementation from Laravel Debugbar which adds the widget before closing </body>

@ganyicz ganyicz merged commit d848280 into livewire:main Mar 12, 2026
4 checks passed
@ganyicz
Copy link
Collaborator

ganyicz commented Mar 12, 2026

Thanks!

@faraweilyas
Copy link
Contributor Author

There's an error with your implementation though:

Livewire\Blaze\DebuggerMiddleware::injectDebugger(): Argument #1 ($response) must be of type Illuminate\Http\Response, Illuminate\Http\JsonResponse given, called in vendor/livewire/blaze/src/DebuggerMiddleware.php on line 62

i tried this below to fix if, but i noticed blaze profiler was injected into json responses as well, we might need to review your implementation.

use Illuminate\Http\JsonResponse;

public function handle(Request $request, Closure $next): JsonResponse|Response
{
    // ...

protected function injectDebugger(JsonResponse|Response $response, Debugger $debugger): void
{
    // ...

@ganyicz
Copy link
Collaborator

ganyicz commented Mar 13, 2026

@faraweilyas pull in dev-main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants