fix: resolve the REST index locally on namespaced sites - #617
Conversation
XCFramework BuildThis PR's XCFramework is available for testing. Add the following to your .package(url: "https://github.com/wordpress-mobile/GutenbergKit", branch: "pr-build/617")Built from ef26163 |
Gutenberg's `root`/`__unstableBase` entity requests the REST API index (`/`) during editor initialization. On namespaced sites the path has no segments for the namespace middleware to rewrite, so the request targets the API host's root, which serves no index and fails. Resolve the entity locally with the site URL the host already provides. The remaining fields are either unavailable on namespaced sites or read from the `site` entity by the blocks that use them. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017jPW5y3FrRywuHHn3gAHgu
a1725eb to
ef26163
Compare
|
Looks good, but Claude found these could of points that might be worth a look:
|
What?
Resolve Gutenberg's REST API index request locally on namespaced sites instead of sending it to the API host's root.
Why?
This was primarily implemented to address an observed request failure in the console. There was no user-facing issue observed.
The editor fails a request on every launch for a namespaced site. Gutenberg's
root/__unstableBaseentity fetches the REST API index (/?_fields=...) during initialization. That path has no segments forapiPathModifierMiddlewareto insert the namespace into, so the request targets the bare API host root, which serves no index. Over HTTP origins (the Vite dev server, Android) it fails CORS preflight; fromfile://on iOS it follows a redirect to a docs page and fails JSON parsing.The only unconditional consumer,
useBlockEditorSettings, reads image sizes for client-side media processing, which GutenbergKit does not enable. The Site Title, Tagline, and Logo blocks read thesiteentity when the user can edit settings and only fall back to this record otherwise. No namespaced route serves the index, so there is nothing to redirect to.How?
Add
siteIndexMiddlewaretoconfigureApiFetch. For GET requests to/on a site with a configured API namespace, it resolves withhomeandurlfrom the host'ssiteURLrather than callingnext. Every other request passes through unchanged. It runs after the preloading middleware so a host-supplied index entry still takes precedence.Testing Instructions
Tip
Use the bundled build rather than the Vite dev server for step 4. Some hosts answer the index request from a dev-server origin with a 429 that carries no CORS headers, which the browser reports as a CORS failure unrelated to this change.
make buildand launch the demo app against a namespaced site (e.g., WordPress.com Simple)./?_fields=...) appears and the editor loads normally.<siteApiRoot>/?_fields=...and succeeds.npm run test:unit -- src/utils/api-fetch.test.jsExample root
/?_fields=...request:Accessibility Testing Instructions
No UI changes.
🤖 Generated with Claude Code
https://claude.ai/code/session_017jPW5y3FrRywuHHn3gAHgu