Skip to content

Instantly share code, notes, and snippets.

@mattheu
Created April 29, 2025 07:42
Show Gist options
  • Save mattheu/508a4b544808d3438290d0f918b713c6 to your computer and use it in GitHub Desktop.
Save mattheu/508a4b544808d3438290d0f918b713c6 to your computer and use it in GitHub Desktop.
<?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