Repository navigation
Description
Follow-up to #6657 - cc @danielbachhuber. This PR introduced a magic value of -1 into a couple of API endpoints, which means to return an "unbounded" number of items:
Lines 512 to 513 in fb804e2
| // Change from '1' to '-1', which means unlimited. | |
| $query_params['per_page']['minimum'] = -1; |
However, this value doesn't actually do what it says - it really means some arbitrarily high limit instead of unbounded:
Lines 532 to 536 in fb804e2
| // Avoid triggering 'rest_post_invalid_page_number' error | |
| // which will need to be addressed in https://core.trac.wordpress.org/ticket/43998. | |
| if ( -1 === $prepared_args['posts_per_page'] ) { | |
| $prepared_args['posts_per_page'] = 100000; | |
| } |
This is a bit unclear and requires more complicated code than necessary, as well as a core patch referenced on the initial PR.
Allowing unbounded (or arbitrarily high) requests is also basically guaranteed to break for some sites due to memory or execution time constraints. This is a pretty opaque failure mode which doesn't leave the average user much chance of recovery.
I think a better trade-off would be to increase the limit for users who can edit_posts to a larger, but still plausible value, such as 1000 or 2000. This value should also be filterable by hosting providers. This way a site with a very large number of items would still work, but the cut-off for when items stop appearing in the UI would be much higher. This will lead to simpler API code with more predictable failure modes.
Ideally a site with this high number of posts would also be tested so that the performance characteristics are roughly understood.
Activity
tofumatt commentedon May 11, 2018
If we went this route (makes sense to me), it would be useful for the JS to log responses with the maximum number of results.
console.warn("Maximum API response size encountered: [Link to GitHub issue where we want to paginate them]")or something.chrisvanpatten commentedon May 11, 2018
I may be wrong, but isn't this the case for the current core approach as well? As far as I can tell, the select rendered by
wp_dropdown_pagesdoesn't have any enforced limits.Is there a difference in memory usage between what gets rendered server-side vs. how it would be done with an API call and rendered through React?
danielbachhuber commentedon May 11, 2018
Sorry for the confusion. To clarify, the arbitrarily high limit was a workaround to a current bug in WordPress core that will need to be addressed in the ticket. The end goal is to pass
per_page=-1all of the way through into the controller.If you were to comment out the workaround and run
test_get_pages_unbounded_per_pagelocally, you'd see:Ultimately, the problem is
WP_REST_Posts_Controllerisn't configured to properly handleposts_per_page=-1right now because of:https://github.com/WordPress/WordPress/blob/2f792d442bf771a3aade170cc9cae459f024c57b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L330-L334
I've updated the Trac ticket accordingly: https://core.trac.wordpress.org/ticket/43998#comment:2
Can you explain what the user experience for this might be, particularly as it relates to issues like #6487?
Correct, and neither does
wp_dropdown_users. See history in https://core.trac.wordpress.org/ticket/19867For a summary of the path core went down, see #5921 (comment)
tofumatt commentedon May 11, 2018
My suggestion from earlier: a number high enough (20,000 or whatever) will probably just crash most servers anyway. Returning the first 20,000 results and outputting a console warning might be better than 500ing.
nylen commentedon May 12, 2018
I would expect the REST API to perform significantly worse than
wp_dropdown_pagesin terms of both memory usage and execution time.Right, this is the main part I disagree with. I think it is better to always set a reasonable limit to the number of results the REST API can return.
I suspect that 20,000 is way too high for most servers.
adamsilverstein commentedon May 14, 2018
I second this concern (and previously raised on #6657). We should not enable unbounded requests - even for authenticated users.
danielbachhuber commentedon May 14, 2018
@nylen @adamsilverstein To move the conversation forward, can you suggest an alternative implementation with design- and accessibility-approved UX?
adamsilverstein commentedon May 14, 2018
Great question. I still think my original concept of a select that also performs an ajax search both the best UX and the least resource intensive design:
Unfortunately, my original implementation in #5921 was with
react-selectwhich @afercia pointed out was not accessible. @afercia have you seen this selector, the author is at least very active and motivated to improve accessibility: https://github.com/alsoscotland/react-super-select - I still think a selector is the best UX here.I've explored leveraging our autocompletion code here, this would work similar to the current tags selector (except you can't add authors, and you can only select one)... The goal is for it to would look something like this:
Unless we can find an accessible select component we like, this seems like a reasonable second choice.
nylen commentedon May 14, 2018
Building a UI that uses the existing pagination features of the API would be ideal.
I realize this is difficult, and this is not the right issue to debate how this is accomplished. So my proposal here is not related to UI implementation at all, instead it is just a robustness improvement to the API code merged in #6657:
chriscct7 commentedon May 18, 2018
There was a time when WP Core was working with and auditing Select2 for compatibility, perhaps that could be used as an alternative (not sure how far along that project got before it seemed to stall out)?
In any case, I further agree, a limit, likely not more than 5k if we want to look at the average server of an average WP user should be used. The average WordPress site has resources that are often significantly less than an avg WP dev's localhost resources.
An ajax based approach has the ability to scale, meet accessibility requirements, provide for a significantly better user experience, and also meet the design of Gutenberg.
danielbachhuber commentedon May 18, 2018
See #5921 (comment), which I mentioned earlier.
Can you share your data and methodology for coming to this amount?
Again, all of the prior research has not identified an AJAX-based approach that meets accessibility requirements. If you have a specific proposal to share, please do.
danielbachhuber commentedon May 19, 2018
Also, as I mentioned in Post Status Slack, this was a Hard decision, not an Easy one. Easy decisions make sense to everyone. Hard decisions involve trade-offs, and require a great deal of background information to understand fully.
#4622 sat for months before it was fixed with #6627. It was not a rushed decision, but rather weeks of discussion and consideration leveraging years of domain experience. The decision we came to is documented in #6180 (comment)
If someone wants to put in the effort towards producing an accessible and acceptable UI alternative, we welcome your participation. Keep in mind that it will be multiple weeks of research, documentation, and planning — not a quick pull request.
And, if you have time to spend on other Hard problems, we have an entire Back Compat milestone to work through.
31 remaining items
danielbachhuber commentedon Oct 18, 2018
Just so it's noted, this is a blocker for 5.0 beta1. Because https://core.trac.wordpress.org/ticket/43998 was closed, this issue needs to be resolved prior to beta1. Without a solution to this issue, Gutenberg the editor will query with
?per_page=-1against the existing core endpoints and get REST API failures.kadamwhite commentedon Oct 18, 2018
Notes from @joehoyle on this paginated approach:
This is good feedback, and this approach raises a few questions:
In the core REST-API channel I've proposed upping the
per_pagecap to 500 or 1000 to more efficiently handle the majority of sites; I don't know we are going to come up with a perfect solution to this in time for 5.0. I feel the long-term correct path forward continues to be working to implement an accessible typeahead search dropdown.danielbachhuber commentedon Oct 18, 2018
@kadamwhite Our options at this point are:
per_page=-1in the REST API; leave the current Gutenberg implementation as-is.per_page=100.Arbitrarily capping the number of results is not an option.
Like I've mentioned several times before, an accessible typeahead search dropdown doesn't solve for all five UI elements that are impacted by this:
kadamwhite commentedon Oct 18, 2018
Arbitrarily capping the results is not an option, as in raising the cap to 500? May I ask why? It feels like that would be a nicely complementary modification to any other changes we make here.
danielbachhuber commentedon Oct 18, 2018
Just to clarify, if we raise the cap to 500, would I be able to access my 501st item?
If yes, then we're talking about option 2. If no, then it's not a viable option.
We'd need to evaluate performance / memory consumption. My gut says 200 would be fine, 350 might be ok, and 500 is where we get to a magically bad number for
WP_Query: https://github.com/WordPress/WordPress/blob/47d32decd6668b3ccf100acc8a40aa0a9fc35003/wp-includes/class-wp-query.php#L2916kadamwhite commentedon Oct 18, 2018
I was specifically proposing upping the limit in conjunction with paginating over each page response to assemble the complete set. The goal would be to balance the cost of bootstrapping WordPress with the cost of requesting too many items; so for example if we cap at 350, a request for 800 items will generate three requests to the server.
danielbachhuber commentedon Oct 18, 2018
Ok. I'd be fine with 350 unless @pento has opinions otherwise.
danielbachhuber commentedon Oct 18, 2018
To be clear though, we should land #10762 first and foremost. We can more easily raise the limit after beta1.
joehoyle commentedon Oct 18, 2018
For ideal implementations of these, do you think that there is ever a need to load all content? I'm not sure if it's widely accepted that it's pretty much always a bad idea to have to load all of
xwherexcan be of infinite length. I know it is a lot of work to create such UI, I'm not saying that has to be done, but there does seem to be a undercurrent here of just hoping we can load all data, when I think that's just a fundamental broken approach.danielbachhuber commentedon Oct 18, 2018
We're not blue-sky thinking right now. We're a day away from 5.0 beta1, this issue is blocking it, and we're producing the most practical solution possible given constraints and circumstances.
nylen commentedon Oct 19, 2018
FYI: I am unsubscribing from this issue because despite repeating myself multiple times and asking follow-up questions, there was one single comment from a Gutenberg team member where the original goal of the issue was understood, and the rest are either unrelated to what I was proposing, worse solutions, or orders of magnitude more difficult.
This was my goal from the start with this issue. If you'd like to discuss what this could look like, pick a Slack instance and ping me on it.
kadamwhite commentedon Oct 21, 2018
I have opened #10845 implementing paging handling directly within
apiFetchinstead of using Middleware. After burning a number of hours working on the prior PR, I never found a clean solution for ensuring the error handling is correctly applied in all cases when using middleware. The system was not designed assuming additional asynchronous requests would be generated from within a middleware method, and I ran into many different issues trying to work around that; applying this logic inside of apiFetch feels cleaner, requires much less duplication of logic, and is now my recommendation.danielbachhuber commentedon Oct 21, 2018
Fixed by #10762