Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite Scroll: Prevent first page duplication, and fix main query detection #9112

Merged
merged 3 commits into from
Mar 27, 2018

Conversation

mikejolley
Copy link
Contributor

I believe this fixes #1135 but we have slightly different reproduction steps in that we're simply running this on a custom post type archive. I found issues in the way pagination works which I'll do my best to explain below.

There are 2 main issues this PR aims to resolve;

  1. Page "1" being loaded when you're already viewing page 1.
  2. Queries from other plugins failing because the Infinite scroll query is not the "main query".

Changes proposed in this Pull Request:

Globals

This PR adjusts the order in which the globals wp_the_query and wp_query are set so that is_main_query is accurate when ran on the pre_get_posts hook.

Looking at the code where it set wp_the_query and wp_query it looks like the intention here was to be the 'main query' but because the query is RAN before these globals are set, any functions hooking into pre_get_posts will not see correct results.

Before:

  • Query is ran in the constructor of WP_Query
  • pre_get_posts fires during the query.
  • At this point wp_the_query and wp_query have the original values. This means is_main_query will return false.

After:

  • New WP_Query is constructed.
  • wp_the_query and wp_query are set to this new WP_Query object.
  • query is ran.
  • pre_get_posts fires during the query.
  • At this point wp_the_query and wp_query contain the current query object, so is_main_query will return true.

This fixes a conflict with WooCommerce which only adjusts the main query.

Pagination JavaScript

When viewing the first page and scrolling, IS loads page 1 again. The adjustment in the script I made makes it load page 1 plus the current page (offset).

So example, WooCommerce shop page at local.wordpress.test/shop/ is showing page 1 already. When you scroll it loads page 1 + offset 1 = page 2.

If you start at local.wordpress.test/shop/page/2 it loads page 1 + offset 2 + page 3.

Both the results are correct, and the address bar is updated accordingly.

I'm not sure how or if it actually worked before :)

Testing instructions:

So these are the testing instructions for our WooCommerce use case. @tiagonoronha May comment for general blog testing instructions but basically.

  • Checkout this branch
  • Checkout this branch of WooCommerce Infinite scroll compatibility woocommerce/woocommerce#19477
  • In customizer > woocommerce > product catalog, set orderby to title, and show 3 products per row and 1 row per page.
  • View the shop page.
  • Scroll/load more, and you'll see another row of products added until complete.

Before the patch you'd see random number of products, in random order, with missing items in the grid. Page 1 would also be duplicated.

Proposed changelog entry for your changes:

  • Fixes is_main_query detection within infinite scroll's WP_Query object.
  • Fixes first page pagination to prevent duplicate pages being returned.

Previously, if you ran `is_main_query`, it would return false during
the `pre_get_posts` hook because `is_main_query` compares the current
query to the global.

