Created
March 15, 2024 05:19
-
-
Save aliboy08/523edb1f1343bdcf97de41944e76b2db to your computer and use it in GitHub Desktop.
vite wp load critical css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('wp_head', 'load_critical_css', 0); | |
function load_critical_css(){ | |
$dir = FF_DIR .'/dist'; | |
$handle = 'src/critical.js'; | |
$manifest = FF\Vite\get_manifest($dir); | |
if( !$manifest ) return; | |
if( $manifest->is_dev ) { | |
// dev, load file | |
FF\Vite\enqueue_asset( $dir, $handle, [ 'handle' => $handle, 'in-footer' => false ] ); | |
} | |
else { | |
// production, inline css | |
$css_file = $dir.'/'.$manifest->data->$handle->css[0]; | |
if( !file_exists($css_file) ) return; | |
echo '<style type="text/css">'. file_get_contents($css_file) .'</style>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment