#64649 closed enhancement (fixed)
Interactivity API: Initialize router's `state.url` on the server
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Interactivity API | Keywords: | has-patch has-unit-tests |
| Focuses: | Cc: |
Description
The Interactivity API router currently initializes state.url on the client using window.location.href. With the changes in Gutenberg PR #70882, this initialization was moved to a fallback pattern (state.url = state.url || window.location.href) so that the server can populate it first.
This ticket adds the server-side initialization of state.url in the core/router namespace. The URL is set during the processing of the data-wp-router-region directive, using the same pattern that WordPress core uses internally to construct the current request URL (see redirect_canonical() and get_self_link()):
wp_interactivity_state(
'core/router',
array(
'url' => set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ),
)
);
This enables proper server-side rendering of directives that reference state.url in the core/router store.
Change History (3)
This ticket was mentioned in PR #10944 on WordPress/wordpress-develop by @luisherranz.
3 weeks ago
#1
- Keywords has-patch has-unit-tests added
#2
@
3 weeks ago
- Owner set to luisherranz
- Resolution set to fixed
- Status changed from new to closed
In 61659:
@luisherranz commented on PR #10944:
3 weeks ago
#3
Thanks (as always), Weston! Committed 🙏
### What
This PR populates
state.urlin thecore/routerInteractivity API namespace on the server, inside thedata-wp-router-regiondirective processor.Trac ticket: https://core.trac.wordpress.org/ticket/64649
### Why
After Gutenberg PR https://github.com/WordPress/gutenberg/pull/70882,
state.urlin the interactivity router is no longer unconditionally set towindow.location.hrefon the client. Instead, it falls back towindow.location.hrefonly if the server hasn't already provided a value:This PR provides that server-side value by calling
wp_interactivity_state( 'core/router', ... )during directive processing, following the same URL construction pattern used byredirect_canonical()andget_self_link().### How
wp_interactivity_state( 'core/router', array( 'url' => ... ) )call insidedata_wp_router_region_processor()inWP_Interactivity_API.set_url_scheme(),$_SERVER['HTTP_HOST'], and$_SERVER['REQUEST_URI'].### Testing
data-wp-router-region)."core/router"with the"url"property matching the current page URL.### Use of AI tools
Claude Code was used to research the best way to construct the URL and how other internal WordPress functions do it, and assist in creating the tests.