-
Notifications
You must be signed in to change notification settings - Fork 4.8k
WIP Use package.json React version for vendor script cache busting #76798
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,29 +369,32 @@ function gutenberg_enqueue_stored_styles( $options = array() ) { | |
| function gutenberg_register_vendor_scripts( $scripts ) { | ||
| $extension = SCRIPT_DEBUG ? '.js' : '.min.js'; | ||
|
|
||
| // Bust browser caches when the bundled React version changes (see build/constants.php, generated from package.json). | ||
| $react_vendor_version = defined( 'GUTENBERG_REACT_VENDOR_VERSION' ) ? GUTENBERG_REACT_VENDOR_VERSION : '18.3.1'; | ||
|
|
||
| gutenberg_override_script( | ||
| $scripts, | ||
| 'react', | ||
| gutenberg_url( 'build/scripts/vendors/react' . $extension ), | ||
| // WordPress Core in `wp_register_development_scripts` sets `wp-react-refresh-entry` as a dependency to `react` when `SCRIPT_DEBUG` is true. | ||
| // We need to preserve that here. | ||
| SCRIPT_DEBUG ? array( 'wp-react-refresh-entry', 'wp-polyfill' ) : array( 'wp-polyfill' ), | ||
| '18' | ||
| $react_vendor_version | ||
| ); | ||
| gutenberg_override_script( | ||
| $scripts, | ||
| 'react-dom', | ||
| gutenberg_url( 'build/scripts/vendors/react-dom' . $extension ), | ||
| array( 'react' ), | ||
| '18' | ||
| $react_vendor_version | ||
| ); | ||
|
|
||
| gutenberg_override_script( | ||
| $scripts, | ||
| 'react-jsx-runtime', | ||
| gutenberg_url( 'build/scripts/vendors/react-jsx-runtime' . $extension ), | ||
| array( 'react' ), | ||
| '18' | ||
| $react_vendor_version | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the react version here is good but I'm not a fan of the wp-build changes. I'll comment there.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A good approach how to extract and use the React version is to generate The version can be extracted from the The root |
||
| ); | ||
| } | ||
| add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ const __dirname = path.dirname( fileURLToPath( import.meta.url ) ); | |
| * | ||
| * @param {string} rootDir Root directory path. | ||
| * @param {string} baseUrlExpression PHP expression for base URL (e.g. "includes_url( 'build' )"). | ||
| * @return {Promise<Record<string, string>>} Replacements object with {{PREFIX}}, {{VERSION}}, {{BASE_URL}}. | ||
| * @return {Promise<Record<string, string>>} Replacements object with {{PREFIX}}, {{VERSION}}, {{BASE_URL}}, {{REACT_VERSION}}. | ||
| */ | ||
| export async function getPhpReplacements( rootDir, baseUrlExpression ) { | ||
| const rootPackageJson = getPackageInfoFromFile( | ||
|
|
@@ -31,10 +31,17 @@ export async function getPhpReplacements( rootDir, baseUrlExpression ) { | |
| const name = rootPackageJson.wpPlugin?.name || 'gutenberg'; | ||
| const version = rootPackageJson.version; | ||
|
|
||
| // Used to cache-bust vendor React bundles when the React pin changes independently of WordPress. | ||
| const reactVersion = | ||
| rootPackageJson.devDependencies?.react ?? | ||
| rootPackageJson.dependencies?.react ?? | ||
| ''; | ||
|
|
||
| return { | ||
| '{{PREFIX}}': name, | ||
| '{{VERSION}}': version, | ||
| '{{BASE_URL}}': baseUrlExpression, | ||
| '{{REACT_VERSION}}': reactVersion, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WP build is a generic tool, so we shouldn't add random variables like that IMO. |
||
| }; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following from #46768