月火水木金土日
   1234
567891011
12131415161718
19202122232425
262728293031 
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
1234567
891011121314
15161718192021
22232425262728
293031    
       
 123456
78910111213
14151617181920
21222324252627
28      
       
     12
3456789
10111213141516
17181920212223
24252627282930
31      
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
   1234
567891011
12131415161718
19202122232425
262728293031 
       
 123456
78910111213
14151617181920
21222324252627
282930    
       
   1234
567891011
12131415161718
19202122232425
2627282930  
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
1234567
891011121314
15161718192021
22232425262728
       
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
12131415161718
19202122232425
262728293031 
       
 123456
78910111213
14151617181920
21222324252627
282930    
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
1234567
891011121314
15161718192021
22232425262728
2930     
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
    123
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
12131415161718
19202122232425
262728293031 
       
1234567
891011121314
15161718192021
293031    
       
     12
3456789
10111213141516
17181920212223
24252627282930
       
  12345
6789101112
20212223242526
2728293031  
       
1234567
891011121314
15161718192021
22232425262728
2930     
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
    123
11121314151617
25262728   
       
14151617181920
21222324252627
28293031   
       
     12
3456789
10111213141516
17181920212223
24252627282930
31      
   1234
567891011
12131415161718
19202122232425
2627282930  
       
1234567
891011121314
22232425262728
293031    
       
     12
3456789
10111213141516
24252627282930
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
19202122232425
262728293031 
       
   1234
567891011
12131415161718
19202122232425
262728    
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
27282930   
       
      1
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
     12
3456789
10111213141516
24252627282930
31      
   1234
567891011
12131415161718
19202122232425
2627282930  
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
     12
3456789
10111213141516
       
  12345
6789101112
20212223242526
2728293031  
       
  12345
6789101112
2728     
       
      1
23242526272829
3031     
   1234
567891011
12131415161718
       
 123456
78910111213
14151617181920
21222324252627
282930    
       
   1234
567891011
12131415161718
19202122232425
2627282930  
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
28293031   
       
1234567
891011121314
15161718192021
22232425262728
29      
       
    123
45678910
11121314151617
18192021222324
       
 123456
78910111213
21222324252627
28293031   
       
      1
16171819202122
30      
   1234
19202122232425
262728293031 
       
 123456
78910111213
14151617181920
282930    
       
     12
3456789
10111213141516
17181920212223
24252627282930
31      
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
      1
2345678
9101112131415
16171819202122
232425262728 
       
   1234
12131415161718
19202122232425
262728293031 
       
891011121314
22232425262728
293031    
       
     12
3456789
10111213141516
17181920212223
24252627282930
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
1234567
891011121314
15161718192021
22232425262728
2930     
       
    123
45678910
25262728293031
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      

CSSをPreload先読みしてレンダリングブロックしない方法

CSSを遅延読み込みするためには、preload を使えばよいのだが、Firefox 、IEブラウザーでは動作しない。

 

それで、loadCSS   cssrelpreload.js を併用する。

 

 

これだけだと、java script 非対応のブラウザでcss が読み込まれないのでもうひと工夫が必要。

 

これで完璧。

 

オリジナルをミニファイしてサーバーに設置する。

 

  • https://makotoiwasaki.com/wp-content/themes/jstork_custom/library/cssrelpreload.js

 

functions.php に追記する。cssrelpreload.js を読み込む。

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 100  );
function theme_enqueue_styles() {
	wp_deregister_script( 'jquery' );
	wp_register_script('jquery', 'https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js', array(), '1.12.4' );
	wp_enqueue_script( 'loadCSS', get_stylesheet_directory_uri() . '/library/cssrelpreload.js', false, null, false );
}

 

style_loader_tag にフックし、タグ を書き換える。preload 、noscript 等。

<noscript>' . $tag . '</noscript> を入れておかないと、js が動かないブラウザでCSSが読み込まれない。

 

//Convert CSS scripts for use with loadCSS library - https://github.com/filamentgroup/loadCSS
function loadCSS_parser( $tag ){
    //If not a stylesheet, do not edit
    if ( FALSE === strpos( $tag, "rel='stylesheet'" ) ) return $tag;
    
    //Set a variable which will hold the default script for a '<noscript>' tag
    $noscript = '<noscript>' . $tag . '</noscript>';
    
    //Change 'rel' value from 'stylesheet' to 'preload'
    $tag = preg_replace("/='stylesheet'/", '="preload"', $tag);
    
    //Add 'as' and 'onload' attributes
    $tag = preg_replace("/type='text\/css'/", 'as="style" onload="this.onload=null;this.rel=\'stylesheet\'"', $tag);
    
    //Remove media attribute
    $tag = preg_replace("/media='.*'/", '', $tag);
    
    return $tag . $noscript;    //Return
}
add_filter( 'style_loader_tag', 'loadCSS_parser' );

 

 

