Created
April 29, 2025 07:42
-
-
Save mattheu/508a4b544808d3438290d0f918b713c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Speculative Loading Eager Prerender Demo Plugin | |
* Description: Overrides WordPress speculative loading rules to use "eager" and "prerender" for better perceived performance. | |
* Version: 1.0 | |
* Author: Matthew Haines-Young | |
*/ | |
// Hook into the filter WordPress uses to generate the speculationrules script. | |
add_filter('wp_speculation_rules', function($rules) { | |
// Ensure the structure exists | |
foreach (['prefetch', 'prerender'] as $type) { | |
if (!empty($rules[$type]) && is_array($rules[$type])) { | |
foreach ($rules[$type] as &$rule) { | |
$rule['eagerness'] = 'eager'; | |
$rule['action'] = 'prerender'; // In case the browser supports 'action' | |
} | |
// If they were in 'prefetch', move to 'prerender' | |
if ($type === 'prefetch') { | |
$rules['prerender'] = array_merge($rules['prerender'] ?? [], $rules['prefetch']); | |
unset($rules['prefetch']); | |
} | |
} | |
} | |
return $rules; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment