From 91509d31ce94cd76a70670b04f0d63c89361ac70 Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 4 May 2024 11:50:02 -0500 Subject: [PATCH] Remove randomness from region keys --- src/StaticCaching/NoCache/Session.php | 9 +++++++++ src/StaticCaching/NoCache/StringRegion.php | 4 +--- src/StaticCaching/NoCache/ViewRegion.php | 4 +--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/StaticCaching/NoCache/Session.php b/src/StaticCaching/NoCache/Session.php index bf42e198af4..f6cb04b6d89 100644 --- a/src/StaticCaching/NoCache/Session.php +++ b/src/StaticCaching/NoCache/Session.php @@ -20,6 +20,8 @@ class Session protected $url; + private $regionCount = 0; + public function __construct($url) { $this->url = $url; @@ -55,6 +57,13 @@ public function region(string $key): Region throw new RegionNotFound($key); } + public function getRegionId(): string + { + $this->regionCount += 1; + + return md5($this->url.$this->regionCount); + } + public function pushRegion($contents, $context, $extension): StringRegion { $region = new StringRegion($this, trim($contents), $context, $extension); diff --git a/src/StaticCaching/NoCache/StringRegion.php b/src/StaticCaching/NoCache/StringRegion.php index a37233cb934..3f63b6950f2 100644 --- a/src/StaticCaching/NoCache/StringRegion.php +++ b/src/StaticCaching/NoCache/StringRegion.php @@ -2,8 +2,6 @@ namespace Statamic\StaticCaching\NoCache; -use Statamic\Support\Str; - class StringRegion extends Region { protected $content; @@ -15,7 +13,7 @@ public function __construct(Session $session, string $content, array $context, s $this->content = $content; $this->context = $this->filterContext($context); $this->extension = $extension; - $this->key = sha1($content.Str::random()); + $this->key = sha1($content).$session->getRegionId(); } public function key(): string diff --git a/src/StaticCaching/NoCache/ViewRegion.php b/src/StaticCaching/NoCache/ViewRegion.php index 4d013d5b85d..536df2a63de 100644 --- a/src/StaticCaching/NoCache/ViewRegion.php +++ b/src/StaticCaching/NoCache/ViewRegion.php @@ -2,8 +2,6 @@ namespace Statamic\StaticCaching\NoCache; -use Statamic\Support\Str; - class ViewRegion extends Region { protected $view; @@ -13,7 +11,7 @@ public function __construct(Session $session, string $view, array $context) $this->session = $session; $this->view = $view; $this->context = $this->filterContext($context); - $this->key = Str::random(32); + $this->key = sha1($view).$session->getRegionId(); } public function key(): string