Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Don't load frontend scripts in the editor #2788

Merged
merged 4 commits into from Jul 7, 2020

Conversation

Aljullu
Copy link
Contributor

@Aljullu Aljullu commented Jun 26, 2020

Fixes #1910

This PR:

  • Moves register_block_script and get_block_asset_build_path to the AssetApi class (134d212).
  • Updates register_block_script so it doesn't register block scripts in admin (f61fcc9).
  • Fixes the version comparison so 5.2.x point releases get the legacy files (e0dc45e).

How to test the changes in this Pull Request:

  1. Create a post with some JS-based blocks (All Products block and filters blocks, for example).
  2. Open your browser devtools and switch to the network tab. There, filter by files that contain the string frontend in their name.
  3. Reload the tab.
  4. Verify no file ending with -frontend.js was loaded.
  5. With an WP version between 5.2.0 and 5.3.0 (ie: 5.2.5) and verify legacy scripts are still being enqueued.

Changelog

Scripts only relevant to the frontend side of blocks are no longer loaded in the editor.

@Aljullu Aljullu added status: needs review focus: performance The issue/PR is related to performance. labels Jun 26, 2020
@Aljullu Aljullu added this to the 2.9.0 milestone Jun 26, 2020
@Aljullu Aljullu requested a review from nerrad June 26, 2020 09:21
@Aljullu Aljullu requested a review from a team as a code owner June 26, 2020 09:21
@Aljullu Aljullu self-assigned this Jun 26, 2020
@Aljullu Aljullu removed the request for review from a team June 26, 2020 09:21
@github-actions
Copy link
Contributor

github-actions bot commented Jun 26, 2020

Size Change: +819 B (0%)

Total Size: 1.59 MB

Filename Size Change
build/active-filters.js 7.95 kB +1 B
build/all-products-frontend.js 41.4 kB +23 B (0%)
build/all-products.js 24.9 kB +146 B (0%)
build/all-reviews.js 9.61 kB +2 B (0%)
build/attribute-filter.js 11.6 kB -2 B (0%)
build/blocks-legacy.js 3.21 kB +291 B (9%) 🔍
build/blocks.js 3.22 kB +293 B (9%) 🔍
build/cart-frontend.js 63.8 kB +11 B (0%)
build/cart.js 33.1 kB +17 B (0%)
build/checkout-frontend.js 80.4 kB +29 B (0%)
build/checkout.js 38.6 kB +13 B (0%)
build/custom-select-control-style.js 783 B +1 B
build/featured-category-legacy.js 7.25 kB -14 B (0%)
build/featured-category.js 7.6 kB +2 B (0%)
build/featured-product-legacy.js 9.51 kB -4 B (0%)
build/featured-product.js 9.85 kB -1 B
build/handpicked-products-legacy.js 6.91 kB -2 B (0%)
build/handpicked-products.js 7.24 kB -1 B
build/price-filter.js 10.1 kB +1 B
build/product-best-sellers-legacy.js 6.99 kB -2 B (0%)
build/product-best-sellers.js 7.3 kB -2 B (0%)
build/product-categories-legacy.js 3.22 kB -6 B (0%)
build/product-category-legacy.js 7.91 kB -4 B (0%)
build/product-category.js 8.25 kB -4 B (0%)
build/product-new-legacy.js 7.14 kB -2 B (0%)
build/product-new.js 7.47 kB -3 B (0%)
build/product-on-sale-legacy.js 7.52 kB +6 B (0%)
build/product-on-sale.js 7.87 kB -2 B (0%)
build/product-search-legacy.js 3.13 kB -6 B (0%)
build/product-search.js 3.43 kB -2 B (0%)
build/product-tag-legacy.js 6.08 kB +1 B
build/product-tag.js 6.39 kB -1 B
build/product-top-rated-legacy.js 7.11 kB -4 B (0%)
build/product-top-rated.js 7.44 kB -1 B
build/reviews-by-category.js 11.6 kB +3 B (0%)
build/reviews-by-product-legacy.js 12.8 kB -3 B (0%)
build/reviews-by-product.js 13.1 kB +8 B (0%)
build/single-product-frontend.js 44.2 kB +21 B (0%)
build/single-product.js 18.3 kB +15 B (0%)
build/spinner-style-legacy.js 771 B -1 B
build/vendors-legacy.js 367 kB +3 B (0%)
build/vendors.js 415 kB +4 B (0%)
build/wc-payment-method-stripe.js 11.9 kB -2 B (0%)
build/wc-settings.js 2.14 kB -3 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/active-filters-frontend.js 7.23 kB 0 B
build/all-reviews-legacy.js 9.29 kB 0 B
build/attribute-filter-frontend.js 16.8 kB 0 B
build/block-error-boundary-legacy.js 775 B 0 B
build/block-error-boundary.js 775 B 0 B
build/custom-select-control-style-legacy.js 782 B 0 B
build/editor-legacy-rtl.css 12.5 kB 0 B
build/editor-legacy.css 12.5 kB 0 B
build/editor-rtl.css 13.5 kB 0 B
build/editor.css 13.5 kB 0 B
build/price-filter-frontend.js 14.1 kB 0 B
build/product-categories.js 3.22 kB 0 B
build/product-list-style-legacy.js 775 B 0 B
build/products-by-attribute-legacy.js 7.88 kB 0 B
build/products-by-attribute.js 8.2 kB 0 B
build/reviews-by-category-legacy.js 11.3 kB 0 B
build/reviews-frontend-legacy.js 8.16 kB 0 B
build/reviews-frontend.js 9.01 kB 0 B
build/snackbar-notice-style-legacy.js 778 B 0 B
build/snackbar-notice-style.js 779 B 0 B
build/spinner-style.js 772 B 0 B
build/style-legacy-rtl.css 5.56 kB 0 B
build/style-legacy.css 5.57 kB 0 B
build/style-rtl.css 18.1 kB 0 B
build/style.css 18.1 kB 0 B
build/vendors-style-legacy-rtl.css 1.03 kB 0 B
build/vendors-style-legacy.css 1.03 kB 0 B
build/vendors-style-legacy.js 102 B 0 B
build/vendors-style-rtl.css 1.03 kB 0 B
build/vendors-style.css 1.03 kB 0 B
build/vendors-style.js 102 B 0 B
build/wc-blocks-data.js 7.09 kB 0 B
build/wc-blocks-middleware.js 932 B 0 B
build/wc-blocks-registry.js 2.19 kB 0 B
build/wc-payment-method-cheque.js 794 B 0 B
build/wc-payment-method-paypal.js 830 B 0 B
build/wc-shared-context.js 1.51 kB 0 B