JS を遅延読み込みするには、次を追加する。

if(!is_admin()){
function replace_script_tag ( $tag ) {
			if ( ( $tag, 'jquery-X-X-X.min.js' ) ) {
		return str_replace( "type='text/javascript' ", 'async ', $tag );
					}
    return str_replace( "type='text/javascript'", 'defer ', $tag );
}
add_filter( 'script_loader_tag', 'replace_script_tag' );
}

基本的に全部 defer にする。

jquery もdefer にする。async にすると動作しないプラグインがでてくる。

 

その他のCSS、JSも以下のように配置し直した。

wp_register_script で最後に true と設定すると、header ではなく、 footer で読み込まれるようになる。

jquery のみヘッダーで defer 読込し、その他のJSはフッターで読み込む。

jqueryを先に読み込んでおかないと機能しないプラグインJSは、依存関係を明示し、array('jquery') を付加する。

 

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 100  );
function theme_enqueue_styles() {
	            wp_deregister_script( 'jquery' );
	            wp_register_script('jquery', 'https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js', array(), '1.12.4' );
				wp_register_script( 'doubletaptogo', '/wp-content/themes/jstork_custom/doubletaptogo.js', array('jquery'));	
				wp_enqueue_script( 'doubletaptogo' );
	    		wp_enqueue_script( 'loadCSS', get_stylesheet_directory_uri() . '/library/cssrelpreload.js', false, null, false );
	 wp_dequeue_style( 'jetpack_css' );
	 wp_dequeue_style( 'tiled-gallery' );
     wp_dequeue_script( 'tiled-gallery' );
	wp_deregister_script( 'tiled-gallery' );
	wp_deregister_style( 'tiled-gallery' );

	wp_deregister_script( 'wpp-js' );
    wp_dequeue_style('wp-block-library');
	wp_dequeue_script( 'photoswipe-lib' );
	wp_dequeue_script( 'photoswipe-ui-default' );
  	wp_dequeue_script( 'photoswipe-frontend' );
	wp_dequeue_style( 'photoswipe-lib' );
	wp_dequeue_style( 'photoswipe-skin' );
	wp_dequeue_style( 'toc-screen' ); 
	wp_dequeue_style( 'whats-new-style' ); 
 	wp_deregister_script( 'jquery-arcw' );
	wp_dequeue_style( 'pz-linkcard' ); 
	
		if(is_singular()){
    //現在の投稿本文をショートコードを展開させて取得
$content = apply_filters( 'the_content', get_post_field('post_content', get_the_ID()) );
if(preg_match('/href=(\"|\')?([^<|>|\"|\'|\n] )\.(jpg|jpeg|png|gif)(\"|\')?/', $content)){
    wp_enqueue_style( 'photoswipe-lib' );
	wp_enqueue_style( 'photoswipe-skin' );
	wp_enqueue_script( 'photoswipe-lib', '/wp-content/plugins/lightbox-photoswipe/lib/photoswipe.min.js', array('jquery'), false, true );	
	wp_enqueue_script( 'photoswipe-ui-default', '/wp-content/plugins/lightbox-photoswipe/lib/photoswipe-ui-default.min.js', array('jquery'), false, true );	
	wp_enqueue_script( 'photoswipe-frontend', '/wp-content/plugins/lightbox-photoswipe/js/frontend.min.js', array('jquery'), false, true );	
	}
if(strpos($content,'lkc-content') !== false){
	wp_register_style( 'linkcard', '/wp-content/themes/jstork_custom/linkcard.css');
    wp_enqueue_style( 'linkcard' );
}
if(strpos($content,'tiled-gallery') !== false){
		wp_enqueue_script(
			'tiled-gallery',
			Jetpack::get_file_url_for_environment(
				'_inc/build/tiled-gallery/tiled-gallery/tiled-gallery.min.js',
				'modules/tiled-gallery/tiled-gallery/tiled-gallery.js'
			),
			array( 'jquery' ), false, true
		);
		wp_enqueue_style( 'tiled-gallery', ( '/wp-content/plugins/jetpack/modules/tiled-gallery/tiled-gallery/tiled-gallery.css' ), array(), '2012-09-21' );
		wp_style_add_data( 'tiled-gallery', 'rtl', 'replace' );
	
}
			if(strpos($content,'toc_container') !== false){
    wp_enqueue_style( 'toc-screen' );
	}
}
     wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('style') );
	
		if( !(is_single()) ){
		wp_enqueue_script( 'pop-tabs', get_stylesheet_directory_uri() . '/pop-tabs.js', array('jquery'), false, true );	
		}
	if(is_date()){
	wp_register_script( 'jquery-arcw', '/wp-content/plugins/archives-calendar-widget/admin/js/jquery.arcw-init.js', array( "jquery" ), ARCWV);
	wp_enqueue_script( 'jquery-arcw' );	
		}
	wp_dequeue_style( 'shortcode' ); 
	wp_dequeue_script( 'devicepx' );
	wp_dequeue_style( 'animate' );
	wp_dequeue_script( 'wp-embed' );
    wp_deregister_script( 'wp-embed' );
    wp_deregister_script( 'fontawesome' );
	wp_deregister_style( 'fontawesome' );
	wp_deregister_style( 'dashicons' );
	wp_deregister_style( 'wpstatistics' );
  wp_deregister_script( 'masonry.pkgd.min' );
  wp_deregister_script( 'wow' );
  wp_deregister_script( 'imagesloaded' );
}

