spatie/laravel-blade-comments
Adds HTML debug comments around every rendered Blade view/component so you can see exactly which template produced each piece of output in browser dev tools. Also includes top-level request and view info at the top of the document.
BladeCommentsPrecompiler) to inject comments during template compilation, avoiding runtime overhead in production.BladeCommenter and RequestCommenter interfaces allow TPMs to extend functionality (e.g., adding custom metadata like user context or performance metrics).--dev), with no runtime impact in production (configurable via APP_DEBUG).AddRequestComments) to inject request metadata (e.g., route, view name) into the <head> or <body>. This requires the middleware to be registered in app/Http/Kernel.php (or via service provider).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Performance | Minimal runtime overhead in dev (precompilation step). In production, disabled by default (enable: env('APP_DEBUG')). |
Monitor compilation time; exclude high-traffic views from comments. |
| Template Bloat | Adds HTML comments to every Blade file, increasing payload size slightly (~1–5KB per page). | Use exclusion lists for performance-critical views; disable in staging. |
| Blade Parser Changes | Relies on Laravel’s Blade parser (AST-based in v2.0+). Breaking changes in future Laravel versions (e.g., Blade component syntax) could require updates. | Track Laravel minor versions; test against new releases early. |
| Livewire Compatibility | Supports Livewire v3/v4 but may lag behind major versions. | Pin Livewire version in composer.json if using unsupported versions. |
| Middleware Conflicts | Middleware runs early in the pipeline. Conflicts unlikely but possible with other middleware modifying the response (e.g., caching, compression). | Test with existing middleware; adjust priority in Kernel.php if needed. |
| Customization Complexity | Extending with custom commenters requires understanding regex/Blade AST. | Provide examples in docs; offer a CustomCommenter boilerplate in the repo. |
Debugging Workflow:
Environment Scope:
Extensibility Needs:
RequestCommenter/BladeCommenter?CI/CD Impact:
Long-Term Maintenance:
Installation:
composer require spatie/laravel-blade-comments --dev
php artisan vendor:publish --tag="blade-comments-config"
excludes, middleware, or blade_commenters.Middleware Registration:
Add to app/Http/Kernel.php (or service provider):
protected $middleware = [
// ...
\Spatie\BladeComments\Http\Middleware\AddRequestComments::class,
];
Testing:
APP_DEBUG=true).@include directives.<x-component />).@livewire).@section/@yield).Exclusions:
Configure config/blade-comments.php to exclude:
'excludes' => [
'includes' => ['partials.css', 'scripts.js'],
'sections' => ['meta', 'canonical'],
],
| Component | Compatibility Notes |
|---|---|
| Laravel 9–13 | Officially supported. Tested with Laravel 11/12/13 in changelog. |
| Livewire | Supports v3/v4 (via LivewireComponentCommenter). May need updates for v5+. |
| Blade Components | Works with both traditional (@component) and new syntax (<x- />). |
| Caching | Blade view caching may hide comments if views are cached. Disable caching for dev or use php artisan view:clear. |
| Frontend Frameworks | No impact on Vue/React/Alpine if using Blade for server-side rendering (e.g., Inertia). |
| Static Site Generators | Not compatible (e.g., Laravel Vapor, Octane with static rendering). |
Phase 1: Local Dev Adoption
<!-- /resources/views/partials/header.blade.php -->).Phase 2: Team Onboarding
APP_DEBUG=true.Phase 3: Staging/QA (Optional)
Phase 4: Customization (If Needed)
// app/Commenters/CustomBladeCommenter.php
use Spatie\BladeComments\Commenters\BladeCommenters\BladeCommenter;
class CustomBladeCommenter implements BladeCommenter {
public function pattern(): string { return '/@customDirective(.*)/'; }
public function replacement(): string { return '<!-- CUSTOM: $1 -->'; }
}
Register in config:
'blade_commenters' => [
// ... default commenters
App\Commenters\CustomBladeCommenter::class,
],
How can I help you explore Laravel packages today?