Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Cache external url/uri results #9646

Merged
merged 5 commits into from
Mar 13, 2024
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
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();
}
}
Loading