This repository was archived by the owner on Oct 13, 2025. It is now read-only.
Repository navigation
iAPI Router: Reimplement the full-page, client-side navigation feature #13
Merged
DAreRodz
merged 24 commits into
interactivity-api
from
iapi-move-full-page-logic-to-its-own-module
May 12, 2025
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d6428fd
WIP Move full-page logic to its own module
DAreRodz 230f41a
Fix interactivity-router TS with sub-packages
sirreal 2c87637
Needs to be composite project
sirreal 429df79
Add empty files for good measure
sirreal ab264a5
Add package.json export
sirreal 23e6a96
Remove extra whitespace
DAreRodz 0fb4976
Remove exports property
DAreRodz 50d7092
Add full-page client-side navigation support
DAreRodz efe5cad
Import script modules before render
DAreRodz 5337993
Turn php logic into a class
DAreRodz 35c814d
Change default mode priority to 9
DAreRodz e749c9b
Fix init action in WP_Interactivity_API_Full_Page_Navigation
DAreRodz d112a77
Prevent warning accessing to clientNavigationMode
DAreRodz f7b458d
Change body router region ID to core/body
DAreRodz 2ae7c4b
Remove unnecessary `body` prop inside `regions`
DAreRodz c371c86
Merge branch 'interactivity-api' into iapi-move-full-page-logic-to-it…
DAreRodz 2679a52
Add a filter to enable/disable the experimental feature
DAreRodz ad96ad3
Use `wp_body_open` instead of `wp_footer`
DAreRodz d949f0b
Move the experiment filter to `set_default_mode`
DAreRodz 503e965
Merge branch 'interactivity-api' into iapi-move-full-page-logic-to-it…
DAreRodz 8b70137
Remove full-page unlock
DAreRodz 9a64d97
Only add the client navigation mode when full page is enabled
DAreRodz 51f5ed2
Move Full Page class to experimental folder
DAreRodz 7aa02e5
End buffer on `wp_footer` instead of `wp_body_open`
DAreRodz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
lib/experimental/interactivity-api/class-wp-interactivity-api-full-page-navigation.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <?php | ||
| /** | ||
| * Enables full page client-side navigation for the Interactivity API. | ||
| * | ||
| * IMPORTANT: this code is not meant to be included in Gutenberg yet. Instead, | ||
| * it will be part of an external plugin to test this feature separately. | ||
| */ | ||
|
|
||
| if ( ! class_exists( 'WP_Interactivity_API_Full_Page_Navigation' ) ) { | ||
|
|
||
| /** | ||
| * Class to enable full page client-side navigation for the Interactivity API. | ||
| */ | ||
| class WP_Interactivity_API_Full_Page_Navigation { | ||
|
|
||
| private static $instance = null; | ||
|
|
||
| private static $unlock_message = 'I acknowledge that full-page client-side navigation is still experimental and will probably change, breaking my plugin or website on its next version.'; | ||
|
|
||
| public function __construct() { | ||
| add_action( 'init', array( $this, 'set_default_mode' ), 9 ); | ||
| add_action( 'wp_head', array( $this, 'buffer_start' ) ); | ||
| add_action( 'wp_footer', array( $this, 'buffer_end' ), 8 ); | ||
| add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_script_modules' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether the client navigation mode is `experimentalFullPage`. | ||
| */ | ||
| public function is_enabled() { | ||
| $iapi_router_config = wp_interactivity_config( 'core/router' ); | ||
| return 'experimentalFullPage' === ( $iapi_router_config['clientNavigationMode'] ?? '' ); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the client navigation mode by default. | ||
| */ | ||
| public function set_default_mode() { | ||
| $unlock_message = apply_filters( 'wp_interactivity_experimental_full_page_client_navigation', '' ); | ||
| if ( self::$unlock_message === $unlock_message ) { | ||
| wp_interactivity_config( | ||
| 'core/router', | ||
| array( 'clientNavigationMode' => 'experimentalFullPage' ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Enqueues the required script modules. | ||
| */ | ||
| public function enqueue_script_modules() { | ||
| if ( $this->is_enabled() ) { | ||
| wp_enqueue_script_module( | ||
| '@wordpress/interactivity-router/full-page' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Starts output buffering at the end of the 'wp_head' action, adding the | ||
| * required directives for client-side navigation to the BODY tags when the | ||
| * buffer is flushed. | ||
| */ | ||
| public function buffer_start() { | ||
| if ( $this->is_enabled() ) { | ||
| ob_start( array( $this, 'add_directives_to_body' ) ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Flushes the output buffer at the end of the 'wp_footer' action. | ||
| */ | ||
| public function buffer_end() { | ||
| if ( $this->is_enabled() ) { | ||
| ob_end_flush(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adds client-side navigation directives to BODY tag in the passed output | ||
| * buffer. | ||
| * | ||
| * Note: This should probably be done per site, not by default when this option | ||
| * is enabled. | ||
| * | ||
| * @param string $buffer Passed output buffer. | ||
| * | ||
| * @return string The same HTML with modified BODY attributes. | ||
| */ | ||
| public function add_directives_to_body( $buffer ) { | ||
| $p = new WP_HTML_Tag_Processor( $buffer ); | ||
| if ( $p->next_tag( array( 'tag_name' => 'BODY' ) ) ) { | ||
| $p->set_attribute( 'data-wp-interactive', true ); | ||
| $p->set_attribute( 'data-wp-router-region', 'core/body' ); | ||
| return $p->get_updated_html(); | ||
| } else { | ||
| return $buffer; | ||
| } | ||
| } | ||
|
|
||
| public static function instance() { | ||
| if ( null === self::$instance ) { | ||
| self::$instance = new WP_Interactivity_API_Full_Page_Navigation(); | ||
| } | ||
| return self::$instance; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { getConfig } from '@wordpress/interactivity'; | ||
|
|
||
| // Check if the navigation mode is full page or region based. | ||
| const clientNavigationMode: 'regionBased' | 'experimentalFullPage' = | ||
| getConfig( 'core/router' ).clientNavigationMode ?? 'regionBased'; | ||
|
|
||
| // Check if the link is valid for client-side navigation. | ||
| const isValidLink = ( ref: HTMLAnchorElement ) => | ||
| ref && | ||
| ref instanceof window.HTMLAnchorElement && | ||
| ref.href && | ||
| ( ! ref.target || ref.target === '_self' ) && | ||
| ref.origin === window.location.origin && | ||
| ! ref.pathname.startsWith( '/wp-admin' ) && | ||
| ! ref.pathname.startsWith( '/wp-login.php' ) && | ||
| ! ref.getAttribute( 'href' ).startsWith( '#' ) && | ||
| ! new URL( ref.href ).searchParams.has( '_wpnonce' ); | ||
|
|
||
| // Check if the event is valid for client-side navigation. | ||
| const isValidEvent = ( event: MouseEvent ) => | ||
| event && | ||
| event.button === 0 && // Left clicks only. | ||
| ! event.metaKey && // Open in new tab (Mac). | ||
| ! event.ctrlKey && // Open in new tab (Windows). | ||
| ! event.altKey && // Download. | ||
| ! event.shiftKey && | ||
| ! event.defaultPrevented; | ||
|
|
||
| // Add click and prefetch to all links. | ||
| if ( clientNavigationMode === 'experimentalFullPage' ) { | ||
| // Navigate on click. | ||
| document.addEventListener( | ||
| 'click', | ||
| async ( event ) => { | ||
| const ref = ( event.target as Element ).closest( 'a' ); | ||
| if ( isValidLink( ref ) && isValidEvent( event ) ) { | ||
| event.preventDefault(); | ||
| const { actions } = await import( | ||
| '@wordpress/interactivity-router' | ||
| ); | ||
| actions.navigate( ref.href ); | ||
| } | ||
| }, | ||
| true | ||
| ); | ||
| // Prefetch on hover. | ||
| document.addEventListener( | ||
| 'mouseenter', | ||
| async ( event ) => { | ||
| if ( ( event.target as Element )?.nodeName === 'A' ) { | ||
| const ref = ( event.target as Element ).closest( 'a' ); | ||
| if ( isValidLink( ref ) && isValidEvent( event ) ) { | ||
| const { actions } = await import( | ||
| '@wordpress/interactivity-router' | ||
| ); | ||
| actions.prefetch( ref.href ); | ||
| } | ||
| } | ||
| }, | ||
| true | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/tsconfig.json", | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "allowJs": false, | ||
| "checkJs": false, | ||
| "strict": false | ||
| }, | ||
| "references": [ { "path": "tsconfig.main.json" } ], | ||
| "files": [ "src/full-page.ts" ], | ||
| "include": [], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/tsconfig.json", | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "checkJs": false, | ||
| "strict": false | ||
| "composite": true | ||
| }, | ||
| "references": [ { "path": "../a11y" }, { "path": "../interactivity" } ] | ||
| "files": [], | ||
| "references": [ | ||
| { "path": "../a11y" }, | ||
| { "path": "../interactivity" }, | ||
| { "path": "tsconfig.main.json" }, | ||
| { "path": "tsconfig.full-page.json" } | ||
| ] | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/tsconfig.json", | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "allowJs": false, | ||
| "checkJs": false, | ||
| "strict": false | ||
| }, | ||
| "references": [ { "path": "../a11y" }, { "path": "../interactivity" } ], | ||
| "exclude": [ | ||
| "**/*.android.js", | ||
| "**/*.ios.js", | ||
| "**/*.native.js", | ||
| "**/benchmark", | ||
| "build-*/**", | ||
| "build/**", | ||
| "**/test/**", | ||
| "**/react-native-*/**", | ||
| "src/full-page.ts" | ||
| ] | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.