I’ll build this as a drop-in WordPress theme file for rx-theme/performance/preconnect.php, focused on DNS prefetch, preconnect, preload, prefetch, modulepreload, resource hints, safe sanitization, filters, and admin-friendly extensibility.
Below is an advanced performance/preconnect.php file for your RX Theme. It includes DNS prefetch, preconnect, preload, prefetch, prerender, modulepreload, font preload, image preload, script/style preload, WooCommerce support, Google Fonts optimization, REST/API hints, and many WordPress filters so you can extend it later.
Create this file:
rx-theme/
└── performance/
└── preconnect.php
Then include it from functions.php:
require_once get_template_directory() . '/performance/preconnect.php';
performance/preconnect.php
<?php
/**
* RX Theme Performance Resource Hints
*
* File: /performance/preconnect.php
*
* Features:
* - DNS prefetch
* - Preconnect
* - Preload
* - Prefetch
* - Prerender
* - Modulepreload
* - Font preload
* - Image preload
* - Script preload
* - Style preload
* - Google Fonts optimization
* - CDN origin hints
* - REST API hints
* - WooCommerce conditional hints
* - Elementor / Google / Cloudflare / YouTube / Analytics hints
* - Safe sanitization
* - Extensible WordPress filters
*
* @package RX_Theme
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'RX_Theme_Preconnect_Performance' ) ) :
final class RX_Theme_Preconnect_Performance {
/**
* Version.
*/
const VERSION = '1.0.0';
/**
* Singleton instance.
*
* @var RX_Theme_Preconnect_Performance|null
*/
private static $instance = null;
/**
* Already printed flag.
*
* @var bool
*/
private $printed = false;
/**
* Get instance.
*
* @return RX_Theme_Preconnect_Performance
*/
public static function instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
private function __construct() {
add_filter( 'wp_resource_hints', array( $this, 'wp_resource_hints' ), 20, 2 );
add_action( 'wp_head', array( $this, 'print_head_resource_hints' ), 1 );
add_action( 'wp_head', array( $this, 'print_preload_resources' ), 2 );
add_action( 'wp_head', array( $this, 'print_prefetch_resources' ), 3 );
add_action( 'wp_head', array( $this, 'print_prerender_resources' ), 4 );
add_action( 'wp_head', array( $this, 'print_modulepreload_resources' ), 5 );
add_action( 'wp_head', array( $this, 'print_early_inline_performance_script' ), 6 );
add_filter( 'style_loader_tag', array( $this, 'maybe_optimize_google_fonts_stylesheet' ), 10, 4 );
add_filter( 'script_loader_tag', array( $this, 'maybe_add_script_fetchpriority' ), 10, 3 );
}
/**
* Main config.
*
* You can override this with:
*
* add_filter( 'rx_theme_preconnect_config', function( $config ) {
* $config['preconnect'][] = 'https://example.com';
* return $config;
* });
*
* @return array
*/
public function get_config() {
$theme_uri = get_template_directory_uri();
$home_url = home_url();
$config = array(
/**
* DNS Prefetch:
* Best for many third-party origins.
*/
'dns_prefetch' => array(
'//fonts.googleapis.com',
'//fonts.gstatic.com',
'//ajax.googleapis.com',
'//cdnjs.cloudflare.com',
'//cdn.jsdelivr.net',
'//unpkg.com',
'//www.googletagmanager.com',
'//www.google-analytics.com',
'//ssl.google-analytics.com',
'//www.google.com',
'//www.gstatic.com',
'//connect.facebook.net',
'//platform.twitter.com',
'//syndication.twitter.com',
'//pbs.twimg.com',
'//i.ytimg.com',
'//www.youtube.com',
'//s.ytimg.com',
'//vimeo.com',
'//player.vimeo.com',
'//secure.gravatar.com',
'//pixel.wp.com',
'//stats.wp.com',
'//s.w.org',
'//api.wordpress.org',
),
/**
* Preconnect:
* Use for critical third-party origins.
*/
'preconnect' => array(
array(
'href' => 'https://fonts.googleapis.com',
'crossorigin' => false,
),
array(
'href' => 'https://fonts.gstatic.com',
'crossorigin' => true,
),
array(
'href' => 'https://cdn.jsdelivr.net',
'crossorigin' => true,
),
array(
'href' => 'https://cdnjs.cloudflare.com',
'crossorigin' => true,
),
array(
'href' => 'https://www.googletagmanager.com',
'crossorigin' => false,
),
array(
'href' => 'https://www.google-analytics.com',
'crossorigin' => false,
),
array(
'href' => 'https://secure.gravatar.com',
'crossorigin' => true,
),
),
/**
* Preload:
* Critical resources needed very early.
*/
'preload' => array(
/**
* Example local CSS preload.
* Uncomment and change path if needed.
*/
/*
array(
'href' => $theme_uri . '/assets/css/main.css',
'as' => 'style',
'type' => 'text/css',
'crossorigin' => false,
'media' => 'all',
),
*/
/**
* Example local JS preload.
*/
/*
array(
'href' => $theme_uri . '/assets/js/main.js',
'as' => 'script',
'type' => 'text/javascript',
'crossorigin' => false,
),
*/
/**
* Example font preload.
*/
/*
array(
'href' => $theme_uri . '/assets/fonts/rx-font.woff2',
'as' => 'font',
'type' => 'font/woff2',
'crossorigin' => true,
),
*/
/**
* Example hero image preload.
*/
/*
array(
'href' => $theme_uri . '/assets/images/hero.webp',
'as' => 'image',
'type' => 'image/webp',
'fetchpriority' => 'high',
),
*/
),
/**
* Modulepreload:
* Useful for modern ES module apps.
*/
'modulepreload' => array(
/*
array(
'href' => $theme_uri . '/assets/app/app.module.js',
'crossorigin' => false,
),
*/
),
/**
* Prefetch:
* Future navigation/resources.
*/
'prefetch' => array(
/*
array(
'href' => $home_url . '/about/',
'as' => 'document',
),
*/
),
/**
* Prerender:
* Use very carefully. Only for highly likely next page.
*/
'prerender' => array(
/*
$home_url . '/important-page/',
*/
),
/**
* Critical images.
*/
'critical_images' => array(
/*
$theme_uri . '/assets/images/logo.webp',
*/
),
/**
* Critical fonts.
*/
'critical_fonts' => array(
/*
array(
'href' => $theme_uri . '/assets/fonts/inter-var.woff2',
'type' => 'font/woff2',
'crossorigin' => true,
),
*/
),
/**
* WordPress REST/API hints.
*/
'rest_api_hints' => true,
/**
* WooCommerce hints.
*/
'woocommerce_hints' => true,
/**
* Elementor hints.
*/
'elementor_hints' => true,
/**
* Google fonts optimization.
*/
'optimize_google_fonts' => true,
/**
* Add fetchpriority to selected scripts.
*/
'script_fetchpriority' => array(
/*
'theme-main' => 'high',
*/
),
/**
* Prevent duplicate output.
*/
'avoid_duplicates' => true,
/**
* Disable on admin/login pages.
*/
'frontend_only' => true,
);
/**
* Filter full RX preconnect config.
*/
return apply_filters( 'rx_theme_preconnect_config', $config );
}
/**
* Check whether hints should load.
*
* @return bool
*/
private function should_load() {
$config = $this->get_config();
if ( ! empty( $config['frontend_only'] ) ) {
if ( is_admin() || wp_doing_ajax() || wp_doing_cron() ) {
return false;
}
global $pagenow;
if ( isset( $pagenow ) && in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ), true ) ) {
return false;
}
}
return (bool) apply_filters( 'rx_theme_preconnect_should_load', true );
}
/**
* Add WordPress native resource hints.
*
* @param array $urls URLs.
* @param string $relation_type Relation type.
*
* @return array
*/
public function wp_resource_hints( $urls, $relation_type ) {
if ( ! $this->should_load() ) {
return $urls;
}
$config = $this->get_config();
if ( 'dns-prefetch' === $relation_type && ! empty( $config['dns_prefetch'] ) ) {
foreach ( $config['dns_prefetch'] as $url ) {
$clean = $this->sanitize_resource_url( $url );
if ( $clean ) {
$urls[] = $clean;
}
}
}
if ( 'preconnect' === $relation_type && ! empty( $config['preconnect'] ) ) {
foreach ( $config['preconnect'] as $item ) {
if ( is_string( $item ) ) {
$href = $this->sanitize_resource_url( $item );
if ( $href ) {
$urls[] = $href;
}
}
if ( is_array( $item ) && ! empty( $item['href'] ) ) {
$href = $this->sanitize_resource_url( $item['href'] );
if ( $href ) {
$entry = array(
'href' => $href,
);
if ( ! empty( $item['crossorigin'] ) ) {
$entry['crossorigin'] = 'anonymous';
}
$urls[] = $entry;
}
}
}
}
$urls = $this->unique_resource_hints( $urls );
return apply_filters( 'rx_theme_wp_resource_hints', $urls, $relation_type );
}
/**
* Print extra head resource hints manually.
*
* @return void
*/
public function print_head_resource_hints() {
if ( ! $this->should_load() || $this->printed ) {
return;
}
$config = $this->get_config();
echo "\n<!-- RX Theme Performance: Resource Hints -->\n";
$this->print_dns_prefetch( $config );
$this->print_preconnect( $config );
$this->print_rest_api_hints( $config );
$this->print_woocommerce_hints( $config );
$this->print_elementor_hints( $config );
echo "<!-- /RX Theme Performance: Resource Hints -->\n";
$this->printed = true;
}
/**
* Print DNS prefetch.
*
* @param array $config Config.
*
* @return void
*/
private function print_dns_prefetch( $config ) {
if ( empty( $config['dns_prefetch'] ) || ! is_array( $config['dns_prefetch'] ) ) {
return;
}
$items = apply_filters( 'rx_theme_dns_prefetch_urls', $config['dns_prefetch'] );
$done = array();
foreach ( $items as $url ) {
$href = $this->sanitize_resource_url( $url );
if ( ! $href || isset( $done[ $href ] ) ) {
continue;
}
$done[ $href ] = true;
printf(
'<link rel="dns-prefetch" href="%s">' . "\n",
esc_url( $href )
);
}
}
/**
* Print preconnect.
*
* @param array $config Config.
*
* @return void
*/
private function print_preconnect( $config ) {
if ( empty( $config['preconnect'] ) || ! is_array( $config['preconnect'] ) ) {
return;
}
$items = apply_filters( 'rx_theme_preconnect_urls', $config['preconnect'] );
$done = array();
foreach ( $items as $item ) {
$href = '';
$crossorigin = false;
if ( is_string( $item ) ) {
$href = $item;
}
if ( is_array( $item ) ) {
$href = isset( $item['href'] ) ? $item['href'] : '';
$crossorigin = ! empty( $item['crossorigin'] );
}
$href = $this->sanitize_resource_url( $href );
if ( ! $href || isset( $done[ $href ] ) ) {
continue;
}
$done[ $href ] = true;
printf(
'<link rel="preconnect" href="%s"%s>' . "\n",
esc_url( $href ),
$crossorigin ? ' crossorigin' : ''
);
}
}
/**
* Print REST API hints.
*
* @param array $config Config.
*
* @return void
*/
private function print_rest_api_hints( $config ) {
if ( empty( $config['rest_api_hints'] ) ) {
return;
}
$rest_url = esc_url( rest_url() );
if ( $rest_url ) {
printf(
'<link rel="preconnect" href="%s">' . "\n",
esc_url( home_url() )
);
}
}
/**
* Print WooCommerce hints.
*
* @param array $config Config.
*
* @return void
*/
private function print_woocommerce_hints( $config ) {
if ( empty( $config['woocommerce_hints'] ) ) {
return;
}
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
if ( function_exists( 'is_cart' ) && is_cart() ) {
echo '<link rel="prefetch" href="' . esc_url( wc_get_checkout_url() ) . '" as="document">' . "\n";
}
if ( function_exists( 'is_product' ) && is_product() ) {
$cart_url = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : '';
if ( $cart_url ) {
echo '<link rel="prefetch" href="' . esc_url( $cart_url ) . '" as="document">' . "\n";
}
}
}
/**
* Print Elementor hints.
*
* @param array $config Config.
*
* @return void
*/
private function print_elementor_hints( $config ) {
if ( empty( $config['elementor_hints'] ) ) {
return;
}
if ( ! did_action( 'elementor/loaded' ) ) {
return;
}
echo '<link rel="dns-prefetch" href="//assets.elementor.com">' . "\n";
echo '<link rel="preconnect" href="https://assets.elementor.com" crossorigin>' . "\n";
}
/**
* Print preload resources.
*
* @return void
*/
public function print_preload_resources() {
if ( ! $this->should_load() ) {
return;
}
$config = $this->get_config();
echo "\n<!-- RX Theme Performance: Preload -->\n";
$this->print_general_preloads( $config );
$this->print_critical_font_preloads( $config );
$this->print_critical_image_preloads( $config );
$this->print_dynamic_featured_image_preload();
echo "<!-- /RX Theme Performance: Preload -->\n";
}
/**
* Print general preloads.
*
* @param array $config Config.
*
* @return void
*/
private function print_general_preloads( $config ) {
if ( empty( $config['preload'] ) || ! is_array( $config['preload'] ) ) {
return;
}
$items = apply_filters( 'rx_theme_preload_resources', $config['preload'] );
$done = array();
foreach ( $items as $item ) {
if ( ! is_array( $item ) || empty( $item['href'] ) || empty( $item['as'] ) ) {
continue;
}
$href = $this->sanitize_resource_url( $item['href'] );
if ( ! $href || isset( $done[ $href ] ) ) {
continue;
}
$done[ $href ] = true;
$attrs = array(
'rel' => 'preload',
'href' => $href,
'as' => sanitize_key( $item['as'] ),
);
if ( ! empty( $item['type'] ) ) {
$attrs['type'] = sanitize_text_field( $item['type'] );
}
if ( ! empty( $item['crossorigin'] ) ) {
$attrs['crossorigin'] = 'anonymous';
}
if ( ! empty( $item['media'] ) ) {
$attrs['media'] = sanitize_text_field( $item['media'] );
}
if ( ! empty( $item['imagesrcset'] ) ) {
$attrs['imagesrcset'] = esc_attr( $item['imagesrcset'] );
}
if ( ! empty( $item['imagesizes'] ) ) {
$attrs['imagesizes'] = esc_attr( $item['imagesizes'] );
}
if ( ! empty( $item['fetchpriority'] ) ) {
$attrs['fetchpriority'] = $this->sanitize_fetchpriority( $item['fetchpriority'] );
}
echo $this->build_link_tag( $attrs ) . "\n";
}
}
/**
* Print critical fonts.
*
* @param array $config Config.
*
* @return void
*/
private function print_critical_font_preloads( $config ) {
if ( empty( $config['critical_fonts'] ) || ! is_array( $config['critical_fonts'] ) ) {
return;
}
$fonts = apply_filters( 'rx_theme_critical_fonts', $config['critical_fonts'] );
$done = array();
foreach ( $fonts as $font ) {
if ( is_string( $font ) ) {
$font = array(
'href' => $font,
'type' => 'font/woff2',
'crossorigin' => true,
);
}
if ( ! is_array( $font ) || empty( $font['href'] ) ) {
continue;
}
$href = $this->sanitize_resource_url( $font['href'] );
if ( ! $href || isset( $done[ $href ] ) ) {
continue;
}
$done[ $href ] = true;
$attrs = array(
'rel' => 'preload',
'href' => $href,
'as' => 'font',
'type' => ! empty( $font['type'] ) ? sanitize_text_field( $font['type'] ) : 'font/woff2',
'crossorigin' => 'anonymous',
);
echo $this->build_link_tag( $attrs ) . "\n";
}
}
/**
* Print critical images.
*
* @param array $config Config.
*
* @return void
*/
private function print_critical_image_preloads( $config ) {
if ( empty( $config['critical_images'] ) || ! is_array( $config['critical_images'] ) ) {
return;
}
$images = apply_filters( 'rx_theme_critical_images', $config['critical_images'] );
$done = array();
foreach ( $images as $image ) {
$href = '';
if ( is_string( $image ) ) {
$href = $image;
} elseif ( is_array( $image ) && ! empty( $image['href'] ) ) {
$href = $image['href'];
}
$href = $this->sanitize_resource_url( $href );
if ( ! $href || isset( $done[ $href ] ) ) {
continue;
}
$done[ $href ] = true;
$attrs = array(
'rel' => 'preload',
'href' => $href,
'as' => 'image',
'fetchpriority' => 'high',
);
if ( is_array( $image ) ) {
if ( ! empty( $image['type'] ) ) {
$attrs['type'] = sanitize_text_field( $image['type'] );
}
if ( ! empty( $image['imagesrcset'] ) ) {
$attrs['imagesrcset'] = esc_attr( $image['imagesrcset'] );
}
if ( ! empty( $image['imagesizes'] ) ) {
$attrs['imagesizes'] = esc_attr( $image['imagesizes'] );
}
}
echo $this->build_link_tag( $attrs ) . "\n";
}
}
/**
* Preload featured image on singular pages.
*
* @return void
*/
private function print_dynamic_featured_image_preload() {
if ( ! is_singular() || ! has_post_thumbnail() ) {
return;
}
$enabled = apply_filters( 'rx_theme_preload_featured_image', true );
if ( ! $enabled ) {
return;
}
$post_id = get_the_ID();
if ( ! $post_id ) {
return;
}
$image_id = get_post_thumbnail_id( $post_id );
if ( ! $image_id ) {
return;
}
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
if ( ! $image_url ) {
return;
}
$image_srcset = wp_get_attachment_image_srcset( $image_id, 'full' );
$image_sizes = wp_get_attachment_image_sizes( $image_id, 'full' );
$attrs = array(
'rel' => 'preload',
'href' => esc_url( $image_url ),
'as' => 'image',
'fetchpriority' => 'high',
);
if ( $image_srcset ) {
$attrs['imagesrcset'] = esc_attr( $image_srcset );
}
if ( $image_sizes ) {
$attrs['imagesizes'] = esc_attr( $image_sizes );
}
echo $this->build_link_tag( $attrs ) . "\n";
}
/**
* Print prefetch resources.
*
* @return void
*/
public function print_prefetch_resources() {
if ( ! $this->should_load() ) {
return;
}
$config = $this->get_config();
if ( empty( $config['prefetch'] ) || ! is_array( $config['prefetch'] ) ) {
return;
}
$items = apply_filters( 'rx_theme_prefetch_resources', $config['prefetch'] );
if ( empty( $items ) ) {
return;
}
echo "\n<!-- RX Theme Performance: Prefetch -->\n";
$done = array();
foreach ( $items as $item ) {
$href = '';
$as = '';
if ( is_string( $item ) ) {
$href = $item;
}
if ( is_array( $item ) && ! empty( $item['href'] ) ) {
$href = $item['href'];
$as = ! empty( $item['as'] ) ? sanitize_key( $item['as'] ) : '';
}
$href = $this->sanitize_resource_url( $href );
if ( ! $href || isset( $done[ $href ] ) ) {
continue;
}
$done[ $href ] = true;
$attrs = array(
'rel' => 'prefetch',
'href' => $href,
);
if ( $as ) {
$attrs['as'] = $as;
}
echo $this->build_link_tag( $attrs ) . "\n";
}
echo "<!-- /RX Theme Performance: Prefetch -->\n";
}
/**
* Print prerender resources.
*
* @return void
*/
public function print_prerender_resources() {
if ( ! $this->should_load() ) {
return;
}
$config = $this->get_config();
if ( empty( $config['prerender'] ) || ! is_array( $config['prerender'] ) ) {
return;
}
$items = apply_filters( 'rx_theme_prerender_resources', $config['prerender'] );
if ( empty( $items ) ) {
return;
}
echo "\n<!-- RX Theme Performance: Prerender -->\n";
$done = array();
foreach ( $items as $url ) {
$href = $this->sanitize_resource_url( $url );
if ( ! $href || isset( $done[ $href ] ) ) {
continue;
}
$done[ $href ] = true;
printf(
'<link rel="prerender" href="%s">' . "\n",
esc_url( $href )
);
}
echo "<!-- /RX Theme Performance: Prerender -->\n";
}
/**
* Print modulepreload resources.
*
* @return void
*/
public function print_modulepreload_resources() {
if ( ! $this->should_load() ) {
return;
}
$config = $this->get_config();
if ( empty( $config['modulepreload'] ) || ! is_array( $config['modulepreload'] ) ) {
return;
}
$items = apply_filters( 'rx_theme_modulepreload_resources', $config['modulepreload'] );
if ( empty( $items ) ) {
return;
}
echo "\n<!-- RX Theme Performance: Modulepreload -->\n";
$done = array();
foreach ( $items as $item ) {
$href = '';
$crossorigin = false;
if ( is_string( $item ) ) {
$href = $item;
}
if ( is_array( $item ) ) {
$href = ! empty( $item['href'] ) ? $item['href'] : '';
$crossorigin = ! empty( $item['crossorigin'] );
}
$href = $this->sanitize_resource_url( $href );
if ( ! $href || isset( $done[ $href ] ) ) {
continue;
}
$done[ $href ] = true;
$attrs = array(
'rel' => 'modulepreload',
'href' => $href,
);
if ( $crossorigin ) {
$attrs['crossorigin'] = 'anonymous';
}
echo $this->build_link_tag( $attrs ) . "\n";
}
echo "<!-- /RX Theme Performance: Modulepreload -->\n";
}
/**
* Print small inline helper script.
*
* Adds hover-intent prefetch for internal links.
*
* @return void
*/
public function print_early_inline_performance_script() {
if ( ! $this->should_load() ) {
return;
}
$enabled = apply_filters( 'rx_theme_hover_prefetch_enabled', true );
if ( ! $enabled ) {
return;
}
?>
<!-- RX Theme Performance: Hover Prefetch -->
<script>
(function () {
'use strict';
if (!('requestIdleCallback' in window)) {
window.requestIdleCallback = function (callback) {
return setTimeout(function () {
callback({
didTimeout: false,
timeRemaining: function () {
return 1;
}
});
}, 1);
};
}
var prefetched = {};
var origin = window.location.origin;
function isGoodUrl(url) {
try {
var parsed = new URL(url, window.location.href);
if (parsed.origin !== origin) {
return false;
}
if (parsed.pathname === window.location.pathname) {
return false;
}
if (parsed.hash && parsed.pathname === window.location.pathname) {
return false;
}
if (parsed.href.indexOf('/wp-admin/') !== -1) {
return false;
}
if (parsed.href.indexOf('/wp-login.php') !== -1) {
return false;
}
if (parsed.href.indexOf('add-to-cart=') !== -1) {
return false;
}
return true;
} catch (e) {
return false;
}
}
function prefetch(url) {
if (!isGoodUrl(url) || prefetched[url]) {
return;
}
prefetched[url] = true;
window.requestIdleCallback(function () {
var link = document.createElement('link');
link.rel = 'prefetch';
link.href = url;
link.as = 'document';
document.head.appendChild(link);
});
}
function handlePointerOver(event) {
var link = event.target.closest && event.target.closest('a[href]');
if (!link) {
return;
}
prefetch(link.href);
}
document.addEventListener('mouseover', handlePointerOver, {
capture: true,
passive: true
});
document.addEventListener('touchstart', handlePointerOver, {
capture: true,
passive: true
});
})();
</script>
<!-- /RX Theme Performance: Hover Prefetch -->
<?php
}
/**
* Optimize Google Fonts style tags.
*
* @param string $html HTML.
* @param string $handle Handle.
* @param string $href Href.
* @param string $media Media.
*
* @return string
*/
public function maybe_optimize_google_fonts_stylesheet( $html, $handle, $href, $media ) {
$config = $this->get_config();
if ( empty( $config['optimize_google_fonts'] ) ) {
return $html;
}
if ( false === strpos( $href, 'fonts.googleapis.com' ) ) {
return $html;
}
$optimized = sprintf(
'<link rel="preload" href="%1$s" as="style" onload="this.onload=null;this.rel=\'stylesheet\'" media="%2$s">' . "\n" .
'<noscript><link rel="stylesheet" href="%1$s" media="%2$s"></noscript>' . "\n",
esc_url( $href ),
esc_attr( $media )
);
return apply_filters( 'rx_theme_google_fonts_optimized_tag', $optimized, $html, $handle, $href, $media );
}
/**
* Add fetchpriority to selected script handles.
*
* @param string $tag Script tag.
* @param string $handle Script handle.
* @param string $src Source.
*
* @return string
*/
public function maybe_add_script_fetchpriority( $tag, $handle, $src ) {
$config = $this->get_config();
if ( empty( $config['script_fetchpriority'] ) || ! is_array( $config['script_fetchpriority'] ) ) {
return $tag;
}
if ( empty( $config['script_fetchpriority'][ $handle ] ) ) {
return $tag;
}
$priority = $this->sanitize_fetchpriority( $config['script_fetchpriority'][ $handle ] );
if ( ! $priority ) {
return $tag;
}
if ( false !== strpos( $tag, ' fetchpriority=' ) ) {
return $tag;
}
$tag = str_replace( '<script ', '<script fetchpriority="' . esc_attr( $priority ) . '" ', $tag );
return $tag;
}
/**
* Build link tag.
*
* @param array $attrs Attributes.
*
* @return string
*/
private function build_link_tag( $attrs ) {
$output = '<link';
foreach ( $attrs as $key => $value ) {
if ( '' === $value || null === $value || false === $value ) {
continue;
}
$key = sanitize_key( $key );
if ( true === $value ) {
$output .= ' ' . esc_attr( $key );
continue;
}
$output .= sprintf(
' %s="%s"',
esc_attr( $key ),
esc_attr( $value )
);
}
$output .= '>';
return $output;
}
/**
* Sanitize resource URL.
*
* @param string $url URL.
*
* @return string
*/
private function sanitize_resource_url( $url ) {
if ( ! is_string( $url ) || '' === trim( $url ) ) {
return '';
}
$url = trim( $url );
if ( 0 === strpos( $url, '//' ) ) {
return esc_url_raw( $url );
}
if ( 0 === strpos( $url, '/' ) ) {
return esc_url_raw( home_url( $url ) );
}
return esc_url_raw( $url );
}
/**
* Sanitize fetchpriority.
*
* @param string $priority Priority.
*
* @return string
*/
private function sanitize_fetchpriority( $priority ) {
$priority = strtolower( sanitize_text_field( $priority ) );
if ( in_array( $priority, array( 'high', 'low', 'auto' ), true ) ) {
return $priority;
}
return '';
}
/**
* Unique resource hints.
*
* @param array $urls URLs.
*
* @return array
*/
private function unique_resource_hints( $urls ) {
$seen = array();
$output = array();
foreach ( $urls as $item ) {
$key = '';
if ( is_string( $item ) ) {
$key = $item;
} elseif ( is_array( $item ) && ! empty( $item['href'] ) ) {
$key = $item['href'];
}
if ( ! $key || isset( $seen[ $key ] ) ) {
continue;
}
$seen[ $key ] = true;
$output[] = $item;
}
return $output;
}
}
endif;
/**
* Boot RX Theme Preconnect Performance.
*/
add_action(
'after_setup_theme',
function () {
RX_Theme_Preconnect_Performance::instance();
},
5
);
Add your own CDN URLs from functions.php
Example for your static CDN:
add_filter( 'rx_theme_preconnect_config', function ( $config ) {
$config['dns_prefetch'][] = '//static-assets-cdnrx.rxharun.com';
$config['preconnect'][] = array(
'href' => 'https://static-assets-cdnrx.rxharun.com',
'crossorigin' => true,
);
$config['modulepreload'][] = array(
'href' => 'https://static-assets-cdnrx.rxharun.com/_next/static/chunks/app.js',
'crossorigin' => true,
);
$config['preload'][] = array(
'href' => 'https://static-assets-cdnrx.rxharun.com/_next/static/chunks/02c3irf6-c0dr.js',
'as' => 'script',
'type' => 'text/javascript',
'crossorigin' => true,
);
return $config;
});
Important advice
Do not preload too many chunk files. Preload only the most critical files needed for the first visible page. Too many preload tags can slow the site because the browser becomes busy downloading files that may not be needed immediately.
Best use:
preconnect = CDN origins, fonts, analytics
dns-prefetch = many third-party domains
preload = only critical CSS, main JS, hero image, important font
prefetch = next page or future chunk
modulepreload = modern module app files
For your RX Theme, this file is safe as a strong base. You can keep adding CDN, app, chunk, CSS, font, and image URLs through the filter instead of editing the core file every time.