if(!is_admin()){
function replace_script_tag ( $tag ) {
			if ( strpos( $tag, 'jquery1-12-4.min.js' ) ) {
		return str_replace( "type='text/javascript' ", 'defer ', $tag );
					}
    return str_replace( "type='text/javascript'", 'defer ', $tag );
}
add_filter( 'script_loader_tag', 'replace_script_tag' );
}

//Convert CSS scripts for use with loadCSS library - https://github.com/filamentgroup/loadCSS
function loadCSS_parser( $tag ){
    //If not a stylesheet, do not edit
    if ( FALSE === strpos( $tag, "rel='stylesheet'" ) ) return $tag;
    
    //Set a variable which will hold the default script for a '<noscript>' tag
    $noscript = '<noscript>' . $tag . '</noscript>';
    
    //Change 'rel' value from 'stylesheet' to 'preload'
    $tag = preg_replace("/='stylesheet'/", '="preload"', $tag);
    
    //Add 'as' and 'onload' attributes
    $tag = preg_replace("/type='text\/css'/", 'as="style" onload="this.onload=null;this.rel=\'stylesheet\'"', $tag);
    
    //Remove media attribute
    $tag = preg_replace("/media='.*'/", '', $tag);
    
    return $tag . $noscript;    //Return
}
add_filter( 'style_loader_tag', 'loadCSS_parser' );

クリティカルCSSジェネレーター

ファーストビューで必要なCSSはインラインで読み込む。

クリティカルCSSジェネレーターを使用する。

 

投稿記事の頁、アーカイブの頁、カテゴリーの頁などの種類別にクリティカルCSSを読み込む。

header.php に記述。

<?php echo '<style type="text/css" id="firstview">';?>
<?php if(is_singular()):?>
<?php echo file_get_contents(get_stylesheet_directory_uri() . '/first-view/fv-post.css') ."\n";?>
<?php elseif( is_front_page() || is_home() ) : ?>
<?php echo file_get_contents(get_stylesheet_directory_uri() . '/first-view/fv-front.css') ."\n";?>
<?php elseif(()): ?>
<?php echo file_get_contents(get_stylesheet_directory_uri() . '/first-view/fv-cat.css') ."\n";?>
<?php elseif(is_date()): ?>
<?php echo file_get_contents(get_stylesheet_directory_uri() . '/first-view/fv-date.css') ."\n";?>
<?php elseif(is_tag()): ?>
<?php echo file_get_contents(get_stylesheet_directory_uri() . '/first-view/fv-tag.css') ."\n";?>
<?php elseif(is_search()): ?>
<?php echo file_get_contents(get_stylesheet_directory_uri() . '/first-view/fv-src.css') ."\n";?>
<?php else : ?>
<?php echo file_get_contents(get_stylesheet_directory_uri() . '/first-view/fv-front.css') ."\n";?>
<?php endif; ?>
<?php echo '</style>';?>

 

以上の設定で、PageSpeed Insights のテストで、レンダリング云々の警告はなくなるはず。

 

woff2 ヱブフォントの先読み込みについて

 

ヱブフォントをPreload してみる。

header.php の一番上の方に記述する。

 

