- Issue created by @mglaman
- Merge request !54Resolve #3545480 "Code component previews drupalsettings performance" → (Merged) created by wim leers
I noticed this while using the AI capabilities. CodeComponentDataProvider::getXbDataBreadcrumbsV0 took 553ms becaue it was invoked 132 times. The breadcrumb builder can be expensive. The preview rendering of whatever was happening behind the scenes ran this a lot
Use \SplObjectStorage to cache data per-request.
public function __construct(
private readonly ConfigFactoryInterface $configFactory,
private readonly RequestStack $requestStack,
private readonly RouteMatchInterface $routeMatch,
private readonly TitleResolverInterface $titleResolver,
private readonly ChainBreadcrumbBuilderInterface $breadcrumbManager,
private readonly ContainerInterface $container,
) {
$this->data = new \SplObjectStorage();
}
/**
* Returns the BaseUrl for V0 of drupalSettings.xbData.
*
* @return array[]
*/
public function getXbDataBaseUrlV0(): array {
$request = $this->requestStack->getCurrentRequest();
\assert($request instanceof Request);
if (!isset($this->data[$request])) {
$this->data[$request] = [
self::V0 => [
// ⚠️ Not the same as `drupalSettings.path.baseUrl` nor Symfony's
// definition of a base URL.
// JavaScript tools like @drupal-api-client/json-api-client usually need
// a full absolute URL.
// @see \Symfony\Component\HttpFoundation\Request::getBaseUrl()
// @see \Drupal\system\Hook\SystemHooks::jsSettingsAlter()
'baseUrl' => $request->getSchemeAndHttpHost() . $request->getBaseUrl(),
],
];
}
return $this->data[$request];
}
Active
1.0
… to be triaged