[wp-trac] [WordPress Trac] #60761: Interactivity API: Empty state objects are not correctly sent to the client

WordPress Trac noreply at wordpress.org
Thu Mar 14 16:23:02 UTC 2024


#60761: Interactivity API: Empty state objects are not correctly sent to the client
--------------------------------------+-------------------------
 Reporter:  jonsurrell                |       Owner:  jonsurrell
     Type:  defect (bug)              |      Status:  assigned
 Priority:  normal                    |   Milestone:  6.5
Component:  Editor                    |     Version:  trunk
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+-------------------------

Comment (by jonsurrell):

 Replying to [comment:5 gziolo]:
 > What's the implication of serving an empty array `[]` from the server
 instead of an object `{}`? Will it break something on the client if the
 client-side patch lands? I'm thinking about how severe the problem is to
 make a good decision whether to include in 6.5.0.

 In my opinion this fix is important and should be landed for 6.5. I'd like
 https://github.com/WordPress/gutenberg/pull/59842 to also be backported to
 6.5. I think those two changes together are sufficient to address the
 related problems I observed.

 The client expects `state` to be a JavaScript object. I explained some of
 this [https://github.com/WordPress/wordpress-
 develop/pull/6261#discussion_r1524471565 on the PR in this comment], but
 I'll summarize here.

 The client expects state to be a JavaScript object (`{}`) and does not
 currently do any validation on the initial state provided by the server,
 or any state the first time a store is created for a given namespace.
 Checks should be added to the client, but that may be a bigger change.
 Both the client and the server should be fixed to prevent non-objects from
 being stored in state.

 I've observed this problem of an array entering state breaking plugin code
 on the client. Looking at the code, it seems like the problem is the
 following:
 -
 [https://github.com/WordPress/gutenberg/blob/afc08a1ca41dfb4c2e9fd6effd3d9878c1955381/packages/interactivity/src/store.ts#L313-L345
 State data sent from the server is injected into the store namespaces.
 State values are not checked for whether they're objects.]
 -
 [https://github.com/WordPress/gutenberg/blob/afc08a1ca41dfb4c2e9fd6effd3d9878c1955381/packages/interactivity/src/store.ts#L270-L282
 Initially this data is stored.]
 - Subsequent calls to `store` are prevented from updating `state` because
 [https://github.com/WordPress/gutenberg/blob/afc08a1ca41dfb4c2e9fd6effd3d9878c1955381/packages/interactivity/src/store.ts#L307
 they would be merged], but the
 [https://github.com/WordPress/gutenberg/blob/afc08a1ca41dfb4c2e9fd6effd3d9878c1955381/packages/interactivity/src/store.ts#L22-L23
 merge function bails if either argument is not an object].

 I'm happy to also fix non-objects being stored in state on the first store
 call, that would be another client side PR. If that is fixed on the
 client, this change would become more of an optimization than a bugfix. It
 would still be incorrect to send `[]` state to the client, but it wouldn't
 be harmful.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60761#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list