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

no-store cache policy for admin pages #9010

Merged
merged 2 commits into from
Mar 16, 2018
Merged

no-store cache policy for admin pages #9010

merged 2 commits into from
Mar 16, 2018

Conversation

johnHackworth
Copy link
Contributor

@johnHackworth johnHackworth commented Mar 8, 2018

fixes #8723

Right now, if you navigate to WordPress.com from the Jetpack dashboard and then come back via back button, you are getting the previous state of the app because of browser's cache. This PR add no-store cache policy to jetpack admin pages so you don't get a cached version of the dashboard when you click 'back' from wpcom

How to test

  1. Activate Jetpack (or if it's already active and you don't want to deactivate, run wp option update show_welcome_for_new_plan true with wp-cli)
  2. You should get the Warm Welcome modal (if you used wp-cli, you may need to navigate to the jetpack dashboard or reload the page)
  3. Click on the "view more stats in WordPress.com" button.
  4. Once you get to wp.com, click on the browser's back button
  5. You should be taken back to your jetpack dashboard, and the Warm Welcome modal shouldn't show

Changelog entry

Admin Page: Fixed an issue by which clicking the back button on a page visited after the Admin Page would result in the Admin Page being rendered with cached data.

… cached version of the dashboard when you click 'back' from wpcom
@johnHackworth johnHackworth added [Type] Bug When a feature is broken and / or not performing as intended [Focus] NUX [Focus] FixTheFlows [Feature] Warm Welcome labels Mar 8, 2018
@johnHackworth johnHackworth self-assigned this Mar 8, 2018
@johnHackworth johnHackworth requested a review from a team as a code owner March 8, 2018 08:13
Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

I was able to test this one properly 👍

Left some drive-by comments.

Also, can we verify that this won't have negative performance implications, or break something that relies on this caching being present?

@@ -1,5 +1,10 @@
<?php

