Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save westonruter/eb51e56c7e92f3cd6e3088eb87d9ce07 to your computer and use it in GitHub Desktop.
Save westonruter/eb51e56c7e92f3cd6e3088eb87d9ce07 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Speculative Loading: Eagerly Prerender Homepage from Singular Template
* Plugin URI: https://gist.github.com/westonruter/eb51e56c7e92f3cd6e3088eb87d9ce07
* Description: When viewing a singular post or page, eagerly prerender the homepage so that navigating there will be instant.
* Requires at least: 5.7
* Requires PHP: 7.2
* Version: 0.1.0
* Author: Weston Ruter
* Author URI: https://weston.ruter.net/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Update URI: https://gist.github.com/westonruter/eb51e56c7e92f3cd6e3088eb87d9ce07
* Gist Plugin URI: https://gist.github.com/westonruter/eb51e56c7e92f3cd6e3088eb87d9ce07
*
* @package SpeculationRulesPrerenderHomepageFromSingularTemplate
*/
add_action(
'wp',
static function (): void {
// Abort if WordPress is not serving a singular post or page.
if ( ! is_singular() ) {
return;
}
/*
* Create the eager speculation rule for the link to the homepage. This presumes that the link is constructed
* with the rel-home Microformat <https://microformats.org/wiki/rel-home> which is used by all core themes.
*/
$speculation_rule = array(
'source' => 'document',
'where' => array(
'selector_matches' => 'a[rel="home"]',
),
'eagerness' => 'eager',
);
/*
* As an alternative to the above, the speculation rule could instead be constructed as follows:
*
* array(
* 'source' => 'list',
* 'urls' => array( home_url( '/' ) ),
* 'eagerness' => 'eager',
* )
*
* However, there is a danger that a plugin may append query parameters to the links as they are rendered in
* the template. This could result in a post permalink being eagerly prerendered which does not correspond
* to the actual post link which the user navigates to, and thereby resulting in a wasted the prerender.
* An added benefit to using a document source with a selector is that it ensures the URL being prerendered
* actually appears in the page as a link: granted it surely would the vast majority of the time, but in
* WordPress anything is possible.
*/
// Add the new speculation rule to the page.
if ( class_exists( 'WP_Speculation_Rules' ) ) {
// This branch is for WordPress 6.8-beta1. See <https://make.wordpress.org/core/2025/03/06/speculative-loading-in-6-8/>.
add_action(
'wp_load_speculation_rules',
static function ( WP_Speculation_Rules $speculation_rules ) use ( $speculation_rule ): void {
$speculation_rules->add_rule(
'prerender',
'prerender-homepage-from-singular-template',
$speculation_rule
);
}
);
} else {
// This branch is for WordPress versions prior to 6.8-beta1.
// Note that multiple 'speculationrules' scripts can be on a page, so it is fine if the Speculative Loading
// plugin is also adding its script that may do moderate prerendering for all links as well.
add_action(
'wp_footer',
static function () use ( $speculation_rule ): void {
wp_print_inline_script_tag(
(string) wp_json_encode( array( 'prerender' => array( $speculation_rule ) ) ),
array( 'type' => 'speculationrules' )
);
}
);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment