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

Register Assets: cache bust without filemtime #29775

Merged

Conversation

hellofromtonya
Copy link
Contributor

@hellofromtonya hellofromtonya commented Mar 11, 2021

Description

#18227 reports times when outdated assets are being served when updating the plugin. filemtime is suggested as the root cause. Though I was unable to reproduce, it is conceivable that the filemtime could cause this problem. Why?

The asset version strategy in the registration uses filemtime to generate a unique version based on the asset file's modification time. The return of filemtime is cached in the realpath cache with an expiration defined by the realpath_cache_ttl option in php.ini.

This PR replaces filemtime with 2 different strategies:

  • For production, it uses GUTENBERG_VERSION as the asset version.
  • For development, it uses current time in seconds via time() as the asset version.

Note:
time() is not cached in the realpath cache.

Benefits:

  • Simplifies the code
  • Avoids an extra hit to the filesystem (teeny tiny performance boost though user will never see it as it's in microseconds)
  • Mitigates the realpath cache scenario

How has this been tested?

Validated new version of the stylesheet loads when updating the plugin.

Steps:

  1. Installed/activated version 9.9.3
  2. In stylesheet build/build-directory/style/css, changed :root{--wp-admin-theme-color:#00ba3e;
  3. Noted asset's version ...gutenberg/build/block-directory/style.css?ver=1615418702
  4. Updated to new version with this PR's changes

Results:
The updated stylesheet was served to the browser.

  • The color changed to #007cba and rendered
  • Version changed ..gutenberg/build/block-directory/style.css?ver=1615469843

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

hellofromtonya added 3 commits March 11, 2021 06:57
For production, uses GUTENBERG_VERSION.

For dev, uses microtime().
For production, uses GUTENBERG_VERSION.

For dev, uses microtime().
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Mar 11, 2021
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @hellofromtonya! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@gziolo gziolo added the Gutenberg Plugin Issues or PRs related to Gutenberg Plugin management related efforts label Mar 11, 2021
Copy link
Member

@mkaz mkaz left a comment

Choose a reason for hiding this comment

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

The change looks good, tested and works fine.

The one caveat might be for Gutenberg developers who do not set the WP_DEBUG constant, we probably will want to confirm that is mentioned in the docs somewhere. This can be a separate PR.

@hellofromtonya
Copy link
Contributor Author

The one caveat might be for Gutenberg developers who do not set the WP_DEBUG constant, we probably will want to confirm that is mentioned in the docs somewhere. This can be a separate PR.

What constant are Gutenberg devs more inclined to set, if any? The PR can check for either WP_DEBUG or ____.

@mkaz
Copy link
Member

mkaz commented Mar 11, 2021

What constant are Gutenberg devs more inclined to set, if any? The PR can check for either WP_DEBUG or ____.

I'm not sure, my guess is none on the PHP side. The expectation is more setting in at build using something like:

$ NODE_ENV=development npm run build

Here is the dev setup document:
https://github.com/WordPress/gutenberg/blob/trunk/docs/contributors/code/getting-started-with-code-contribution.md

@hellofromtonya
Copy link
Contributor Author

hellofromtonya commented Mar 11, 2021

That makes.

It's a reasonable expectation when working in npm run dev that dev versions will be served up. The WP_DEBUG constant is set when using wp-env https://github.com/WordPress/gutenberg/tree/trunk/packages/env#default-wp-config-values. In a separate PR we could add default set up for those not using wp-env.

@aristath
Copy link
Member

What constant are Gutenberg devs more inclined to set, if any? The PR can check for either WP_DEBUG or ____.

Usually I have SCRIPT_DEBUG enabled... People working on Gutenberg do mostly JS work, so frequently that is set to true in order to get better debug info in the browser console

@hellofromtonya
Copy link
Contributor Author

hellofromtonya commented Mar 12, 2021

Usually I have SCRIPT_DEBUG enabled... People working on Gutenberg do mostly JS work, so frequently that is set to true in order to get better debug info in the browser console

Thanks @aristath.

In looking at constants predefined when running wp-env, both are set to true:

WP_DEBUG: true,
SCRIPT_DEBUG: true,

SCRIPT_DEBUG is already being used as development flag in the same file.

Switching to use SCRIPT_DEBUG for consistency.

@gziolo
Copy link
Member

gziolo commented Mar 16, 2021

When I tested it locally I saw the following:

Warning: Use of undefined constant GUTENBERG_VERSION - assumed 'GUTENBERG_VERSION' (this will throw an Error in a future version of PHP) in /var/www/html/wp-content/plugins/gutenberg/gutenberg.php on line 245
  | GUTENBERG_VERSION
 
It looks like GUTENBERG_VERSION is only set on production when using the build version of the plugin. You should be able to reproduce it by setting SCRIPT_DEBUG to false.

This is also why PHP unit tests fail:

https://github.com/WordPress/gutenberg/pull/29775/checks?check_run_id=2095591189#step:9:46

Screen Shot 2021-03-16 at 17 52 25

Overall, I like the idea. Thank you for working on it. We just need to find a way to make it work in the case when GUTENBERG_VERSION is udnefined.

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

See my previous comment.

@hellofromtonya
Copy link
Contributor Author

@gziolo I was looking at that earlier today, but then got sidetracked. I'm thinking about altering the decision to include checking if GUTENBERG_VERSION is not defined, then it's also in a development or test mode. Why? As you pointed out, that constant is for production, i.e. meaning it's defined in the production version of the plugin.

hellofromtonya added 2 commits March 16, 2021 12:27
Switches the check around to determine if in production mode:

- GUTENBERG_VERSION is defined
- SCRIPT_DEBUG is not defined || is not true.

Else, defaults to time().
@hellofromtonya hellofromtonya requested a review from gziolo March 16, 2021 18:01
@hellofromtonya
Copy link
Contributor Author

hellofromtonya commented Mar 16, 2021

@gziolo Commit 352e06a flips the check around by determining if in production mode, else it defaults to use time() as the asset version. For prod mode, it checks:

  • Is GUTENBERG_VERSION defined? If yes, do the next check; else use time()
  • Is in not in debug/dev mode, where debug/dev is determined by SCRIPT_DEBUG defined and true? If not in debug/dev mode, then use GUTENBERG_VERSION; else use time().

Testing locally

When the above conditions are met:

<link rel="stylesheet" id="wp-edit-blocks-css" href="http://wpcore.local/wp-content/plugins/gutenberg/build/block-library/editor.css?ver=10.1" media="all">

When adding define( 'SCRIPT_DEBUG', false ); to wp-config.php:

<link rel="stylesheet" id="wp-edit-blocks-css" href="http://wpcore.local/wp-content/plugins/gutenberg/build/block-library/editor.css?ver=10.1" media="all">

When commenting out define( 'GUTENBERG_VERSION', '10.1.1' ) in the plugin:

<link rel="stylesheet" id="wp-edit-blocks-css" href="http://wpcore.local/wp-content/plugins/gutenberg/build/block-library/editor.css?ver=1615918791" media="all">

When restoring the constant and then changing define( 'SCRIPT_DEBUG', true );:

<link rel="stylesheet" id="wp-edit-blocks-css" href="http://wpcore.local/wp-content/plugins/gutenberg/build/block-library/editor.css?ver=1615918820" media="all">

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

Awesome work polishing the logic 💯

@hellofromtonya hellofromtonya deleted the fix/18227/cache-bust-without-filemtime branch March 17, 2021 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository Gutenberg Plugin Issues or PRs related to Gutenberg Plugin management related efforts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants