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
9 changes: 9 additions & 0 deletions src/StaticCaching/Cachers/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public function write($path, $content, $lockFor = 0)
return false;
}

// Truncate the file before writing. Since the "c" mode doesn't truncate and leaves
// the pointer at the start, writing content shorter than what's already on disk
// would otherwise leave stale bytes behind, producing an invalid document.
if (! ftruncate($handle, 0)) {
fclose($handle);

return false;
}

fwrite($handle, $content);
chmod($path, $this->permissions['file']);

Expand Down
49 changes: 49 additions & 0 deletions tests/StaticCaching/WriterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Tests\StaticCaching;

use PHPUnit\Framework\Attributes\Test;
use Statamic\StaticCaching\Cachers\Writer;
use Tests\TestCase;

class WriterTest extends TestCase
{
private string $path;

public function setUp(): void
{
parent::setUp();

$this->path = storage_path('framework/testing/static-cache-writer/page.html');

@unlink($this->path);
}

public function tearDown(): void
{
@unlink($this->path);
@rmdir(dirname($this->path));

parent::tearDown();
}

#[Test]
public function it_writes_the_content_to_disk()
{
$written = (new Writer)->write($this->path, '<html>hello</html>');

$this->assertTrue($written);
$this->assertSame('<html>hello</html>', file_get_contents($this->path));
}

#[Test]
public function it_truncates_stale_bytes_when_overwriting_with_shorter_content()
{
$writer = new Writer;

$writer->write($this->path, '<html>this is a much longer page</html>');
$writer->write($this->path, '<html>short</html>');

$this->assertSame('<html>short</html>', file_get_contents($this->path));
}
}
Loading