Core Data: Fix per_page query logic for when offset is present in the query#76613
Core Data: Fix per_page query logic for when offset is present in the query#76613andrewserong merged 3 commits intotrunkfrom
Conversation
|
Size Change: +43 B (0%) Total Size: 8.75 MB
ℹ️ View Unchanged
|
|
Flaky tests detected in 5bf3804. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23227274347
|
There was a problem hiding this comment.
I used 5 for the PER_PAGE value since my media library count was 84. Took me a while to get there 😄 which makes me think if that path deserves a test just for completeness. E.g., when offset >= totalItems (e.g. 84 items, per_page=7 → offset=84, expected=0)
I could reproduce the bug in trunk, and can confirm this PR fixes them.
I tested the steps in #76422
Thanks for the quick fix 🚀
| // count remaining after the offset. The number of items | ||
| // available for this query is (totalItems - offset), so a | ||
| // partial last page is expected and valid. | ||
| const effectiveTotal = Number.isFinite( queryOffset ) |
There was a problem hiding this comment.
No action required, just a question: Could this just be queryOffset !== undefined?
Just wondering if might cause bad values in getQueryParts to silently fail.
There was a problem hiding this comment.
Oh, I think I get you — we should do the real validation of the number in getQueryParts rather than in the consumer (this function). I'll push a small update, I think that'll clean things up 👍
There was a problem hiding this comment.
Not a blocker! I was just curious about the Number.isFinite check. 👍🏻
There was a problem hiding this comment.
I was just curious about the Number.isFinite check. 👍🏻
It's just a (slightly unnecessary) guard against invalid values / NaN. In practice it shouldn't ever be reached because the endpoint should throw a 400 when given invalid values, but it felt neater to do the check once I started adding tests 😄
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Thanks for the quick review @ramonjd! 🙇 I've added an extra test for that case where the offset matches the totalItems, and I've moved where the number validation happens (and added a tiny test). I think that makes things a little tidier, but let me know if it's not what you had in mind. |
|
LGTM - nice one! |
|
Good catch and thanks, @andrewserong! I totally forgot about the Looking folks for feedback, @jsnajdr, @youknowriad, @ntsekouras. P.S. I think infinite scroll logic should use |
|
Thanks George!
Good question. As far as I could tell, to do that we'd need to update more of the logic to handle Could be a good code quality / performance thing to follow-up on at some point (I likely won't get a chance to look at in in the next couple of weeks as I'll be travelling).
Worth taking a look at the latest infinite scroll PR if you haven't already (it is a big one, though!): #74378 |
|
Agree, that's something we can improve in follow-up. I can give it a try if I have time this week. One last question: Have you tried using I think that approach would require fewer changes and would mostly be contained in |
Oh no, I didn't try that! I have to say, I went from hearing about the bug to trying to fix it fairly quickly, so didn't consider much how else we could go about it 🙂 |
|
Not a problem. I'll do more testing and see if it works. Update: Just finished testing my theory, and it doesn't work, mostly because the server also ignores the |
|
We should definitely follow up with an improvement where |
Trying to follow up on this in #76808. |
What?
Follows:
Update the calculation of total items in the
getQueriedItemsUncachedfunction to factor inoffset.Why?
While working on infinite scroll @tellthemachines located a bug where the last page of results (retrieved via
offset) would never appear. It turns out that in the logic we added in #76422 we didn't factor in an offset when looking at the number of items available and the total items — this would result inoffsetbased queries returningnullbut never actually resolving because for the last page of resultsitemIds.lengthwould always be less thantotalItemsas the offset means that the actual number of items will be that amount less thantotalItems.How?
When calculating the total items, treat it as an effective total items, substracting
offsetfrom the value (if present).And in
getQueryPartsaddparts.offsetto the parts we're extracting. Offset is still part of the stableKey, but this allows us to accessoffsetingetQueriedItemsUncachedin the same way as we access the other parts of the query.Testing Instructions
I've added some tests to hopefully cover the desired behaviour. But you can manually test via the following in your browser console to test the last (effective) page of results when using an
offset:To be thorough, double check that the steps in #76422 still work as expected.
Use of AI Tools
I used Claude Code to generate tests, verify logic, add code comments, and to perform four separate rounds of reviews to try to catch any problems from the changes added here. I also used Claude to generate the above testing snippet. This PR description is otherwise written by me, and I tested and verified the code is working in my local env, but any further testing from reviewers is greatly appreciated! 😄