Skip to content

Instantly share code, notes, and snippets.

@aliboy08
Created March 15, 2024 05:19
Show Gist options
  • Save aliboy08/523edb1f1343bdcf97de41944e76b2db to your computer and use it in GitHub Desktop.
Save aliboy08/523edb1f1343bdcf97de41944e76b2db to your computer and use it in GitHub Desktop.
vite wp load critical css
<?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