Make WordPress Core


Ignore:
Timestamp:
10/17/2025 02:33:53 PM (13 months ago)
Author:
luisherranz
Message:

Interactivity API: Support lazy-loaded derived state props.

Serialize on the server the paths of derived state closures accessed during directive processing and include them in the serialized data. During the Interactivity API hydration, if a directive reads one of those paths before its JavaScript getter is available, keep the server-rendered value intact instead of replacing it with undefined.

props darerodz.
Fixes #63898.

File:
1 edited

Legend:

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

    r60729 r60953  
    2626                parent::set_up();
    2727                $this->interactivity = new WP_Interactivity_API();
     28                wp_default_script_modules();
    2829        }
    2930
    … …  
    315316
    316317                $this->assertSame( array( 'state' => array( 'myPlugin' => array( 'emptyArray' => array() ) ) ), $filter->get_args()[0][0] );
     318        }
     319        /**
     320         * Tests that derived state props invoked during directive evaluation are
     321         * serialized correctly.
     322         *
     323         * @ticket 63898
     324         */
     325        public function test_invoked_derived_state_props_are_serialized() {
     326                $returns_whatever = function () {
     327                        return 'whatever';
     328                };
     329
     330                $returns_array = function () {
     331                        return array( 'prop' => 'whatever' );
     332                };
     333
     334                $filter = $this->get_script_data_filter_result(
     335                        function () use ( $returns_whatever, $returns_array ) {
     336                                $this->interactivity->state(
     337                                        'pluginWithInvokedDerivedState',
     338                                        array(
     339                                                'derivedProp' => $returns_whatever,
     340                                                'nested'      => array(
     341                                                        'derivedProp'             => $returns_whatever,
     342                                                        'derivedPropReturnsArray' => $returns_array,
     343                                                ),
     344                                        )
     345                                );
     346
     347                                $this->interactivity->state(
     348                                        'pluginWithInvokedDerivedStateReturningArray',
     349                                        array(
     350                                                'derivedProp' => $returns_whatever,
     351                                                'nested'      => array(
     352                                                        'derivedProp'             => $returns_whatever,
     353                                                        'derivedPropReturnsArray' => $returns_array,
     354                                                ),
     355                                        )
     356                                );
     357
     358                                $this->interactivity->state(
     359                                        'pluginWithoutInvokedDerivedState',
     360                                        array(
     361                                                'derivedProp' => $returns_whatever,
     362                                                'nested'      => array(
     363                                                        'derivedProp' => $returns_whatever,
     364                                                ),
     365                                        )
     366                                );
     367
     368                                $this->set_internal_context_stack( array() );
     369
     370                                // Multiple evaluations should be serialized only once.
     371                                $this->set_internal_namespace_stack( 'pluginWithInvokedDerivedState' );
     372                                $this->evaluate( 'state.derivedProp' );
     373                                $this->evaluate( 'state.derivedProp' );
     374                                $this->evaluate( 'state.nested.derivedProp' );
     375                                $this->evaluate( 'state.nested.derivedProp' );
     376
     377                                // Only the path part that points to a derived state prop should be serialized.
     378                                $this->set_internal_namespace_stack( 'pluginWithInvokedDerivedStateReturningArray' );
     379                                $this->evaluate( 'state.nested.derivedProp.prop' );
     380                        }
     381                );
     382
     383                $this->assertSame(
     384                        array(
     385                                'state'                => array(
     386                                        'pluginWithInvokedDerivedState'    => array(
     387                                                'derivedProp' => $returns_whatever,
     388                                                'nested'      => array(
     389                                                        'derivedProp'             => $returns_whatever,
     390                                                        'derivedPropReturnsArray' => $returns_array,
     391                                                ),
     392                                        ),
     393                                        'pluginWithInvokedDerivedStateReturningArray' => array(
     394                                                'derivedProp' => $returns_whatever,
     395                                                'nested'      => array(
     396                                                        'derivedProp'             => $returns_whatever,
     397                                                        'derivedPropReturnsArray' => $returns_array,
     398                                                ),
     399                                        ),
     400                                        'pluginWithoutInvokedDerivedState' => array(
     401                                                'derivedProp' => $returns_whatever,
     402                                                'nested'      => array(
     403                                                        'derivedProp' => $returns_whatever,
     404                                                ),
     405                                        ),
     406                                ),
     407                                'derivedStateClosures' => array(
     408                                        'pluginWithInvokedDerivedState' => array(
     409                                                'state.derivedProp',
     410                                                'state.nested.derivedProp',
     411                                        ),
     412                                        'pluginWithInvokedDerivedStateReturningArray' => array(
     413                                                'state.nested.derivedProp',
     414                                        ),
     415                                ),
     416                        ),
     417                        $filter->get_args()[0][0]
     418                );
    317419        }
    318420
Note: See TracChangeset for help on using the changeset viewer.