Interactivity API: Add getServerState() and getServerContext()#65151
Interactivity API: Add getServerState() and getServerContext()#65151
getServerState() and getServerContext()#65151Conversation
|
Size Change: +724 B (+0.04%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
ce56700 to
2392742
Compare
9cc32c7 to
60198c8
Compare
60198c8 to
98e959b
Compare
88a8e6b to
580aba6
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
…65151) * Expose client and server context from provider * Create `getServerContext` * Add simple test for server context * Implement `getServerState` * Add tests for read-only state proxies * Add e2e tests for `getServerState()` * Avoid PHPCS UndefinedVariable error * Add e2e tests for `getServerContext` WIP * Finish e2e tests for `getServerContext()` * Update `getServerState()` tests * Revert "Add simple test for server context" This reverts commit 7e6f530. * Update TSDocs Co-authored-by: DAreRodz <darerodz@git.wordpress.org> Co-authored-by: luisherranz <luisherranz@git.wordpress.org> Co-authored-by: sethrubenstein <smrubenstein@git.wordpress.org> Co-authored-by: michalczaplinski <czapla@git.wordpress.org>
|
I just cherry-picked this PR to the wp/6.7 branch to get it included in the next release: fa1a0d6 |
|
Great job finishing this task. @DAreRodz, it's going to be included in WordPress 6.7 and will need an accompanying dev note (added label) and documentation in the Interactivity API reference. |
…65151) * Expose client and server context from provider * Create `getServerContext` * Add simple test for server context * Implement `getServerState` * Add tests for read-only state proxies * Add e2e tests for `getServerState()` * Avoid PHPCS UndefinedVariable error * Add e2e tests for `getServerContext` WIP * Finish e2e tests for `getServerContext()` * Update `getServerState()` tests * Revert "Add simple test for server context" This reverts commit 7e6f530. * Update TSDocs Co-authored-by: DAreRodz <darerodz@git.wordpress.org> Co-authored-by: luisherranz <luisherranz@git.wordpress.org> Co-authored-by: sethrubenstein <smrubenstein@git.wordpress.org> Co-authored-by: michalczaplinski <czapla@git.wordpress.org>
|
I just cherry-picked this PR to the release/19.3 branch to get it included in the next release: 4c9fdd1 |
What?
Fixes: #65243
Exposes
getServerState()andgetServerContext()from the@wordpress/interactivitypackage.These functions return an object that is analogous to the state/context returned by
store()/getContext(), with a couple of differences:navigate()calls, using the values defined in the HTML.Example:
Why?
These functions are required for subscribing to changes in state/context when the HTML is updated using
navigate().How?
Both the state and the context use
proxifyState()internally, a function that wraps objects with proxies and handles all the reactivity and property subscriptions. This function has been extended to accept a{ readOnly }option that, whentrue, prevents any modification to the given object and nested ones.The objects returned by
getServerState()andgetServerContext()usereadOnly: true, and it's the functiondeepMerge(), refactored in #64879, the one that updates the server state and the server context.The function
populateServerData()has been changed to update the server state. As that function is already used innavigate(), no changes in@wordpress/interactivity-routerwere required.Lastly, the implementation of the
data-wp-contextdirective has been adapted to handle the server context updates and inheritance.Testing Instructions
I've added unit tests and e2e tests.