Skip to content

Conversation

@roykho
Copy link
Contributor

@roykho roykho commented May 14, 2021

All Submissions:

Changes proposed in this Pull Request:

So apparently different browsers handles bfcache (back/forward cache) differently. Safari and Firefox seems to not cache the header directive by default while Chrome does.

This PR assumes that it is best to not cache the cart and will reflect that in the code. Note that is_cart() returns true for checkout page as well so this implementation will inadvertently affect the checkout page.

If we could decide for certain we don't need to cache cart/checkout pages at all, then we could essentially remove the "Chrome" check from the agent. We could also alternatively introduce a filter to disable the cache if they so choose.

Closes #29484

How to test the changes in this Pull Request:

  1. Using Chrome, add several items to the cart.
  2. Go to the cart page and remove/delete one of the items and go to checkout page.
  3. Click the back button on your Chrome browser.
  4. Ensure the item you removed/deleted is still gone.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?

Changelog entry

Prevent caching of cart/checkout page when using Chrome browser.

@roykho roykho requested review from a team and barryhughes and removed request for a team May 14, 2021 21:01
* @param bool $enable_nocache_headers Flag indicating whether to add nocache headers. Default: false.
*/
if ( false !== strpos( $agent, 'googleweblight' ) || apply_filters( 'woocommerce_enable_nocache_headers', false ) ) {
if ( apply_filters( 'woocommerce_enable_nocache_headers', false ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm kind of curious why the code (before your changes) wasn't framed in this sort of way:

if ( false !== strpos( $agent, 'googleweblight' ) ) {
	$set_cache = true;
}

/**
 * Allow plugins to enable nocache headers. Enabled for Google weblight.
 *
 * @param bool $enable_nocache_headers Flag indicating whether to add nocache headers. Default: false.
 */
$set_cache = (bool) apply_filters( 'woocommerce_enable_nocache_headers', $set_cache );

If only because the way the filter's docblock reads to me is that $set_cache will already be true if the request was made by Google Weblight...but really what's happening in the current code is, we force it to true if the request was made by Google Weblight—regardless of what comes through the filter (and I'm just noting this to give context for my next few points).

We could also alternatively introduce a filter to disable the cache if they so choose.

Couldn't we do this now? If for back compat reasons we don't want to change the way the logic works for Weblight, we could still do something like I outlined above but focused on Chrome and the cart page. That way it would be easy for merchants to unwind this change if necessary, at no extra expense in terms of adding or triggering new hooks.

I'm also wondering if we should update the docblock, because we're adding this new exception for Chrome but we're not documenting it there (whereas it's documented for Weblight).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah not sure why it was like that from before but I just reorganized it to a way which was more readable to me. Before it was kind of confusing not to mention the added conditional for Chrome and cart.

Comment on lines 67 to 68
// Note that is_cart() will return true even for checkout page.
if ( false !== strpos( $agent, 'Chrome' ) && is_cart() ) {
Copy link
Member

Choose a reason for hiding this comment

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

So just to be clear, do we want this to take effect on the checkout page?

  • For me, is_cart() is only triggered on the cart page and this condition is not met on the checkout page. I'm not sure why we see different things there.
  • If we want this to take effect on checkout pages, should we be explicit about it and use the is_checkout() function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because we already have a bug in the logic somewhere. If you look at this https://github.com/woocommerce/woocommerce/blob/5.3.0/includes/wc-conditional-functions.php#L98 you will see it also checks for WOOCOMMERCE_CART constant and if that is true, is_cart() will return true. Coincidentally that constant is set in checkout page as well. Hence this issue. But yeah we could explicitly also add is_checkout() as well.

Copy link
Member

Choose a reason for hiding this comment

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

Yep, I think that makes sense—again for me, locally, is_cart() does not return true on the checkout page. So it would seem like that's a bug, as you say, and possibly a bug that needs specific conditions to be triggered (so either way, shouldn't be relied on)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I am not too sure myself. It might of been something wonky with cache but the way I found this is when I ran xdebug and stepped through. I've since removed that comment. So another round please @barryhughes

@barryhughes barryhughes merged commit 31da945 into trunk May 17, 2021
@barryhughes barryhughes deleted the fix/29484 branch May 17, 2021 20:34
@woocommercebot woocommercebot added release: add changelog Mark all PRs that have not had their changelog entries added. [auto] release: add testing instructions PRs that have not had testing instructions added to the wiki. [auto] labels May 17, 2021
@github-actions github-actions bot added this to the 5.5.0 milestone May 17, 2021
@vedanshujain vedanshujain added the release: highlight Issues that have a high user impact and need to be discussed/paid attention to. label Jun 18, 2021
@zhongruige zhongruige removed the release: add testing instructions PRs that have not had testing instructions added to the wiki. [auto] label Jun 18, 2021
@vedanshujain vedanshujain removed the release: add changelog Mark all PRs that have not had their changelog entries added. [auto] label Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: highlight Issues that have a high user impact and need to be discussed/paid attention to.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cart not updated if I click back button in browser

5 participants