Skip to content

Commit

Permalink
Merge pull request #3 from kuno1/bugfix/lazy-load-warning
Browse files Browse the repository at this point in the history
Bugfix/lazy load warning
  • Loading branch information
fumikito authored Feb 26, 2024
2 parents 227d2ed + 7db56b1 commit 98ceb06
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 74 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
}
],
"minimum-stability": "stable",
"require": {},
"require": {
"php": ">=7.4.0"
},
"autoload": {
"psr-0": {
"Kunoichi\\AssetsLazyLoader": "./src"
Expand Down
21 changes: 0 additions & 21 deletions dist/fg-loadcss/LICENSE

This file was deleted.

1 change: 0 additions & 1 deletion dist/fg-loadcss/cssrelpreload.min.js

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
},
"homepage": "https://github.com/kuno1/assets-lazy-loader#readme",
"devDependencies": {
"@wordpress/env": "^1.6.0",
"fg-loadcss": "^2.1.0"
"@wordpress/env": "9.0"
}
}
20 changes: 12 additions & 8 deletions src/Kunoichi/AssetsLazyLoader/JqueryOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JqueryOptimizer extends Singleton {
*/
protected function init() {
// Change jQuery path.
add_action( 'init', [ $this, 'enhance_jQuery' ], 1 );
add_action( 'wp_default_scripts', [ $this, 'enhance_jquery' ], 11 );
$this->setting = wp_parse_args( $this->setting );
}

Expand All @@ -39,16 +39,21 @@ protected function parse_args( $args ) {
}

/**
* Change jQuery to
* Change jQuery to footer.
*
* @param \WP_Scripts $wp_scripts
*/
public function enhance_jquery() {
public function enhance_jquery( $wp_scripts ) {
// Do nothing on admin screen.
// It's prohibit to remove jQuery.
if ( is_admin() || $this->is_login() ) {
return;
}
if ( ! isset( $wp_scripts->registered['jquery-core'] ) ) {
// jQuery is not registered.
return;
}
// Save current version.
global $wp_scripts;
$jquery = $wp_scripts->registered['jquery-core'];
// Set jQuery version and src if specified.
// If not set, use default.
Expand All @@ -57,10 +62,9 @@ public function enhance_jquery() {
// Flag to move_footer.
$move_jquery_to_footer = (bool) $this->footer;
// Remove existing.
wp_deregister_script( 'jquery' );
wp_deregister_script( 'jquery-core' );
$wp_scripts->remove( [ 'jquery', 'jquery-core' ] );
// Register them again.
wp_register_script( 'jquery', false, [ 'jquery-core' ], $jquery_ver, $move_jquery_to_footer );
wp_register_script( 'jquery-core', $jquery_src, [], $jquery_ver, $move_jquery_to_footer );
$wp_scripts->add( 'jquery-core', $jquery_src, [], $jquery_ver, $move_jquery_to_footer );
$wp_scripts->add( 'jquery', false, [ 'jquery-core' ], $jquery_ver, $move_jquery_to_footer );
}
}
48 changes: 7 additions & 41 deletions src/Kunoichi/AssetsLazyLoader/StyleLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
*/
class StyleLoader extends HandleDetector {

/**
* @var bool If set preload at least 1, enable.
*/
protected $preloaded = false;

/**
* Get admin critical files.
*
Expand All @@ -32,34 +27,15 @@ public static function admin_critical( $extra = [] ) {
*/
protected function init() {
add_filter( 'style_loader_tag', [ $this, 'style_loader_tag' ], 9999, 4 );
add_action( 'wp_head', [ $this, 'preload_helper' ], 100 );
if ( $this->in_login ) {
add_action( 'login_head', [ $this, 'preload_helper' ], 100 );
}
if ( $this->in_admin ) {
add_action( 'admin_head', [ $this, 'preload_helper' ], 100 );
}
}

/**
* Render Helper JS if enabled.
*/
public function preload_helper() {
if ( ! $this->preloaded ) {
return;
}
$js = $this->dir . '/dist/fg-loadcss/cssrelpreload.min.js';
if ( ! file_exists( $js ) ) {
return;
}
printf( "<script>\n%s\n</script>", file_get_contents( $js ) );
}

/**
* @param string $tag
* @param string $handle
* @param string $href
* @param string $media
* Change style loader tag.
*
* @param string $tag HTML tag.
* @param string $handle Handle name.
* @param string $href URL of CSS.
* @param string $media Media attribute.
*
* @return string
*/
Expand All @@ -72,22 +48,12 @@ public function style_loader_tag( $tag, $handle, $href, $media ) {
return $tag;
}
// Change stylesheet.
$this->preloaded = true;
$html = <<<'HTML'
<link rel="preload" id="%1$s" href="%2$s" as="style" onload="this.onload=null;this.rel='stylesheet'" media="%4$s" />
<noscript>
%3$s
</noscript>
HTML;
$html ='<link id="%1$s" rel="stylesheet" href="%2$s" onload="this.onload=null;this.media=\'%3$s\'" media="print" />';
return sprintf(
$html,
esc_attr( $handle . '-css' ),
esc_url( $href ),
$tag,
esc_attr( $media )
);
}


}

0 comments on commit 98ceb06

Please sign in to comment.