Skip to content

Commit

Permalink
[5.x] Fix nocache race condition (#10642)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Aug 15, 2024
1 parent 8c93c43 commit ba1833b
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/StaticCaching/NoCache/StringFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Statamic\Facades\File;
use Statamic\Support\Arr;
use Statamic\Support\Str;

class StringFragment
{
Expand All @@ -27,28 +28,26 @@ public function render(): string
view()->addNamespace('nocache', $this->directory);
File::makeDirectory($this->directory);

$path = $this->createTemporaryView();
$path = $this->createTemporaryView($view = $this->region.Str::random());

$this->data['__frontmatter'] = Arr::pull($this->data, 'view', []);

$rendered = view('nocache::'.$this->region, $this->data)->render();
$rendered = view('nocache::'.$view, $this->data)->render();

File::delete($path);

return $rendered;
}

private function createTemporaryView(): string
private function createTemporaryView($view): string
{
$path = vsprintf('%s/%s.%s', [
$this->directory,
$this->region,
$view,
$this->extension,
]);

if (! File::exists($path)) {
File::put($path, $this->contents);
}
File::put($path, $this->contents);

return $path;
}
Expand Down

0 comments on commit ba1833b

Please sign in to comment.