Skip to content

Commit

Permalink
Fix prerender exclusions so that they also exclude links that opt out…
Browse files Browse the repository at this point in the history
… of prefetch.
  • Loading branch information
felixarntz committed Feb 3, 2025
1 parent 8c12bd3 commit 6827503
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
59 changes: 35 additions & 24 deletions src/wp-includes/speculative-loading.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,36 +228,47 @@ static function ( string $href_exclude_path ) use ( $prefixer ): string {

$speculation_rules = new WP_Speculation_Rules();

$main_rule_conditions = array(
// Include any URLs within the same site.
array(
'href_matches' => $prefixer->prefix_path_pattern( '/*' ),
),
// Except for excluded paths.
array(
'not' => array(
'href_matches' => $href_exclude_paths,
),
),
// Also exclude rel=nofollow links, as certain plugins use that on their links that perform an action.
array(
'not' => array(
'selector_matches' => 'a[rel~="nofollow"]',
),
),
// Last but not least, exclude links that are explicitly marked to opt out.
array(
'not' => array(
'selector_matches' => ".no-{$mode}",
),
),
);

// If using 'prerender', we need to also exclude links that opt-out of 'prefetch' because it's part of 'prerender'.
if ( 'prerender' === $mode ) {
$main_rule_conditions[] = array(
'not' => array(
'selector_matches' => '.no-prefetch',
),
);
}

$speculation_rules->add_rule(
$mode,
'main',
array(
'source' => 'document',
'where' => array(
'and' => array(
// Include any URLs within the same site.
array(
'href_matches' => $prefixer->prefix_path_pattern( '/*' ),
),
// Except for excluded paths.
array(
'not' => array(
'href_matches' => $href_exclude_paths,
),
),
// Also exclude rel=nofollow links, as certain plugins use that on their links that perform an action.
array(
'not' => array(
'selector_matches' => 'a[rel~="nofollow"]',
),
),
// Last but not least, exclude links that are explicitly marked to opt out.
array(
'not' => array(
'selector_matches' => ".no-{$mode}",
),
),
),
'and' => $main_rule_conditions,
),
'eagerness' => $eagerness,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,13 @@ public function test_wp_get_speculation_rules_prerender_entries() {
$rules = $rules->jsonSerialize();

$this->assertArrayHasKey( 'prerender', $rules );
$this->assertCount( 4, $rules['prerender'][0]['where']['and'] );
$this->assertCount( 5, $rules['prerender'][0]['where']['and'] );
$this->assertArrayHasKey( 'not', $rules['prerender'][0]['where']['and'][3] );
$this->assertArrayHasKey( 'selector_matches', $rules['prerender'][0]['where']['and'][3]['not'] );
$this->assertSame( '.no-prerender', $rules['prerender'][0]['where']['and'][3]['not']['selector_matches'] );
$this->assertArrayHasKey( 'not', $rules['prerender'][0]['where']['and'][4] );
$this->assertArrayHasKey( 'selector_matches', $rules['prerender'][0]['where']['and'][4]['not'] );
$this->assertSame( '.no-prefetch', $rules['prerender'][0]['where']['and'][4]['not']['selector_matches'] );
}

/**
Expand Down

0 comments on commit 6827503

Please sign in to comment.