By setting to global first, these checks can be done correctly.
@mikejolley mikejolley requested a review from a team as a code owner March 21, 2018 18:15
@mikejolley mikejolley changed the title Fix/infinite scroll pagination results 1135 Page 1 duplication and main query support in Infinite Scroll module Mar 21, 2018
@mikejolley mikejolley changed the title Page 1 duplication and main query support in Infinite Scroll module Infinite Scroll: Prevent first page duplication, and fix main query detection Mar 22, 2018
@mikejolley mikejolley self-assigned this Mar 22, 2018
@mikejolley mikejolley added [Feature] Infinite Scroll [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. [Status] Needs Testing We need to add this change to the testing call for this month's release labels Mar 22, 2018
@jeherve jeherve added this to the 6.0 milestone Mar 22, 2018
@jeherve jeherve added [Type] Bug When a feature is broken and / or not performing as intended [Pri] Normal labels Mar 22, 2018
@danieldudzic
Copy link
Contributor

I have tested this with Storefront (and few other themes), but I'm still experiencing an issue:

@tiagonoronha
Copy link

@danieldudzic Thanks for testing.

Please test with #9120 instead. Also make sure that none of the themes has any custom code to alter the IS query. You can test with woocommerce/storefront#788.

@mikejolley
Copy link
Contributor Author

^ +1 This patch alone won't make WC compatible.

Copy link
Member

@zinigor zinigor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a small nitpick regarding the code, plus I seem to have problems when testing this PR. If you get a site with a big number of posts, you can observe for yourself:

  • open a category view;
  • scroll towards the second page;
  • remember what posts you see;
  • open the same link but in a new tab so that it has to load from the server;
  • see that you don't get the same posts that were loaded in IS.

I can't put my finger on it, but I think that something is wrong with the offset, as though some posts are getting skipped because of posts per page number mismatch.

I'm happy to let you test on my site with tons of posts, just let me know if you need the creds. Here's a link to the site with IS and lots of posts with categories: https://dev71.lousy.site/category/cats/

$GLOBALS['wp_the_query'] = $GLOBALS['wp_query'] = new WP_Query( $query_args );
$GLOBALS['wp_the_query'] = $GLOBALS['wp_query'] = $infinite_scroll_query = new WP_Query();

$infinite_scroll_query->query( $query_args );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the PRs description, there's a pretty big change implied in this simple diff here. Do you think we should add a comment so that no one would have a desire to roll the thing back and avoid using an additional variable? :)

Or maybe we can even get rid of the variable, and just call the query method on one of the globals after they are set?

@zinigor zinigor added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Mar 27, 2018
@zinigor
Copy link
Member

zinigor commented Mar 27, 2018

After testing together with #9120 I can say that the problem is gone, well done, thank you!

@dereksmart dereksmart merged commit 2f83588 into master Mar 27, 2018
@dereksmart dereksmart deleted the fix/infinite-scroll-pagination-results-1135 branch March 27, 2018 20:37
@ghost ghost removed the [Status] Ready to Merge Go ahead, you can push that green button! label Mar 27, 2018
dereksmart added a commit that referenced this pull request Mar 27, 2018
dereksmart pushed a commit that referenced this pull request Mar 27, 2018
* Changelog 6.0: create base for changelog.

* Add #8938 to changelog

* Add #8962 to changelog

* Add #8974 to changelog

* Add #8975 to changelog

* Add #8978 to changelog

* Add #8867 to changelog

* Add #8937 to changelog

* Add #8961 to changelog

* Add #8855 to changelog

* Add #8944 to changelog

* Add #8973 to changelog

* Add #8977 to changelog

* Add #8979 to changelog

* Add #8980 to changelog

* Add #8982 to changelog

* Add #8983 to changelog

* Add #8984 to changelog

* Add #8986 to changelog

* Add #9005 to changelog

* Add #9010 to changelog

* Add #9012 to changelog

* Add #9021 to changelog

* Add #9022 to changelog

* Add #9056 to changelog

* Add #9061 to changelog

* Add #9079 to changelog

* Add #9080 to changelog

* Add #9088 to changelog

* Add #9096 to changelog

* Add #9097 to changelog

* Add #9100 to changelog

* Add #9107 to changelog

* Add #8969 to changelog

* Add #8993 to changelog

* Add #9003 to changelog

* Add #9031 to changelog

* Add #8945 to changelog

* Add #9052 to changelog

* Add #9058 to changelog

* Add #9066 to changelog

* Add #9076 to changelog

* Add #9053 to changelog

* Add #9108 to changelog

* Add #9135 to changelog

* Add #9148 to changelog

* Add #9125 to changelog

* Add #9137 to changelog

* Added testing instructions for 6.0.

* Added IS testing instructions, huge props to @tiagonoronha.

* Added #8498 to changelog.

* Added #8954 to changelog.

* Added #8985 to changelog.

* add #9027

* add #9112 to changelog

* add #9136 to changelog

* add #9102 to changelog

* add #9093 to changelog

* add #9062 to changelog

* add #9172 to changelog
@jeherve jeherve removed the [Status] Needs Testing We need to add this change to the testing call for this month's release label Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Different orderby causes double load in Infinite Scroll
7 participants