Skip to content

Commit 987d6bc

Browse files
committed
Enhance interactivity-router plugin by adding prefetch functionality for links and updating the rendering logic. Non-interactive router links are now excluded from click actions, and new prefetch actions are implemented in both the view and render files for improved navigation performance.
1 parent 5dc8652 commit 987d6bc

File tree

7 files changed

+60
-11
lines changed

7 files changed

+60
-11
lines changed

plugins/interactivity-router-2f43f8/build/render.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@
3434
data-wp-watch--newPage="callbacks.newPage"
3535
>
3636
<a href="/">🏠 Start Page</a>
37-
<p><small><em>Displaying region "region-example-2f43f8" from <a data-wp-bind--href="state.urlRegionDisplay" data-wp-text="state.urlRegionDisplay"></a></em></small></p>
37+
<p><small><em>Displaying region "region-example-2f43f8" from <a class="non-interactive-router-link" data-wp-bind--href="state.urlRegionDisplay" data-wp-text="state.urlRegionDisplay"></a> (non-interactive router link)</em></small></p>
3838
<ul class="beatles-links">
3939
<template data-wp-each="state.slugs">
4040
<li><a
41-
data-wp-on--click="actions.navigate"
4241
data-wp-text="state.itemName"
4342
data-wp-bind--href="state.itemSlug"
4443
data-wp-class--current="state.isCurrentSlug"
@@ -52,13 +51,11 @@
5251
<a
5352
class="navigation-link-prev"
5453
data-wp-bind--hidden="!state.prev"
55-
data-wp-on--click="actions.navigate"
5654
data-wp-bind--href="state.prev"
5755
>&lt; Prev</a>
5856
<a
5957
class="navigation-link-next"
6058
data-wp-bind--hidden="!state.next"
61-
data-wp-on--click="actions.navigate"
6259
data-wp-bind--href="state.next"
6360
>Next &gt;</a>
6461
</nav>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => 'ddc3aea312f07cd92ede', 'type' => 'module');
1+
<?php return array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => 'c7ca229104ca13d76abf', 'type' => 'module');

plugins/interactivity-router-2f43f8/build/view.js

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/interactivity-router-2f43f8/build/view.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/interactivity-router-2f43f8/plugin.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ function bde_2f43f8__render_block_interactivity_router( $block_content ) {
6464

6565
$p = new WP_HTML_Tag_Processor( $block_content );
6666
while ( $p->next_tag( array( 'tag_name' => 'a' ) ) ) {
67+
if ( $p->get_attribute( 'class' ) !== 'non-interactive-router-link' ) {
6768
$p->set_attribute( 'data-wp-on--click', 'actions.navigate' );
69+
$p->set_attribute( 'data-wp-on--mouseenter', 'actions.prefetch' );
70+
$p->set_attribute( 'data-wp-watch', 'callbacks.prefetch' );
71+
}
6872
}
6973

7074
return $p->get_updated_html();

plugins/interactivity-router-2f43f8/src/render.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@
3434
data-wp-watch--newPage="callbacks.newPage"
3535
>
3636
<a href="/">🏠 Start Page</a>
37-
<p><small><em>Displaying region "region-example-2f43f8" from <a data-wp-bind--href="state.urlRegionDisplay" data-wp-text="state.urlRegionDisplay"></a></em></small></p>
37+
<p><small><em>Displaying region "region-example-2f43f8" from <a class="non-interactive-router-link" data-wp-bind--href="state.urlRegionDisplay" data-wp-text="state.urlRegionDisplay"></a> (non-interactive router link)</em></small></p>
3838
<ul class="beatles-links">
3939
<template data-wp-each="state.slugs">
4040
<li><a
41-
data-wp-on--click="actions.navigate"
4241
data-wp-text="state.itemName"
4342
data-wp-bind--href="state.itemSlug"
4443
data-wp-class--current="state.isCurrentSlug"
@@ -52,13 +51,11 @@
5251
<a
5352
class="navigation-link-prev"
5453
data-wp-bind--hidden="!state.prev"
55-
data-wp-on--click="actions.navigate"
5654
data-wp-bind--href="state.prev"
5755
>&lt; Prev</a>
5856
<a
5957
class="navigation-link-next"
6058
data-wp-bind--hidden="!state.next"
61-
data-wp-on--click="actions.navigate"
6259
data-wp-bind--href="state.next"
6360
>Next &gt;</a>
6461
</nav>

plugins/interactivity-router-2f43f8/src/view.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
import { store, getServerState, getContext } from '@wordpress/interactivity';
1+
import {
2+
store,
3+
getServerState,
4+
getContext,
5+
getElement,
6+
} from '@wordpress/interactivity';
7+
8+
const isValidLink = ( ref ) =>
9+
ref &&
10+
ref instanceof window.HTMLAnchorElement &&
11+
ref.href &&
12+
( ! ref.target || ref.target === '_self' ) &&
13+
ref.origin === window.location.origin;
14+
15+
// Shared prefetch logic
16+
const prefetchLink = function* ( ref ) {
17+
if ( isValidLink( ref ) ) {
18+
const { actions } = yield import( '@wordpress/interactivity-router' );
19+
yield actions.prefetch( ref.href );
20+
}
21+
};
222

323
const { state } = store( 'router-2f43f8', {
424
state: {
@@ -27,6 +47,10 @@ const { state } = store( 'router-2f43f8', {
2747
state.urlRegionDisplay = e.target.href;
2848
yield actions.navigate( e.target.href );
2949
},
50+
*prefetch() {
51+
const { ref } = getElement();
52+
yield* prefetchLink( ref );
53+
},
3054
},
3155
callbacks: {
3256
newPage() {
@@ -35,5 +59,9 @@ const { state } = store( 'router-2f43f8', {
3559
state.next = serverState.next;
3660
state.currentSlug = serverState.currentSlug;
3761
},
62+
*prefetch() {
63+
const { ref } = getElement();
64+
yield* prefetchLink( ref );
65+
},
3866
},
3967
} );

0 commit comments

Comments
 (0)