Speculative Loading enables near-instant page navigation on Drupal sites by implementing the browser Speculation Rules API for intelligent prefetching and prerendering. Maintained by DXPR.
Enables browsers to speculatively prerender or prefetch pages to achieve near-instant loads based on user interaction.
This module adds support for the Speculation Rules API, which allows defining rules by which certain URLs are dynamically prefetched or prerendered.
By default, the module is enabled and configured to prerender Drupal frontend URLs when the user interacts with a relevant link. Rules are added to frontend pages only, never to administration pages. You can change this in the Speculative Loading tab of the Performance settings.
The module always excludes administrative paths, user login and account
paths, URLs with query strings, and file, module and theme assets. A hook
can add or remove excluded site paths. Alternatively, you can add the
no-prerender CSS class to any link that should not be prerendered.
Pages are tagged with the module configuration, so changing settings invalidates page caches automatically.
The Speculation Rules API is a new web API, and the functionality used by the module is supported in Chromium-based browsers such as Chrome, Edge, or Opera using version 121 or above. Other browsers such as Safari and Firefox will ignore the functionality with no ill effects; they will simply not benefit from the speculative loading. Note that certain browser extensions may disable preloading by default.
Wider browser support is underway: Firefox is actively working on implementing the Speculation Rules API. You can track their progress here: Firefox Bug #1969396
- Browser support for the Speculation Rules API in general
- Information on document rules syntax support used by the module
- Install the module via Composer:
composer require drupal/speculative_loading - Enable the module through the Drupal UI or Drush:
drush en speculative_loading
- Go to Configuration > Development > Performance and open the Speculative Loading tab.
- Keep Enable speculative loading checked. Uncheck it to stop adding rules without uninstalling the module.
- Choose between Prefetch or Prerender mode (prerendering is faster but may cause issues with some interactive content).
- Select the eagerness level (Conservative, Moderate, or Eager).
- Save your configuration.
To add or remove excluded site paths, implement
hook_speculation_rules_href_exclude_paths_alter() in your module. Patterns
are site-relative paths such as /cart/*; the module prefixes them for the
site automatically. URLs with query strings and file, module and theme
assets stay excluded and cannot be removed through this hook.
/**
* Implements hook_speculation_rules_href_exclude_paths_alter().
*/
function mymodule_speculation_rules_href_exclude_paths_alter(
array &$exclude_paths,
string $mode,
) {
// Exclude cart pages from prerendering.
if ($mode === 'prerender') {
$exclude_paths[] = '/cart/*';
}
// Exclude certain paths from both prefetching and prerendering.
$exclude_paths[] = '/checkout/*';
}The module collects exclusions and extra rules from every
SpeculationRules plugin. For each plugin, the manager merges
getExcludePaths() into the denylist for the current mode and appends
getRules() to the mode rule list. In prerender mode, the default plugin
additionally excludes content editing paths (/node/add/*,
/node/*/edit, /node/*/delete) and user edit pages (/user/*/edit).
In both modes it excludes password reset paths (/user/reset/*) and
confirmation paths (/confirm/*).
/**
* @SpeculationRules(
* id = "my_custom_rules",
* title = @Translation("My Custom Rules"),
* description = @Translation("Custom speculation rules implementation.")
* )
*/
class MyCustomRules extends PluginBase implements SpeculationRulesInterface {
// Implement the interface methods...
}The module includes several code quality tools to ensure adherence to Drupal coding standards and best practices. These tools run locally within the module directory.
To set up the development tools:
- Navigate to the module directory:
cd web/modules/contrib/speculative_loading - Install the development dependencies:
composer install
-
PHP CodeSniffer - Checks coding standards using
phpcs.xml.dist- Run:
composer phpcs - Fix automatically:
composer phpcbf
- Run:
-
PHPStan - Static analysis tool
- Run:
composer phpstan
- Run:
For continuous integration, you can add these commands to your CI workflow:
- name: Install dependencies
run: cd web/modules/contrib/speculative_loading && composer install
- name: Run coding standards check
run: cd web/modules/contrib/speculative_loading && composer phpcs
- name: Run static analysis
run: cd web/modules/contrib/speculative_loading && composer phpstanThis module is inspired by the WordPress Speculative Loading plugin.
Modules that complement Speculative Loading for Drupal performance:
- BigPipe - Drupal core module for progressive page rendering; works alongside Speculative Loading to reduce both initial and subsequent page load times
- Internal Page Cache - Drupal core caching for anonymous users; caches the pages that Speculative Loading prerenders
- DXPR Theme - Responsive Drupal theme with built-in performance optimizations
- DXPR Builder - Drag-and-drop page builder for Drupal