<link rel="preload" href="/wp-includes/fonts/QuiMi-mincho.woff2" as="font" type="font/woff2" crossorigin>
<noscript><link rel='font' id='QuiMi-mincho-fonts-css'  href='https://makotoiwasaki.com/wp-includes/fonts/QuiMi-mincho.woff2' type='font/woff2' media='all' />
</noscript>

 

 

  • 2685年6月4日

    VPSの移転方法

    VPSメモリ:2GB月額1,530円~CPU 3CoreNVMe SSD 150GB から、 メモリ:2GB 月額690円~ CPU2Core NVMe SSD50GBに移転成功した。朝から5時間ぐらいかかって落ち着いた感じ。 移転の動機は、年間支出削減。8,544円/年 で、さらに、30%をキャッシュバックのキャンペーンだったから。同一の会社だがサービスグレードの違いのみの新規追加VPS契約した。まずs1 からs2(移転先サーバー 192.168.1.2)にSSHログインできるようにする。# s1でSSH鍵生成(まだなければ)ssh-keygen -t rsa -b 4096# Enterキーを3回押してデフォルト設定 # s2に公開鍵をコピ...
  • またまたこのサイトのSSL証明書が期限切れになった。 Kusanagi の自動更新ができていない。 それで手動であれこれしてみても、こんなエラーが出る。# kusanagi update cert Challenge failed for domain makotoiwasaki.com Challenge failed for domain www.makotoiwasaki.com Attempting to renew cert (makotoiwasaki.com) from /etc/letsencrypt/renewal/makotoiwasaki.com.conf produced an unexpected error: Some challenges hav...
  • Google の Indexing API を使うと、新しい投稿記事を瞬時にGoogleの検索エンジンに登録できる。 WordPress のプラグインとしてインデックスAPIが利用できる。使い方は英語だが、この通り: ⏱️ Get Google To Index Your Website Instantly Using the Indexing API ⚡Take a look at how you can use Google's new indexing API & to get your website's pages and content crawled instantly instead of waiting for Google to...
  • Yoast SEO を停止して、Rank Math SEO プラグインを使ってみたらGoogle の検索結果に表示される記事抜粋スニペットの文字数が短過ぎに見えてびっくりした。#1 Yoast Alternative You Deserve - Rank Math SEO vs. Yoast SEORank Math SEO plugin for WordPress is hands down the best Yoast alternative WordPress plugin. And the best thing is, Rank Math is completely FREE!Rank Math
  • WP_CRON を停止して、Linux の crontab に移行する設定をこれまでに何度も試みたがうまくいかなかった。 毎日バックアップされるはずの、UpdraftPlus プラグインのクロンが動いていない。やっと成功した設定方法を記録しておく。wp-config.php に次の行を追加する。define('DISABLE_WP_CRON', true);/var/spool/cron  に、  httpd という名前のファイルを作成し、次の1行を追加する。 所有者を httpd.www など、httpd nginx サーバーの稼働ユーザー名と同じにする。nginx.conf に書いてある。root@s4:/v...
  • Kusanagi WordPress プラットフォームでは Fcache とBcache がある。 Fcache とはNginx ヱブサーバーのキャッシュ機能であり、Kusanagi の独自機能ではない。Nginx のアクセスログを眺めていると、  BYPASS MISS EXPIRED のみで、HITが殆どない。 トップ頁、アーカイブリストの頁ではHIT、 個別投稿頁では、BYPASS MISS EXPIRED ばかりでHITがない。Kusanagi fcache on とすると、fcache は有効になったかのように思えるが、本当にキャッシュが効いているのかどうかはログで確認しないとわからない。まず、Wordpressの編集画面にログインし...
  • HTTPD アクセスログの日本語化 Nginx,  Apache ヱブサーバーのアクセスログを見ると、日本語URLはエンコードされていて読めない。 そこで、デコードして表示させる。 ログのファイル名が ssl_access.log だとすると、tail -f ssl_access.log| perl -ne 'use URI::Escape; print uri_unescape($_);' tail -f access.log | php -R 'echo urldecode($argn)."\n";'で、日本語URlが読める状態で出力される。 Apacheのログをデコードする方法 - Life with ITプログラマ x ...
  • ヱブサイトの再構築中には、スタイルシート、style.css を頻繁に調整更新する。 CSSを追加、編集する度に再読み込みを繰り返して、変更の反映を確認していた。 これで編集者は反映を確認できるのだが、一般閲覧者はわざわざ再読み込みしたり、キャッシュを削除したりするはずはないので、変更が反映されていない崩れたデザインを見ているかもしれない。わざわざリロードしたりキャッシュを削除したりしなくても変更が確実に反映されるような設定方法を発見した。wp_enqueue_scripts で CSS、JS の読み込みを管理している場合には、次のようにfunctions.php に記述する。// 子テーマのstyle.cssを最後に読み込む add_acti...

タグクラウド

靴(1)鰻(1)風邪(2)車旅(21)踊(3)野鳥(1)蛙(2)長寿(3)遺跡(3)鶏(22)車(71)蝶(12)酒(26)蜂(16)著者(3)麺(32)象(25)豆(6)麻(10)香港(16)

WordPress カテゴリ人気記事 Views most

タグ関連記事

閲覧履歴