compressed-size-action

Copy link
Contributor

@nerrad nerrad left a comment

Choose a reason for hiding this comment

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

Code wise this looks great @Aljullu! I especially like the changes to src/Assets.php and Automattic\WooCommerce\Blocks\Assets\Api so that we preserve back-compat while treating Assets\Api as authoritative. That fixes a flaw in the existing code so 👏 .

I tested Single Product and Checkout blocks since those are the ones I was concerned about. Single Product works okay, but it looks like there is a problem with the payment method script registration for the checkout block on the frontend (it's not loading). Will you look into that please?

@@ -40,7 +40,7 @@ public function __construct( Package $package ) {
* @return string The cache buster value to use for the given file.
*/
protected function get_file_version( $file ) {
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $this->package->get_path() . $file ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

good additional change here 👍 . Although I wonder if we should still log instances when the file doesn't exist? Should there be at least a log trail of filemtime failing because the file doesn't exist for potential troubleshooting?

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 not sure to understand you. You mean throwing an error if the file doesn't exist?

Copy link
Contributor

Choose a reason for hiding this comment

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

No I mean error_log if it doesn't exist. Previously there'd be a php warning if filemtime was used on a file that doesn't exist and that'd be a signal that something went wrong with the build. So my comment was birthed out of wondering if we're okay with losing that signal that something isn't right with the build process.

I'm on the fence here, and I'd be okay with leaving this code as is.

if ( version_compare( $wp_version, '5.2', '>' ) ) {
if ( version_compare( $wp_version, '5.3', '>=' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing you made the change here because it covers scenarios where the version might be 5.2.1 etc right? (in which case 💯 ).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, exactly!

@Aljullu
Copy link
Contributor Author

Aljullu commented Jul 3, 2020

Thanks for the review @nerrad!

I tested Single Product and Checkout blocks since those are the ones I was concerned about. Single Product works okay, but it looks like there is a problem with the payment method script registration for the checkout block on the frontend (it's not loading). Will you look into that please?

Honestly, I'm not sure what was going on, but in 70e54dd I refactored register_assets() so it uses the register_scripts() method from the AssetApi class, and it seems to have fixed the issue.

@@ -88,26 +88,31 @@ public function register_script( $handle, $relative_src, $dependencies = [], $ha
$version = $this->get_file_version( $relative_src );
}

wp_register_script( $handle, $src, $dependencies, $version, true );
wp_register_script( $handle, $src, apply_filters( 'woocommerce_blocks_register_script_dependencies', $dependencies, $handle ), $version, true );
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 took this filter from src/Assets.php, we were inconsistently applying it in Assets but not in AssetsApi. Now that we have a unique way to register scripts, I think it makes sense to keep the filter.

Copy link
Contributor

Choose a reason for hiding this comment

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

ahh that would explain it. The Payments API uses that to register the handle for payment method scripts as dependencies on certain handles (so various registration happens before the blocks load).

@haszari haszari modified the milestones: 2.9.0, 3.0.0 Jul 5, 2020
Copy link
Contributor

@nerrad nerrad left a comment

Choose a reason for hiding this comment

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

Re-tested and it all works good now 👍

@Aljullu Aljullu merged commit 9dfb123 into main Jul 7, 2020
@Aljullu Aljullu deleted the fix/1910-no-frontend-files-in-editor branch July 7, 2020 08:15
@nerrad nerrad mentioned this pull request Jul 20, 2020
21 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
focus: performance The issue/PR is related to performance.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

frontend.js files are loaded in the editor
4 participants