Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/BladeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function ($input) {
$this->manager->startFolding();

try {
if (! file_exists($compiled) || filemtime($path) > filemtime($compiled)) {
if (! file_exists($compiled) || filemtime($path) >= filemtime($compiled)) {
$this->blade->compile($path);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/BlazeRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
*/
public function compile(string $path, string $compiledPath): void
{
if (! file_exists($compiledPath) || filemtime($path) > filemtime($compiledPath)) {
if (! file_exists($compiledPath) || filemtime($path) >= filemtime($compiledPath)) {
$this->compiler->compile($path);
}
}
Expand Down
45 changes: 45 additions & 0 deletions tests/BladeRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,48 @@
File::deleteDirectory($dir);
}
});

test('recompiles stale cache when source and compiled timestamps are equal', function () {
$dir = sys_get_temp_dir().'/blaze-'.uniqid();
$path = $dir.'/button.blade.php';

try {
File::ensureDirectoryExists($dir);
File::put($path, '@blaze(fold: true) stale');

$blaze = app(BlazeManager::class);
$renderer = app(BladeRenderer::class);
$blade = app(BladeCompiler::class);
$bladeCachePath = invade($blade)->cachePath;

// We can't use the renderer here because it would `require` the file
// and register a global function, which would produce stale output
// even after recompiling. So instead we'll simulate folding and
// we will only compile the file in the blaze cache directory
invade($blade)->cachePath = $renderer->getTemporaryCachePath();

$blaze->startFolding();
$blade->compile($path);
$blaze->stopFolding();

invade($blade)->cachePath = $bladeCachePath;

// Now change the source and give it the same timestamp as the compiled file
$compiled = $renderer->getTemporaryCachePath().'/'.Utils::hash($path).'.php';
$timestamp = now()->addSecond()->timestamp;

File::put($path, '@blaze(fold: true) fresh');

touch($path, $timestamp);
touch($compiled, $timestamp);
clearstatcache(true);

expect(File::lastModified($path))->toBe(File::lastModified($compiled));

$node = app(Parser::class)->parse('<x-button />')[0];

expect($renderer->render($node, $path))->toContain('fresh');
} finally {
File::deleteDirectory($dir);
}
});
30 changes: 30 additions & 0 deletions tests/Runtime/BlazeRuntimeTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
<?php

use Illuminate\Support\Facades\File;
use Illuminate\View\Compilers\BladeCompiler;
use Livewire\Blaze\Runtime\BlazeRuntime;

test('compiles stale cache when source and compiled timestamps are equal', function () {
$dir = sys_get_temp_dir().'/blaze-'.uniqid();
$path = $dir.'/button.blade.php';

try {
File::ensureDirectoryExists($dir);
File::put($path, 'fresh');

$compiled = app(BladeCompiler::class)->getCompiledPath($path);
$timestamp = now()->addSecond()->timestamp;

File::put($compiled, 'stale');

touch($path, $timestamp);
touch($compiled, $timestamp);
clearstatcache(true);

expect(File::lastModified($path))->toBe(File::lastModified($compiled));

app(BlazeRuntime::class)->compile($path, $compiled);

expect(File::get($compiled))->toContain('fresh');
} finally {
File::delete($compiled ?? '');
File::deleteDirectory($dir);
}
});

it('processPassthroughContent', function ($input, $results) {
$input = str_replace('[UNBLAZE]', '[STARTCOMPILEDUNBLAZE:XXX][ENDCOMPILEDUNBLAZE:XXX]', $input);

Expand Down
Loading