Repository navigation
Trac63946/rest lazy load namespaces - #10080
prettyboymp wants to merge 16 commits into
Conversation
|
Hi @prettyboymp! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
…change around namespaces that partially match existing namespaces
| * ) ); | ||
| * } ); | ||
| */ | ||
| do_action( 'rest_lazy_load_namespace_' . $route_namespace ); |
There was a problem hiding this comment.
This somewhat breaks from convention around only using underscores for hooks since by their own convention namespaces are almost always going to contain dashes and slashes. Would it be better to convert these for the hook for the sake of convention or leave it as is?
There was a problem hiding this comment.
This also isn't a recommended pattern anymore. The namespace should just be it's own parameter.
There was a problem hiding this comment.
This also isn't a recommended pattern anymore. The namespace should just be it's own parameter.
@TimothyBJacobs, do you happen to have a reference to some relevant discussion around this? There seem to still be (relatively) recent hooks added with this pattern.
I would hate for every callback created for lazy route registration to have to start with if( '/my-plugin-namespace' === $route_namespace )....
There was a problem hiding this comment.
Usually WP provides hooks for both. e.g. do_action( 'rest_lazy_load_namespace_' . $route_namespace, $route_namespace ); and do_action( 'rest_lazy_load_namespace', $route_namespace );
|
I rebased against trunk and performed a Claude review. There may be some issues that need to be addressed so sharing them here before looking deeper and/or asking for more eyes. Review NotesPositives
Issues & Concerns1. After calling 'namespaces' => array_keys( $this->namespaces ),If a lazy-loaded namespace's action registers routes via 'namespaces' => array_keys( $this->namespaces + $this->lazy_namespaces ),2.
3. Namespace matching in if ( str_starts_with( trailingslashit( ltrim( $path, '/' ) ), $namespace ) ) {This checks if the path starts with the namespace string, but without a trailing slash on if ( str_starts_with( trailingslashit( ltrim( $path, '/' ) ), trailingslashit( $namespace ) ) ) {(Note: this bug likely pre-exists in trunk's 4. Validation order in The 5. Typo in comment
6. Missing
7. The generic 8. No test for The global 9. Route priority tests seem unrelated to lazy loading
|
- Fix namespace prefix collision in match_request_to_handler() by adding trailingslashit() to namespace comparison. - Reorder validation in register_rest_namespace() so is_string() check runs before empty() to avoid array-to-string warnings. - Fix "namespases" typo. - Add missing @SInCE tags to new methods. - Flesh out rest_lazy_load_namespace action docblock with description and @PARAM. - Clarify route priority test docblocks to explain their relevance to lazy loading changes. - Resolve @todo in test: lazy namespaces intentionally excluded from get_namespaces() until loaded. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@kraftbj, thanks for the reviewing. I addressed several items in 9233b0f. 1. 2. 3. Namespace prefix collision — Fixed. Added 4. Validation order - Fixed. 5. Typo — Fixed. 6. Missing 7. Generic action docblock - Fixed. Added full description and 8. Validation path tests - Skipping for now, but happy to add if we think its helpful. There's no existing precedent for testing 9. Route priority test docblocks - Fixed. Added clarifying text that these document existing route priority behavior that the lazy loading changes to |
kraftbj
left a comment
There was a problem hiding this comment.
Follow-up review after the March updates. The big items from my earlier review are resolved — these are minor doc/test items.
| } | ||
|
|
||
| $routes = $server->get_routes(); | ||
| $routes = $server->get_routes( '', $server::ROUTE_CONTEXT_LOADED_ONLY ); |
There was a problem hiding this comment.
This changed from $server->get_routes() (which would load all lazy namespaces) to ROUTE_CONTEXT_LOADED_ONLY. This is likely correct since rest_send_allow_header runs after dispatch, so the relevant namespace is already loaded. But it'd be worth a brief comment here explaining why LOADED_ONLY is safe — something like "The matching namespace was already loaded during dispatch." Without that context, a future reader might think this is a bug.
| * | ||
| * @return bool True on success, false on failure. | ||
| * | ||
| * @since X.X.0 |
There was a problem hiding this comment.
Minor docblock ordering: per WP inline docs standards, @since should appear before @param and @return, not after them. Current order is @param → @return → @since → @example; should be @since → @param → @return.
| * | ||
| * @since X.X.0 | ||
| * | ||
| * @example |
There was a problem hiding this comment.
WP core inline docs standards don't use the @example tag — code examples go in the description as fenced code blocks instead. Same applies to the @example in class-wp-rest-server.php line 955.
| * | ||
| * @ticket 63946 | ||
| */ | ||
| public function test_lazy_namespace_loading_via_action() { |
There was a problem hiding this comment.
This test covers the namespace-specific rest_lazy_load_namespace_lazy-action/v1 action, but there's no test verifying the generic rest_lazy_load_namespace action also fires (with the correct $route_namespace parameter). A small addition to this test or a separate test case would close that gap.
| * | ||
| * @ticket 63946 | ||
| */ | ||
| public function test_get_routes_empty_namespace_loads_all_lazy() { |
There was a problem hiding this comment.
There's no test that explicitly verifies get_routes('', ROUTE_CONTEXT_LOADED_ONLY) does not trigger lazy loading. That contract is important since several call sites depend on it (rest_send_allow_header, get_index after preloading, get_namespace_index, and the fallback in match_request_to_handler). A test like: register a lazy namespace, call get_routes('', ROUTE_CONTEXT_LOADED_ONLY), assert the lazy action was never fired.
|
I'm going to test this out. My immediate instinct is that we should do the lazy register_rest_route registration in 7.1, and milestone this for evaluation in 7.2 once we have a baseline on how much that other one moves the needle. Thanks very much for the contribution! |
Adds support for registering namespaces to the REST API Server for the purposes of lazy loading. The server will trigger a action specific to that namespace when corresponding routes would need to be loaded. This provides a performance improvement when adding new namespaces and routes by reducing the number of routes that need to be checked for matches and avoiding unnecessary translation processing.
Trac ticket: https://core.trac.wordpress.org/ticket/63946
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.