Make WordPress Core


Ignore:
Timestamp:
10/21/2025 11:30:43 AM (13 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/blocks/register.php

    r61008 r61019  
    428428                        ) === 0
    429429                );
     430        }
     431
     432        /**
     433         * Tests that blocks with supports.interactivity have the
     434         * `data-wp-router-options` directive.
     435         *
     436         * @ticket 64122
     437         *
     438         * @covers ::register_block_script_module_id
     439         */
     440        public function test_register_block_script_module_id_with_interactivity_true() {
     441                $metadata = array(
     442                        'file'             => DIR_TESTDATA . '/blocks/notice/block.json',
     443                        'viewScriptModule' => 'file:./block.js',
     444                );
     445
     446                $interactivity_true                    = array_merge(
     447                        $metadata,
     448                        array(
     449                                'name'     => 'tests/interactivity-true',
     450                                'supports' => array( 'interactivity' => true ),
     451                        )
     452                );
     453                $interactive_and_client_navigation     = array_merge(
     454                        $metadata,
     455                        array(
     456                                'name'     => 'tests/interactive-and-client-navigation',
     457                                'supports' => array(
     458                                        'interactivity' => array(
     459                                                'interactive'      => true,
     460                                                'clientNavigation' => true,
     461                                        ),
     462                                ),
     463                        )
     464                );
     465                $interactive_and_not_client_navigation = array_merge(
     466                        $metadata,
     467                        array(
     468                                'name'     => 'tests/interactive-and-not-client-navigation',
     469                                'supports' => array(
     470                                        'interactivity' => array(
     471                                                'interactive'      => true,
     472                                                'clientNavigation' => false,
     473                                        ),
     474                                ),
     475                        )
     476                );
     477                $not_interactive_and_client_navigation = array_merge(
     478                        $metadata,
     479                        array(
     480                                'name'     => 'tests/not-interactive-and-client-navigation',
     481                                'supports' => array(
     482                                        'interactivity' => array(
     483                                                'interactive'      => false,
     484                                                'clientNavigation' => true,
     485                                        ),
     486                                ),
     487                        )
     488                );
     489                $no_interactivity                      = array_merge(
     490                        $metadata,
     491                        array(
     492                                'name'     => 'tests/no-interactivity',
     493                                'supports' => array(),
     494                        )
     495                );
     496
     497                $interactivity_true_module_id                    = register_block_script_module_id( $interactivity_true, 'viewScriptModule' );
     498                $interactive_and_client_navigation_module_id     = register_block_script_module_id( $interactive_and_client_navigation, 'viewScriptModule' );
     499                $interactive_and_not_client_navigation_module_id = register_block_script_module_id( $interactive_and_not_client_navigation, 'viewScriptModule' );
     500                $not_interactive_and_client_navigation_module_id = register_block_script_module_id( $not_interactive_and_client_navigation, 'viewScriptModule' );
     501                $no_interactivity_module_id                      = register_block_script_module_id( $no_interactivity, 'viewScriptModule' );
     502                wp_enqueue_script_module( $interactivity_true_module_id );
     503                wp_enqueue_script_module( $interactive_and_client_navigation_module_id );
     504                wp_enqueue_script_module( $interactive_and_not_client_navigation_module_id );
     505                wp_enqueue_script_module( $not_interactive_and_client_navigation_module_id );
     506                wp_enqueue_script_module( $no_interactivity_module_id );
     507
     508                $output = get_echo( array( wp_script_modules(), 'print_enqueued_script_modules' ) );
     509
     510                $p = new WP_HTML_Tag_Processor( $output );
     511
     512                $this->assertTrue( $p->next_tag( array( 'tag_name' => 'SCRIPT' ) ), 'Expected there to be another SCRIPT.' );
     513                $this->assertSame( 'tests-interactivity-true-view-script-module-js-module', $p->get_attribute( 'id' ) );
     514                $this->assertSame( '{"loadOnClientNavigation":true}', $p->get_attribute( 'data-wp-router-options' ) );
     515
     516                $this->assertTrue( $p->next_tag( array( 'tag_name' => 'SCRIPT' ) ), 'Expected there to be another SCRIPT.' );
     517                $this->assertSame( 'tests-interactive-and-client-navigation-view-script-module-js-module', $p->get_attribute( 'id' ) );
     518                $this->assertSame( '{"loadOnClientNavigation":true}', $p->get_attribute( 'data-wp-router-options' ) );
     519
     520                $this->assertTrue( $p->next_tag( array( 'tag_name' => 'SCRIPT' ) ), 'Expected there to be another SCRIPT.' );
     521                $this->assertSame( 'tests-interactive-and-not-client-navigation-view-script-module-js-module', $p->get_attribute( 'id' ) );
     522                $this->assertNull( $p->get_attribute( 'data-wp-router-options' ) );
     523
     524                $this->assertTrue( $p->next_tag( array( 'tag_name' => 'SCRIPT' ) ), 'Expected there to be another SCRIPT.' );
     525                $this->assertSame( 'tests-not-interactive-and-client-navigation-view-script-module-js-module', $p->get_attribute( 'id' ) );
     526                $this->assertNull( $p->get_attribute( 'data-wp-router-options' ) );
     527
     528                $this->assertTrue( $p->next_tag( array( 'tag_name' => 'SCRIPT' ) ), 'Expected there to be another SCRIPT.' );
     529                $this->assertSame( 'tests-no-interactivity-view-script-module-js-module', $p->get_attribute( 'id' ) );
     530                $this->assertNull( $p->get_attribute( 'data-wp-router-options' ) );
    430531        }
    431532
Note: See TracChangeset for help on using the changeset viewer.