You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+91-59Lines changed: 91 additions & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,14 @@
1
1
# 🔥 Blaze
2
2
3
+
Speed up your Laravel app by optimizing Blade component rendering performance.
4
+
3
5
```
4
6
Rendering 25,000 anonymous components:
5
7
6
8
Without Blaze ████████████████████████████████████████ 500ms
7
9
With Blaze █ 13ms
8
10
```
9
11
10
-
## Introduction
11
-
12
-
Blaze supercharges your Blade components by compiling them into direct PHP function calls, eliminating 91-97% of the rendering overhead. No configuration changes, no template modifications.
> If you're using Flux, just install Blaze and you're good to go!
21
+
> If you're using [Flux UI](https://fluxui.dev), just install Blaze and you're good to go - no configuration needed!
24
22
25
-
## Usage
26
-
27
-
Blaze is designed as a drop-in replacement for anonymous Blade components. It supports all essential features including props, slots, attributes, and `@aware`. The HTML output is identical to standard Blade rendering.
28
-
29
-
### Limitations
30
23
31
-
Blaze focuses on anonymous components. The following are not supported:
24
+
## Introduction
32
25
33
-
- Class-based components
34
-
- The `$component` variable
35
-
-`View::share()` variables
36
-
- View composers / creators
37
-
- Component lifecycle events
26
+
Out of the box, Blaze is a **drop-in replacement** for anonymous coponents. It does not require any changes to your existing component code and works by compiling components into optimized PHP code - this eliminates 91-97% of the rendering overhead.
38
27
39
-
> [!IMPORTANT]
40
-
> When using `@aware`, both the parent and child components must use Blaze for values to propagate correctly.
28
+
Blaze also offers two additional optimization strategies for even greater performance:
29
+
-**Memoization** - a caching strategy for repeated components
30
+
-**Folding** - pre-rendering components into static HTML
41
31
42
-
### Enabling Blaze
32
+
##Getting started
43
33
44
-
Enable Blaze in your `AppServiceProvider`:
34
+
Enable Blaze in your `AppServiceProvider` to optimize all component paths:
45
35
46
36
```php
47
37
use Livewire\Blaze\Blaze;
@@ -52,14 +42,38 @@ public function boot(): void
52
42
}
53
43
```
54
44
55
-
This optimizes all [anonymous component paths](https://laravel.com/docs/12.x/blade#anonymous-component-paths).
56
-
57
45
After enabling Blaze, you should clear your compiled views:
58
46
59
47
```bash
60
48
php artisan view:clear
61
49
```
62
50
51
+
> [!CAUTION]
52
+
> **If you're installing Blaze into an existing app**, enabling Blaze in all component paths can break your app. Consider scoping Blaze to specific directories or components until you've worked around the limitations described below.
53
+
54
+
### Limitations
55
+
56
+
Blaze supports all essential features of anonymous components and produces HTML output that is identical to Blade. That said, there are some limitations:
57
+
58
+
#### Accessing parent data using `@aware`
59
+
60
+
While `@aware` is supported, you must use Blaze in both parent and child for values to propagate correctly.
61
+
62
+
#### Accessing variables shared using `View::share()`
63
+
64
+
Variables shared via `View::share()` will NOT be injected automatically. You have to access them manually:
65
+
66
+
```blade
67
+
{{ $__env->shared('key') }}
68
+
```
69
+
70
+
#### Unsupported features
71
+
72
+
-**Class-based components** are not supported
73
+
-**The `$component` variable** will not be accessible
Blaze offers three optimization strategies, each suited to different use cases:
121
+
By default Blaze uses **Function Compilation**, which works for virtually all components and provides significant performance improvements — this is sufficient for most use cases. To go even further, you can consider the other strategies that require additional considerations.
108
122
109
123
| Strategy | Parameter | Best For | Overhead Reduction |
| Compile-Time Folding |`fold`| Maximum performance | 100% |
114
128
115
-
### Function Compilation
129
+
Let's break down each strategy:
130
+
131
+
## Function Compilation
116
132
117
-
Function compilation is the default strategy. It transforms your components into optimized PHP functions, bypassing the entire component rendering pipeline while maintaining identical behavior to standard Blade.
133
+
This strategy transforms your components into optimized PHP functions, bypassing the entire component rendering pipeline while maintaining identical behavior to standard Blade.
118
134
119
-
####Benchmark Results
135
+
### Benchmark Results
120
136
121
137
The following benchmarks represent 25,000 components rendered in a loop:
122
138
@@ -132,7 +148,7 @@ The following benchmarks represent 25,000 components rendered in a loop:
132
148
133
149
These numbers reflect rendering pipeline overhead. If your components execute expensive operations internally, that work will still affect performance.
134
150
135
-
####How It Works
151
+
### How It Works
136
152
137
153
When you enable Blaze, components are compiled into direct function calls:
Runtime memoization caches component output during a single request. When a component renders with the same props multiple times, it only executes once.
174
190
@@ -187,11 +203,14 @@ This strategy is ideal for components like icons and avatars that appear many ti
187
203
188
204
If `<x-icon name="check" />` appears 50 times on a page, it renders once and reuses the output.
189
205
190
-
### Compile-Time Folding
206
+
## Compile-Time Folding
207
+
208
+
> [!CAUTION]
209
+
> **Folding requires careful consideration**. Used incorrectly, it can cause subtle bugs that are difficult to diagnose. Make sure you fully understand the limitations described below before using this strategy.
191
210
192
211
Compile-time folding is Blaze's most aggressive optimization. It pre-renders components during compilation, embedding the HTML directly into your template. The component ceases to exist at runtime - there is no function call, no variable resolution, no overhead whatsoever.
193
212
194
-
####Benchmark Results
213
+
### Benchmark Results
195
214
196
215
Because folded components are rendered at compile-time, the runtime cost is effectively zero. The rendering time remains constant regardless of how many components you use:
197
216
@@ -201,72 +220,85 @@ Because folded components are rendered at compile-time, the runtime cost is effe
201
220
| 50,000 | 1,000ms | 0.68ms |
202
221
| 100,000 | 2,000ms | 0.68ms |
203
222
204
-
The time doesn't grow because there are no components to render - just static HTML.
223
+
### How It Works
205
224
206
-
> [!CAUTION]
207
-
> Folding requires careful consideration. Used incorrectly, it can cause subtle bugs that are difficult to diagnose. Review the [Folding](#folding) section before enabling.
225
+
This section covers the intricacies of compile-time folding. If you're using the default function compilation strategy, you can skip this section entirely.
208
226
209
-
#### How It Works
227
+
#### Static props
228
+
229
+
Let's start with a simple component that only recieves a static prop like `color="red"`:
This works flawlessly because all data needed to render the component is available at compile-time.
236
264
237
-
This section covers the intricacies of compile-time folding. If you're using the default function compilation strategy, you can skip this section entirely.
265
+
#### Dynamic props
266
+
267
+
Because Blaze pre-renderes components during compilation, it doesn't have access to data that will be passed to the components at runtime. Blaze works around this by replacing dynamic values with placeholders during rendering and substituting the original expressions back into the final output.
268
+
269
+
To illustrate this, let's assign a random id to the button:
When a component is folded, Blaze renders it at compile-time and embeds the output directly into your template. This eliminates virtually all runtime overhead but introduces constraints on what the component can do.
275
+
During compilation, Blaze will analyze the component to identify dynamic values and store their values:
242
276
243
-
### Dynamic Attributes
277
+
| Property | Dynamic value | Placeholder |
278
+
|------|--------|--------|
279
+
|`id`|`Str::random()`| ATTR_PLACEHOLDER_1 |
244
280
245
-
Blaze handles dynamic attributes that pass through `$attributes` unchanged using placeholder replacement:
281
+
Next, it pre-renders the component using the placeholder:
0 commit comments