Make WordPress Core


Ignore:
Timestamp:
10/21/2025 11:30:43 AM (12 months ago)
Author:
luisherranz
Message:

Interactivity API: Support for loadOnClientNavigation.

Uses the wp_script_attributes filter to add a data-wp-router-options directive with a loadOnClientNavigation: true property for all the interactive blocks that are compatible with client-side navigation to let the Interactivity API router determine which modules it can safely load during client-side navigation.

Props luisherranz, westonruter.
Fixes #64122.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php

    r60953 r61019  
    2727                $this->interactivity = new WP_Interactivity_API();
    2828                wp_default_script_modules();
     29                $this->interactivity->add_hooks();
     30        }
     31
     32        /**
     33         * Tear down.
     34         */
     35        public function tear_down() {
     36                global $wp_script_modules;
     37                parent::tear_down();
     38                $wp_script_modules = null;
    2939        }
    3040
    … …  
    237247         */
    238248        private function get_script_data_filter_result( ?Closure $callback = null ): MockAction {
    239                 $this->interactivity->add_hooks();
    240249                wp_enqueue_script_module( '@wordpress/interactivity' );
    241250                $filter = new MockAction();
    … …  
    17241733                $this->assertStringNotContainsString( 'class="[disallowed]"', $processed_html );
    17251734        }
     1735
     1736        /**
     1737         * Tests that add_client_navigation_support_to_script_module marks a
     1738         * script module for client navigation.
     1739         *
     1740         * @ticket 64122
     1741         *
     1742         * @covers WP_Interactivity_API::add_client_navigation_support_to_script_module
     1743         * @covers WP_Interactivity_API::add_load_on_client_navigation_attribute_to_script_modules
     1744         */
     1745        public function test_add_client_navigation_support_to_script_module() {
     1746                $this->interactivity->add_client_navigation_support_to_script_module( 'marked-module' );
     1747
     1748                wp_register_script_module( 'marked-module', '/marked.js' );
     1749                wp_register_script_module( 'unmarked-module', '/unmarked.js' );
     1750                wp_enqueue_script_module( 'marked-module' );
     1751                wp_enqueue_script_module( 'unmarked-module' );
     1752
     1753                $output = get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) );
     1754
     1755                $p = new WP_HTML_Tag_Processor( $output );
     1756
     1757                // First module: marked-module should have the attribute.
     1758                $p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
     1759                $this->assertSame( 'marked-module-js-module', $p->get_attribute( 'id' ) );
     1760                $this->assertSame(
     1761                        '{"loadOnClientNavigation":true}',
     1762                        $p->get_attribute( 'data-wp-router-options' )
     1763                );
     1764
     1765                // Second module: unmarked-module should NOT have the attribute.
     1766                $p->next_tag( array( 'tag_name' => 'SCRIPT' ) );
     1767                $this->assertSame( 'unmarked-module-js-module', $p->get_attribute( 'id' ) );
     1768                $this->assertNull( $p->get_attribute( 'data-wp-router-options' ) );
     1769        }
    17261770}
Note: See TracChangeset for help on using the changeset viewer.