# Blaze
Blaze is a high-performance Laravel package that optimizes Blade component rendering by compiling templates into optimized PHP functions. It serves as a drop-in replacement for anonymous Blade components, eliminating 91-97% of rendering overhead while maintaining near-full feature parity with standard Blade. The package achieves dramatic performance improvements—rendering 25,000 components in 13ms compared to Blade's 500ms—by bypassing the standard rendering pipeline.
Beyond the default function compilation, Blaze offers two advanced optimization strategies for even greater performance gains. Runtime Memoization caches repeated component renders based on their props, ideal for icons and avatars that appear multiple times on a page. Compile-Time Folding pre-renders components into static HTML during compilation, achieving constant rendering time regardless of component count but requiring careful consideration of dynamic content and global state.
## Installation
Install Blaze via Composer to add optimized Blade component rendering to your Laravel application.
```bash
composer require livewire/blaze:^1.0@beta
```
After installation, clear compiled views to enable Blaze:
```bash
php artisan view:clear
```
## @blaze Directive
The `@blaze` directive enables Blaze optimization for individual anonymous components. Add it to the top of any component file to activate function compilation, with optional parameters for additional optimization strategies.
```blade
{{-- resources/views/components/button.blade.php --}}
@blaze
@props(['type' => 'button', 'variant' => 'primary'])
@php
$classes = match($variant) {
'primary' => 'bg-blue-500 hover:bg-blue-400 text-white',
'secondary' => 'bg-gray-200 hover:bg-gray-300 text-gray-800',
'danger' => 'bg-red-500 hover:bg-red-400 text-white',
default => 'bg-gray-500 text-white',
};
@endphp
```
Usage in parent templates:
```blade
{{-- resources/views/welcome.blade.php --}}
Save Changes
Delete
```
## Blaze::optimize() - Directory Configuration
Configure Blaze optimization for entire directories from your service provider using the fluent `optimize()` API. This approach is ideal for optimizing many components at once without modifying individual files.
```php
in(resource_path('views/components'));
// Enable for specific subdirectories with different strategies
Blaze::optimize()
->in(resource_path('views/components/ui'))
->in(resource_path('views/components/icons'), memo: true)
->in(resource_path('views/components/cards'), fold: true);
// Exclude specific directories from compilation
Blaze::optimize()
->in(resource_path('views/components'))
->in(resource_path('views/components/legacy'), compile: false);
}
}
```
## Runtime Memoization
Enable memoization to cache component output when the same props are used multiple times on a page. This strategy is ideal for icons, avatars, and other frequently repeated elements without slots.
```blade
{{-- resources/views/components/icon.blade.php --}}
@blaze(memo: true)
@props(['name', 'size' => 'md'])
@php
$sizeClasses = match($size) {
'sm' => 'w-4 h-4',
'md' => 'w-6 h-6',
'lg' => 'w-8 h-8',
default => 'w-6 h-6',
};
@endphp
```
Usage showing memoization benefits:
```blade
{{-- These render only once each, subsequent calls use cached output --}}
@foreach($tasks as $task)
{{ $task->title }}
@endforeach
```
## Compile-Time Folding
Enable folding to pre-render components into static HTML during compilation. This eliminates all runtime overhead but requires careful consideration—components must not use global state, and dynamic props need special handling.
```blade
{{-- resources/views/components/badge.blade.php --}}
@blaze(fold: true)
@props(['color' => 'gray'])
@php
$colorClasses = match($color) {
'green' => 'bg-green-100 text-green-800',
'red' => 'bg-red-100 text-red-800',
'blue' => 'bg-blue-100 text-blue-800',
'yellow' => 'bg-yellow-100 text-yellow-800',
default => 'bg-gray-100 text-gray-800',
};
@endphp
class(['px-2 py-1 rounded-full text-xs font-medium', $colorClasses]) }}>
{{ $slot }}
```
Usage with static props (folds successfully):
```blade
{{-- This gets compiled to static HTML at compile time --}}
ActiveInactive
{{-- Output after compilation: --}}
{{-- Active --}}
```
## Selective Folding with safe and unsafe
Control folding behavior for specific props and slots using the `safe` and `unsafe` parameters. Mark pass-through props as `safe` to allow folding even when dynamic, or mark transformed props/slots as `unsafe` to abort folding when they receive dynamic values.
```blade
{{-- resources/views/components/heading.blade.php --}}
{{-- 'level' is pass-through (not transformed), so mark it safe --}}
@blaze(fold: true, safe: ['level'])
@props(['level' => 2])
{{ $slot }}
```
```blade
{{-- resources/views/components/conditional-wrapper.blade.php --}}
{{-- Slot is used in conditional logic, mark as unsafe --}}
@blaze(fold: true, unsafe: ['slot'])
@if ($slot->hasActualContent())
{{ $slot }}
@else
No content provided
@endif
```
```blade
{{-- resources/views/components/link.blade.php --}}
{{-- href is read from attributes for logic, mark as unsafe --}}
@blaze(fold: true, unsafe: ['href'])
@php
$isActive = $attributes->get('href') === url()->current();
@endphp
class(['nav-link', 'active' => $isActive]) }}>
{{ $slot }}
```
Usage examples:
```blade
{{-- Dynamic level is safe because it's pass-through --}}
{{ $section->title }}
{{-- Dynamic href triggers fallback to function compilation --}}
Dashboard
{{-- Self-closing still folds, only slot content triggers abort --}}
{{-- Folds --}}
Content {{-- Falls back --}}
```
## @unblaze Directive
Use `@unblaze` to exclude dynamic sections from folding while keeping the rest of the component optimized. Variables from the component scope must be passed explicitly via the `scope` parameter.
```blade
{{-- resources/views/components/form-field.blade.php --}}
@blaze(fold: true)
@props(['name', 'label', 'type' => 'text'])
```
## @aware Directive Support
Blaze supports the `@aware` directive for accessing parent component data, but both parent and child components must use Blaze for values to propagate correctly.
```blade
{{-- resources/views/components/tabs.blade.php --}}
@blaze
@props(['active' => null])
```
```blade
{{-- resources/views/components/tab.blade.php --}}
@blaze
@props(['name'])
@aware(['active'])
```
Usage:
```blade
ProfileSettingsNotifications
```
## Memo API
The `Memo` class provides the runtime caching mechanism for memoized components. It generates cache keys from component names and props, storing rendered output for reuse within the same request.
```php
'check', 'size' => 'md']);
// Returns: 'blaze_memoized_icon:a1b2c3d4...'
// Check if output is cached
if (Memo::has($key)) {
echo Memo::get($key);
} else {
// Render and cache
$html = '';
Memo::put($key, $html);
echo $html;
}
// Clear all memoized entries (useful in tests)
Memo::clear();
// Remove specific entry
Memo::forget($key);
// Non-serializable props return null key (skip caching)
$key = Memo::key('component', ['callback' => fn() => 'test']);
// Returns: null
```
Blaze is designed to be a seamless performance upgrade for Laravel applications using anonymous Blade components. The default function compilation strategy works for virtually all components and provides significant performance improvements without any configuration changes. For applications with repeated components like icon libraries or design systems, memoization offers additional gains with minimal setup.
For maximum performance in specific use cases, compile-time folding can eliminate rendering overhead entirely, but requires understanding its constraints around dynamic content and global state. The `@unblaze` directive and `safe`/`unsafe` parameters provide fine-grained control for components that mix static and dynamic content. Whether optimizing an entire component library or targeting specific performance bottlenecks, Blaze's layered approach allows gradual adoption while maintaining full compatibility with existing Blade templates.