PHP
Joomla! 6
Joomla! 5
Joomla! 4
Template
Intermediate
Add Font Preload and Preconnect Hints with $doc->addHeadLink()
Injects <link rel="preload"> and <link rel="preconnect"> tags cleanly into the Joomla document head to boost LCP.
templates/seu_template/index.php
<?php
declare(strict_types=1);
use Joomla\CMS\Uri\Uri;
/** @var \Joomla\CMS\Document\HtmlDocument $this */
$fontUrl = Uri::root(true) . '/media/templates/site/meu_tema/fonts/archivo-700.woff2';
$this->addHeadLink($fontUrl, 'preload', 'rel', [
'as' => 'font',
'type' => 'font/woff2',
'crossorigin' => 'anonymous',
]);
$this->addHeadLink('https://fonts.gstatic.com', 'preconnect', 'rel', [
'crossorigin' => 'anonymous',
]);
Always pass crossorigin="anonymous" on font preloads to avoid double-fetching.
Technical explanation and best practices
Using $doc->addHeadLink() prioritizes critical WOFF2 font files and early TLS handshakes directly through the Joomla document API.