site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Refresh button for PageSpeed card

Open amirsadeghian opened this issue 7 years ago β€’ 12 comments

I believe the plugin is caching the page speed results. It would be nice to add a button to refresh the cache and re-run the page speed audit.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • In the Page Speed and Experience module, for the Lab data tab, add a "Run test again" link in the bottom left corner
  • Clicking "Run test again" clears the currently cached results and re-runs the test
  • "View details at PageSpeed Insights" is moved to the bottom right corner
  • Since Field data accumulates over time as users visit the page, and there won't be significant differences, so this functionality is only necessary for the Lab data tab.

Implementation Brief

  • Add a button in the PageSpeed Insights widget that invalidates the selector's resolution and the cached data for API.get.
  • See #2000 for the basis of the approach recommended.

QA Brief

  • Navigate to a single page's Search Console page or the site's Site Kit Dashboard.
  • Scroll to PageSpeed Insights
  • Wait until data is loaded
  • Refresh the page
  • Scroll to PageSpeed Insights and notice data is cached. Click the "Refresh Data" button and watch it update.

Changelog entry

  • Show a button to refresh PageSpeed Insights report data in the widget.

amirsadeghian avatar Jun 30 '19 07:06 amirsadeghian

@amirsadeghian Thanks for submitting this request. PSI data is cached in browser sessionStorage, so if you open in new tab you will get the new data. Although I agree with you, having a button to re-fetch would be better.

ivankristianto avatar Jul 01 '19 03:07 ivankristianto

@ivankristianto , I am interested to contribute. Can I fork the project and add this enhancement and submit a pull request? Thank you

amirsadeghian avatar Jul 04 '19 04:07 amirsadeghian

@amirsadeghian yes feel free to forked and open a PR. Also please read this contributing doc priorly: https://github.com/google/site-kit-wp/blob/develop/CONTRIBUTING.md

Note: Instead branch of and submit PR to master, branch of and submit the PR to develop. We will update that contributing doc to match later.

ivankristianto avatar Jul 04 '19 04:07 ivankristianto

@tofumatt While I generally agree with your point on not "surfacing" cache to the user, this is more specific to PSI here I'd argue: Since that API (to be precise, only the "Lab Data" part) essentially provides realtime data, it's important to allow users to get super up-to-date data, which is not as relevant for other modules. For example, if a user makes a massive technical change to try to improve the site's performance, they should be able to see how that surfaces in the PSI widget right away. That doesn't apply to the other modules though which take time to gather metrics.

While we could also not cache PSI data at all or for a much shorter time, this would result in many unnecessary API requests. However, if the user wants to see fresh data, it should be intuitive to do so. Even 5 minutes could be too long if you want to see changes reflected right away. The fact that we do caching is still mostly internal here - I think providing a refresh button gives the most intuitive user experience: The user will see the button and know that they can retrieve 100% fresh data from there - which isn't relevant all the time, but only when they want to e.g. see how their technical changes affected the PSI lab metrics.

felixarntz avatar Sep 08 '20 22:09 felixarntz

Fair enough! I'll amend the IB to include the actual button, though it'll likely require passing that through from the selector all the way to the API request. πŸ˜„

tofumatt avatar Sep 09 '20 16:09 tofumatt

@tofumatt

Add a new option to API.get and siteKitRequest that allows a cached value to be ignored and the request made, but saved in the cache.

Rather than overloading these functions with a very specific parameter, I think we could rely on the existing functions to clear the respective API cache in the callback handler for the refresh button.

felixarntz avatar Sep 10 '20 00:09 felixarntz

Rather than overloading these functions with a very specific parameter, I think we could rely on the existing functions to clear the respective API cache in the callback handler for the refresh button.

I considered that, but the only way to do that is to extract the cache key that the API.get call would make and then invalidate it, which requires the use of the private API createCacheKey: https://github.com/google/site-kit-wp/blob/2c4ce22c95c0ee00abdbd1117a48f9ddb507af42/assets/js/googlesitekit/api/index.js#L51-L54

Of course, we can do it, but I think having a param that updates the cache but doesn't use it is intuitive and a reasonable use-case to build in, whereas manually clearing the cache requires an understanding of how cache keys are handled "under-the-hood"β€”it isn't behaviour we expose or require even the internal team to concern themselves with right now.

Bonus: by not clearing the cache, if the request fails or something we can still use the old-but-valid data on subsequent requests (after a page refresh, for instance).

I'd prefer the new param for that reason. Let me know if that isn't convincing though, and in that case we'll use createCacheKey to invalidate the existing cache instead.

tofumatt avatar Sep 10 '20 14:09 tofumatt

@tofumatt I don't think we would need to manually use something as low-level as createCacheKey, but we could use googlesitekit.api.invalidateCache, which exists explicitly for that purpose. It would clear all PSI report caches (for the current and other URLs), but I think that is perfectly acceptable here. It wouldn't affect the current view in an unexpected way and would just mean next time you go to the details view of another URL you'd get fresh data again, which is fine.

I think that would be a cleaner approach than the new parameter, we have that function for explicitly clearing API caches.

felixarntz avatar Sep 10 '20 16:09 felixarntz

Ah, fair enough. I wanted to be more targeted with the invalidation, but I suppose here that's a fair point πŸ‘πŸ»

IB updated! πŸ˜„

tofumatt avatar Sep 10 '20 17:09 tofumatt

IB βœ…

felixarntz avatar Sep 10 '20 17:09 felixarntz

Tested:

Installed, activated and setup latest SK release.

Navigated to a single page's Search Console page or the site's Site Kit Dashboard. Scrolled to PageSpeed Insights Waited until data is loaded Refreshed the page Clicked Run test again

image

Confirmed on mobile that this functions properly. Checked window sizes and other sites.

Passed QA βœ…

cole10up avatar Sep 24 '20 21:09 cole10up

Tested on a brand new and on an existing site. Everything working as intended in ACs. LGTM

marrrmarrr avatar Sep 25 '20 14:09 marrrmarrr