AMPプラグインで"タグ「script amp-onerror」内の必須のテキストがないか、正しくありません。"エラーが出たので対処

 last update 2021年8月4日

いまさらようやく WordPress + AMP プラグインを有効化したのですが、その後 Google Search Console から、次のようなエラーが飛んできて対処したので、情報共有しておきます。

  • "タグ「script amp-onerror」内の必須のテキストがないか、正しくありません。"

Google Search Console で詳細を見てみると、以下の部分がエラーとしてハイライトされていました。

<script amp-onerror>document.querySelector("script[src*='/v0.js']").onerror=function(){document.querySelector('style[amp-boilerplate]').textContent=''}</script><style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript></head><body><svgCode language: HTML, XML (xml)

AMP プラグインには、サイトの DOM を解析して適切な AMP コンポーネントを追加する機能があると聞いたことがありますが、恐らくは、プラグイン側が新仕様を取り入れたにも関わらず、バリデーター側が未対応なためにエラー扱いとなっているようです。

いずれは AMP チーム側が対策するものと思われますが、それまでの間は AMP プラグインのフォーラムで公開されている以下のコードで、対象のコード出力を抑止するのが当面の対策となりそうです。

add_filter( 'amp_optimizer_config', static function ( $config ) {
	$config['transformers'] = array_diff( $config['transformers'], [ AmpProject\Optimizer\Transformer\AmpBoilerplateErrorHandler::class ] );
	return $config;
} );Code language: PHP (php)

また、処理内容は変わりませんが、軽量なプラグイン化されたものもあります。

今回の amp-onerror 絡みの仕様は、どうやら高速化のために導入された経緯がありそうですので、AMP チームの対策後に無効化しやすいプラグインで対応した方が良いかもしれませんね。

(※これは2021年6月の記事です)

(※2021/8/4 追記:2021年7月に AMP Validator 側がこの問題に対処したため、WordPress 側に先のコードを設置する必要は無くなりました。
https://github.com/ampproject/amphtml/issues/22543

参考情報:

Hatena Pocket Line

コメントを記入