Skip to content

Commit

Permalink
[5.x] Cache external url/uri results (#9646)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
JohnathonKoster and jasonvarga authored Mar 13, 2024
1 parent 7bb2bfd commit cb7eaaa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Facades/Endpoint/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
class URL
{
private static $externalUriCache = [];

/**
* Removes occurrences of "//" in a $path (except when part of a protocol)
* Alias of Path::tidy().
Expand Down Expand Up @@ -221,14 +223,31 @@ public function format($url)
*/
public function isExternal($url)
{
if (! $url || Str::startsWith($url, ['/', '#'])) {
if (isset(self::$externalUriCache[$url])) {
return self::$externalUriCache[$url];
}

if (! $url) {
return false;
}

return ! Pattern::startsWith(
if (Str::startsWith($url, ['/', '#'])) {
return self::$externalUriCache[$url] = false;
}

$isExternal = ! Pattern::startsWith(
Str::ensureRight($url, '/'),
Site::current()->absoluteUrl()
);

self::$externalUriCache[$url] = $isExternal;

return $isExternal;
}

public function clearExternalUrlCache()
{
self::$externalUriCache = [];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Listeners/ClearState.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Listeners;

use Statamic\Facades\URL;
use Statamic\Statamic;
use Statamic\View\State\StateManager;

Expand All @@ -11,5 +12,6 @@ public function handle()
{
Statamic::clearApiRouteCache();
StateManager::resetState();
URL::clearExternalUrlCache();
}
}

0 comments on commit cb7eaaa

Please sign in to comment.