function add_no_store_header( $headers ) {
$headers[ 'Cache-Control' ] .= ', no-store';
Copy link
Member

Choose a reason for hiding this comment

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

Our coding standards dictate that when referring to array items we should include a space around the index only if it is a variable (see https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#space-usage).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, 👍

@@ -1,5 +1,10 @@
<?php

function add_no_store_header( $headers ) {
Copy link
Member

Choose a reason for hiding this comment

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

It feels a little off to introduce a function in a file that is dedicated for containing a class. How about moving it as a method to this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, the problem here is that the class is an abstract class. Does php allows static methods in abstract classes? I'm under the impression that it doesn't, but I can be wrong

Copy link
Member

Choose a reason for hiding this comment

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

There is no reason for it to not be allowed - abstract classes can't be instantiated, but static methods are called as class members and don't need an instance for that. As long as they're not abstract, and they're not calling any of the abstract methods, you'll should be fine.

@@ -1,5 +1,10 @@
<?php

function add_no_store_header( $headers ) {
$headers[ 'Cache-Control' ] .= ', no-store';
return $headers;
Copy link
Member

Choose a reason for hiding this comment

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

There is an extra space here between the return and the variable.

@@ -41,6 +46,10 @@ function __construct() {
self::$block_page_rendering_for_idc = (
Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' )
);
if( $_GET[ 'page' ] == 'jetpack' ) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use strict check here?

Also spaces inside square brackets are unnecessary.

Optionally: Use a Yoda condition?

Also, generally, I feel this isn't the right place to place this code. If it's for all of Jetpack's admin, I'd expect finding it in Jetpack_Admin.

Finally: I'm wondering if this is the right place to hook this one. I realize that load- actions are a bit late for this, but perhaps if we create a method that would be hooked onwp_loaded?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm wondering if this is the right place to hook this one. I realize that load- actions are a bit late for this, but perhaps if we create a method that would be hooked onwp_loaded

Hmm, but I don't know if I understand... why don't use nocache_headers if it's specific about what we want to modify?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe use strict check here?
Also spaces inside square brackets are unnecessary.
Optionally: Use a Yoda condition?

Ok to all the things!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, generally, I feel this isn't the right place to place this code. If it's for all of Jetpack's admin, I'd expect finding it in Jetpack_Admin.

Hmm, ok!

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, but I don't know if I understand... why don't use nocache_headers if it's specific about what we want to modify?

Ah, sorry for misexplaining. I was suggesting that we use the nocache_headers, but altering when we actually hook the function to the filter - in other words, when precisely to call the add_filter().

@@ -41,6 +46,10 @@ function __construct() {
self::$block_page_rendering_for_idc = (
Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' )
);
if( $_GET[ 'page' ] == 'jetpack' ) {
add_filter( 'nocache_headers', 'add_no_store_header' );
nocache_headers();
Copy link
Member

Choose a reason for hiding this comment

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

Is it necessary to call nocache_headers() again? It will be called early in wp-admin/admin.php anyway, so why call it again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because I'm paranoid and I don't trust that we are not running some customized install that actually doesn't add the cache headers by default... AKA "just in case"

(you are right, it's probably not needed... but it also doesn't do any harm to be sure :D )

Copy link
Member

Choose a reason for hiding this comment

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

Oh well, if anyone modifies this in the WP core, I'd say they should be responsible for it. Other weird things would happen, and sky would fall, especially if disabled for admin-ajax.php for example.

@@ -1,5 +1,10 @@
<?php

function add_no_store_header( $headers ) {
$headers[ 'Cache-Control' ] .= ', no-store';
Copy link
Member

Choose a reason for hiding this comment

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

Also, note that other plugins and themes can also modify this, so we might want to make this a bit safer. For example, if the cache control was modified to be max-age=86400 by another plugin or theme, we probably wouldn't want to add a no-store to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well, if we were going to handle all the possible combinations this would get a little bit complicated. Also, we are changing the cache policy just for jetpack, so it shouldn't affect any other plugin (unless there's a name collusion)

Copy link
Member

Choose a reason for hiding this comment

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

well, if we were going to handle all the possible combinations this would get a little bit complicated.

Fair enough.

Also, we are changing the cache policy just for jetpack, so it shouldn't affect any other plugin

Actually, this isn't 100% true - we're changing the cache policy for when we're visiting a Jetpack admin page, but then any link that is visible there (in the masterbar or in the admin sidebar menu) will be affected too. Probably I'm digging too deep about this, but just wanted to make sure it's well thought of.

@@ -41,6 +46,10 @@ function __construct() {
self::$block_page_rendering_for_idc = (
Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' )
);
if( $_GET[ 'page' ] == 'jetpack' ) {
add_filter( 'nocache_headers', 'add_no_store_header' );
Copy link
Member

Choose a reason for hiding this comment

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

Since other plugins and themes can also modify this, we might want to add it with a larger $priority, so it would be called a bit later in the filter application call queue.

@oskosk oskosk added this to the 6.0 milestone Mar 9, 2018
@johnHackworth johnHackworth requested a review from tyxla March 13, 2018 10:12
Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

This LGTM 👍

Maybe a good idea to verify with @Automattic/jetpack folks that this:

  • Is the right place to add this filter.
  • Isn't introducing any unexpected performance issues.

@oskosk
Copy link
Contributor

oskosk commented Mar 16, 2018

This should fix a lot of stuff. Thank you!

Copy link
Contributor

@oskosk oskosk left a comment

Choose a reason for hiding this comment

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

LGTM

@oskosk oskosk added the [Status] Ready to Merge Go ahead, you can push that green button! label Mar 16, 2018
@oskosk
Copy link
Contributor

oskosk commented Mar 16, 2018

cc @zinigor for a second check of the place we're hooking from.

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.

First I wanted to suggest that we split the existing Cache-Control headers by comma, add no-store then join, but then I tested what the browser does if we just send , no-store, and it looks like headers don't explode because of this.
So generally I'd say this looks safe enough, thank you!

@zinigor zinigor merged commit 8f1e04b into master Mar 16, 2018
@ghost ghost removed the [Status] Ready to Merge Go ahead, you can push that green button! label Mar 16, 2018
@zinigor zinigor deleted the fix/double-ww-modal branch March 16, 2018 20:07
oskosk added a commit that referenced this pull request Mar 23, 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Warm Welcome [Focus] FixTheFlows [Focus] NUX [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warm Welcome: appears twice and causes error
5 participants