Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/BlazeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,30 @@ public function compileForUnblaze(string $template): string
return $output;
}

/**
* Exclude Blaze's internal variables from Volt fragment component arguments.
*
* Volt compiles @volt fragments (used in Laravel Folio pages) into a Livewire
* mount call that captures the surrounding template scope via get_defined_vars().
* Blaze introduces variables into that scope ($__blaze and call-site temporaries)
* that would otherwise be forwarded to Livewire, serialized into the component
* snapshot, and corrupt its checksum on subsequent requests.
*/
public function excludeBlazeVariablesFromVoltFragments(string $template): string
{
$needle = 'ExtractFragments::componentArguments([...get_defined_vars()';

if (! str_contains($template, $needle)) {
return $template;
}

return str_replace(
$needle,
'ExtractFragments::componentArguments([...\Livewire\Blaze\Support\Utils::exceptBlazeVariables(get_defined_vars())',
$template,
);
}

/**
* Compile a template for debug-only mode (Blaze disabled).
*
Expand Down
2 changes: 2 additions & 0 deletions src/BlazeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ protected function interceptBladeCompilation(): void
return $input;
}

$input = $blaze->excludeBlazeVariablesFromVoltFragments($input);

if ($blaze->isDisabled()) {
if ($blaze->isDebugging()) {
return $blaze->compileForDebug($input, $path);
Expand Down
12 changes: 12 additions & 0 deletions src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ public static function hash(string $componentPath): string
{
return hash('xxh128', 'v2' . $componentPath);
}

/**
* Remove Blaze's internal template-scope variables (the shared runtime and
* compiled call-site temporaries) from a get_defined_vars() capture.
*/
public static function exceptBlazeVariables(array $variables): array
{
return array_filter($variables, function ($key) {
return ! str_starts_with($key, '__blaze')
&& ! preg_match('/^__(?:attrs|slots)(?:Stack)?[0-9a-f]{32}$/', $key);
}, ARRAY_FILTER_USE_KEY);
}
}
18 changes: 18 additions & 0 deletions tests/BlazeManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,21 @@

expect($manager->viewContainsExpiredFrontMatter($view))->toBeFalse();
});

test('blaze variables are excluded from volt fragment component arguments', function () {
// Volt compiles @volt fragments into a Livewire mount call that captures the
// entire template scope via get_defined_vars(). Blaze's runtime and call-site
// temporaries must be filtered out of that capture, otherwise they end up
// serialized into the Livewire snapshot and corrupt its checksum...
$input = '@livewire("volt-anonymous-fragment-abc123", Livewire\Volt\Precompilers\ExtractFragments::componentArguments([...get_defined_vars(), ...array()]))';

$compiled = app('blade.compiler')->compileString($input);

expect($compiled)->toContain('ExtractFragments::componentArguments([...\Livewire\Blaze\Support\Utils::exceptBlazeVariables(get_defined_vars()), ...array()])');
});

test('get_defined_vars is left untouched outside volt fragment component arguments', function () {
$input = '<?php $vars = get_defined_vars(); ?>';

expect(Blaze::excludeBlazeVariablesFromVoltFragments($input))->toBe($input);
});
23 changes: 23 additions & 0 deletions tests/Support/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Livewire\Blaze\Support\Utils;

test('exceptBlazeVariables removes blaze internals and keeps user variables', function () {
$hash = Utils::hash('components/layout.blade.php');

$variables = [
'message' => 'Hello',
'count' => 1,
'__blaze' => new stdClass,
'__blazeViewName' => 'layout',
'__attrs'.$hash => ['title' => 'Test'],
'__slots'.$hash => [],
'__attrsStack'.$hash => [],
'__slotsStack'.$hash => [],
];

expect(Utils::exceptBlazeVariables($variables))->toBe([
'message' => 'Hello',
'count' => 1,
]);
});
Loading