Skip to content

Commit

Permalink
This code is now redundant
Browse files Browse the repository at this point in the history
The position option was only introduced in statamic#10898 to allow either Livewire or nocache to work. We can revert all this code as we’ve now got a separate CSRF route that will solve the Livewire issue.
  • Loading branch information
aerni committed Oct 25, 2024
1 parent 69a1496 commit f80e304
Showing 1 changed file with 2 additions and 31 deletions.
33 changes: 2 additions & 31 deletions src/StaticCaching/Replacers/NoCacheReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Statamic\StaticCaching\Replacers;

use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Statamic\Facades\StaticCache;
use Statamic\StaticCaching\Cacher;
use Statamic\StaticCaching\Cachers\FileCacher;
Expand Down Expand Up @@ -79,40 +78,12 @@ private function modifyFullMeasureResponse(Response $response)
$contents = $response->getContent();

if ($cacher->shouldOutputJs()) {
$contents = match ($pos = $this->insertPosition()) {
'head' => $this->insertJsInHead($contents, $cacher),
'body' => $this->insertJsInBody($contents, $cacher),
default => throw new \Exception('Invalid nocache js insert position ['.$pos.']'),
};
$js = $cacher->getNocacheJs();
$contents = str_replace('</body>', '<script type="text/javascript">'.$js.'</script></body>', $contents);
}

$contents = str_replace('NOCACHE_PLACEHOLDER', $cacher->getNocachePlaceholder(), $contents);

$response->setContent($contents);
}

private function insertPosition()
{
return config('statamic.static_caching.nocache_js_position', 'body');
}

private function insertJsInHead($contents, $cacher)
{
$insertBefore = collect([
Str::position($contents, '<link'),
Str::position($contents, '<script'),
Str::position($contents, '</head>'),
])->filter()->min();

$js = "<script type=\"text/javascript\">{$cacher->getNocacheJs()}</script>";

return Str::substrReplace($contents, $js, $insertBefore, 0);
}

private function insertJsInBody($contents, $cacher)
{
$js = $cacher->getNocacheJs();

return str_replace('</body>', '<script type="text/javascript">'.$js.'</script></body>', $contents);
}
}

0 comments on commit f80e304

Please sign in to comment.