#55677 closed enhancement (fixed)
Improve Media REST API endpoint
Reported by: | furi3r | Owned by: | spacedmonkey |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | has-patch has-unit-tests commit add-to-field-guide |
Focuses: | performance | Cc: |
Description
Duplicated queries when querying media_types without files.
await wp.apiFetch({ path: 'wp/v2/media?_envelope&media_type=text'});
Duplicated queries when querying non-existing mime_types.
await wp.apiFetch({ path: 'wp/v2/media?_envelope&mime_type=image/webp'});
Probably related.
Attachments (1)
Change History (15)
#1
@
2 years ago
Thanks for the details @furi3r . I've figured out the issue for this and should have a patch available soon!
This ticket was mentioned in PR #2753 on WordPress/wordpress-develop by gabriel-detassigny.
2 years ago
#2
- Keywords has-patch has-unit-tests added
## The issue
WP REST API has this logic to re-run the query without the page parameter if we didn't get any posts back from the main one. This is to handle the case where we requested an extra page of a collection that is empty. In that case, we want the API to return an empty collection, but still return the total number of items in that collection. Therefore that extra query gives that total back.
However, it currently doesn't check if we were on the first page already.
In that specific case, the second query would be the exact same as the first one and therefore wouldn't provide any additional value (the total of items would still be zero). This is why we sometimes ends up with a duplicated query.
By checking if we're already on the first page, we can avoid a duplicated query for empty collections.
Note that the trac ticket refers to the error happening on attachments endpoint, however any empty collection of posts will be affected (This is why some tests that were previously checking for two queries and had a "FIXME" comment, have been updated to one).
## How to test
- Query Monitor plugin should be enabled to detect any duplicate queries.
- Open the gutenberg editor
- Open the JS console
- Type
await wp.apiFetch({ path: 'wp/v2/media?_envelope&media_type=text'}
- The object returned should contain the number of duplicated queries. On trunc, there will be a duplicate which this patch will fix.
Trac ticket: https://core.trac.wordpress.org/ticket/55677
#3
@
2 years ago
I've added a patch to fix this.
Something worth noting : while this issue was reported about the attachments rest endpoint, this actually is a potential issue for any post type. It's probably more apparent on attachments because they're more likely to have a completely empty collection.
TimothyBJacobs commented on PR #2753:
2 years ago
#6
Can we also interrogate the X-WP-TotalPages
and X-WP-Total
headers in our tests here to have assertions that they still match our expected results.
gabriel-detassigny commented on PR #2753:
2 years ago
#7
Thanks @TimothyBJacobs . I've made some changes now:
- first test for an empty collection now also asserts these headers
- second test checks the appropriate _invalid page number_ error is returned (headers cannot be verify because they don't get set in this specific case, the error is returned right before the headers would be set)
TimothyBJacobs commented on PR #2753:
2 years ago
#8
Thanks @gabriel-detassigny, this looks good to me!
Duplicated queries when querying media_types witouth files.