-
Notifications
You must be signed in to change notification settings - Fork 138
Enhance detection of enqueued frontend assets #136
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
780b098
306bbec
fb3418e
493a5c5
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 |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| require_once __DIR__ . '/helper.php'; | ||
|
|
||
| /** | ||
| * Audit enqueued scripts in is_front_page(). Ignore /wp-includes scripts. | ||
| * Audit enqueued and printed scripts in is_front_page(). Ignore /wp-includes scripts. | ||
| * | ||
| * It will save information in a transient for 12 hours. | ||
| * | ||
|
|
@@ -25,22 +25,39 @@ function perflab_aea_audit_enqueued_scripts() { | |
| global $wp_scripts; | ||
| $enqueued_scripts = array(); | ||
|
|
||
| foreach ( $wp_scripts->queue as $handle ) { | ||
| $src = $wp_scripts->registered[ $handle ]->src; | ||
| if ( $src && ! strpos( $src, 'wp-includes' ) ) { | ||
| $enqueued_scripts[] = array( | ||
| 'src' => $src, | ||
| 'size' => perflab_aea_get_resource_file_size( perflab_aea_get_path_from_resource_url( $src ) ), | ||
| ); | ||
| foreach ( $wp_scripts->done as $handle ) { | ||
| $script = $wp_scripts->registered[ $handle ]; | ||
|
|
||
| if ( ! $script->src || false !== strpos( $script->src, 'wp-includes' ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| // Add any extra data (inlined) that was passed with the script. | ||
| $inline_size = 0; | ||
| if ( ! empty( $script->extra ) && ! empty( $script->extra['after'] ) ) { | ||
| foreach ( $script->extra['after'] as $extra ) { | ||
| $inline_size += ( is_string( $extra ) ) ? mb_strlen( $extra, '8bit' ) : 0; | ||
| } | ||
| } | ||
|
|
||
| $path = perflab_aea_get_path_from_resource_url( $script->src ); | ||
| if ( ! $path ) { | ||
| continue; | ||
| } | ||
|
|
||
| $enqueued_scripts[] = array( | ||
| 'src' => $script->src, | ||
| 'size' => perflab_aea_get_resource_file_size( $path ) + $inline_size, | ||
| ); | ||
|
|
||
| } | ||
| set_transient( 'aea_enqueued_front_page_scripts', $enqueued_scripts, 12 * HOUR_IN_SECONDS ); | ||
| } | ||
| } | ||
| add_action( 'wp_print_scripts', 'perflab_aea_audit_enqueued_scripts' ); | ||
| add_action( 'wp_footer', 'perflab_aea_audit_enqueued_scripts', PHP_INT_MAX ); | ||
|
|
||
| /** | ||
| * Audit enqueued styles in the frontend. Ignore /wp-includes styles. | ||
| * Audit enqueued and printed styles in the frontend. Ignore /wp-includes styles. | ||
|
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. Why would we ignore
Contributor
Author
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. @dainemawer those assets are included by default by wp, and this module focus on an overall overview of external assets being enqueued (plugins, theme), I think it would make more noise to include wp-includes, since most of the consumers won't be techies and won't know how to react on it.
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. Thats a fair statement, thanks @manuelRod ! |
||
| * | ||
| * It will save information in a transient for 12 hours. | ||
| * | ||
|
|
@@ -50,19 +67,40 @@ function perflab_aea_audit_enqueued_styles() { | |
| if ( ! is_admin() && is_front_page() && current_user_can( 'view_site_health_checks' ) && false === get_transient( 'aea_enqueued_front_page_styles' ) ) { | ||
| global $wp_styles; | ||
| $enqueued_styles = array(); | ||
| foreach ( $wp_styles->queue as $handle ) { | ||
| $src = $wp_styles->registered[ $handle ]->src; | ||
| if ( $src && ! strpos( $src, 'wp-includes' ) ) { | ||
| $enqueued_styles[] = array( | ||
| 'src' => $src, | ||
| 'size' => perflab_aea_get_resource_file_size( perflab_aea_get_path_from_resource_url( $src ) ), | ||
| ); | ||
| foreach ( $wp_styles->done as $handle ) { | ||
| $style = $wp_styles->registered[ $handle ]; | ||
|
|
||
| if ( ! $style->src || false !== strpos( $style->src, 'wp-includes' ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| // Check if we already have the style's path ( part of a refactor for block styles from 5.9 ). | ||
| if ( ! empty( $style->extra ) && ! empty( $style->extra['path'] ) ) { | ||
| $path = $style->extra['path']; | ||
| } else { // Fallback to getting the path from the style's src. | ||
| $path = perflab_aea_get_path_from_resource_url( $style->src ); | ||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ( ! $path ) { | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Add any extra data (inlined) that was passed with the style. | ||
| $inline_size = 0; | ||
| if ( ! empty( $style->extra ) && ! empty( $style->extra['after'] ) ) { | ||
| foreach ( $style->extra['after'] as $extra ) { | ||
| $inline_size += ( is_string( $extra ) ) ? mb_strlen( $extra, '8bit' ) : 0; | ||
| } | ||
| } | ||
|
|
||
| $enqueued_styles[] = array( | ||
| 'src' => $style->src, | ||
| 'size' => perflab_aea_get_resource_file_size( $path ) + $inline_size, | ||
| ); | ||
| } | ||
| set_transient( 'aea_enqueued_front_page_styles', $enqueued_styles, 12 * HOUR_IN_SECONDS ); | ||
| } | ||
| } | ||
| add_action( 'wp_print_styles', 'perflab_aea_audit_enqueued_styles' ); | ||
| add_action( 'wp_footer', 'perflab_aea_audit_enqueued_styles', PHP_INT_MAX ); | ||
|
|
||
| /** | ||
| * Adds tests to site health. | ||
|
|
||
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.
Im noticing quite a few varying function namespaces - maybe this function should be called
perflab_get_path_from_resource_url?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.
hello, @dainemawer all functions are prepending "perflab_aea", what are the function namespaces varying? maybe I'm missing some
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.
I think the approach of having module specific namespaces (all of them starting with
perflab_though due to the overall plugin) is a solid approach, which avoids us running into accidental conflicts between individual modules.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.
@manuelRod I was thinking more globally across the plugin - I've noticed other functions are all prefixed with
perflab_but not necessarilyperflab_aea- that being said it sounds like @felixarntz is good with this approach - Im not swayed either way - just wanted to call it out for consistency reasons :)