Back to Document class

Method preloadAssets

protected void
preloadAssets
()
Generate the Link header for assets configured for preloading
Returns
  • void
Since
  • 4.0.0
Class: Document
Project: Joomla

Method preloadAssets - Source code

/**
 * Generate the Link header for assets configured for preloading
 *
 * @return  void
 *
 * @since   4.0.0
 */
protected function preloadAssets()
{
    // Process stylesheets first
    foreach ($this->_styleSheets as $link => $properties) {
        if (empty($properties['options']['preload'])) {
            continue;
        }
        foreach ($properties['options']['preload'] as $preloadMethod) {
            // Make sure the preload method is supported, special case for `dns-prefetch` to convert it to the right method name
            if ($preloadMethod === 'dns-prefetch') {
                $this->getPreloadManager()->dnsPrefetch($link);
            } elseif (\in_array($preloadMethod, $this->preloadTypes)) {
                $this->getPreloadManager()->{$preloadMethod}($link);
            } else {
                throw new \InvalidArgumentException(sprintf('The "%s" method is not supported for preloading.', $preloadMethod), 500);
            }
        }
    }
    // Now process scripts
    foreach ($this->_scripts as $link => $properties) {
        if (empty($properties['options']['preload'])) {
            continue;
        }
        foreach ($properties['options']['preload'] as $preloadMethod) {
            // Make sure the preload method is supported, special case for `dns-prefetch` to convert it to the right method name
            if ($preloadMethod === 'dns-prefetch') {
                $this->getPreloadManager()->dnsPrefetch($link);
            } elseif (\in_array($preloadMethod, $this->preloadTypes)) {
                $this->getPreloadManager()->{$preloadMethod}($link);
            } else {
                throw new \InvalidArgumentException(sprintf('The "%s" method is not supported for preloading.', $preloadMethod), 500);
            }
        }
    }
    // Check if the manager's provider has links, if so add the Link header
    if ($links = $this->getPreloadManager()->getLinkProvider()->getLinks()) {
        CmsFactory::getApplication()->setHeader('Link', (new HttpHeaderSerializer())->serialize($links));
    }
}