AMPページに最低限必要なサンプルコード

2023-02-28AMP

AMPページに最低限必要なサンプルコード

AMPページに最低限必要なサンプルコード

AMPページに最低限必要なサンプルコードです

そのままサンプルページとしてリンク置いて置きます

<!doctype html>
<html ⚡ lang="ja">
  <head>
	<meta charset="utf-8">
  <script async src="https://cdn.ampproject.org/v0.js"></script>
	<title>AMPページサンプル</title>
	<link rel="canonical" href="正規のURL">
	<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
	<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>
	<p>AMPページサンプル</p>
  </body>
</html>

AMPが有効か確認する

ブラウザのデロペッパーツール(F12)を開いて、コンソールログの一番上に「Powered by AMP ⚡ HTML」とあればヘーダー部分でのAMP化は認められています

とわいっても、AMPエラーがあってもこれは表示されるのでちゃんと有効になっているか確認するにはAMPテストツールを利用します

AMPページのURLを入力して、有効なAMPページですと表示されればGoogle検索のAMPインデックスに大きな支障はないと判断できます

AMPテストツールの結果

doctype要素

一般的な記述でAMP用という訳ではないですが、AMPページおいて<!doctype html>と記述する必要があります

<!doctype html>

html要素

HTML要素は<html amp lang="ja">または<html ⚡ lang="ja">の様に雷の絵文字(※)を利用して記述できます
「⚡」はかみなりを変換リストに出てくる全角絵文字のもので問題ありません

<html amp lang="ja">

AMP-JSライブラリの読み込み

基本となるAMP-JSライブラリを読み込みます

公式には「<head>内の2つ目の要素に記述する」とるので一応2つ目に・・って言っても下の方でもエラーは全然出ないし、そもそもそう書いてるampproject公式のサイトがめっちゃ下の方で読み込んでるんですけどね(ついでにいうとampprojectのコンソール見ると真っ赤だったり?)

<script async src="https://cdn.ampproject.org/v0.js"></script>

link rel="canonical"

canonicalはAMPページではない正規のURLを指定します(PCやタブレット用ページという認識でOK)

<link rel="canonical" href="正規のURL">

またPCもタブレットもスマートフォンも同一のAMPページのみで構成されているサイトであれば、link rel="canonical"には自身のページを指定します

viewport設定

スマートフォンが登場した頃からのお馴染みのmeta viewport設定ですがAMPでは必須要素となっています

<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">

AMPのベースCSS読み込み

<style amp-boilerplate>~</style>としてAMPページのベースCSS読み込みが必須です

デザイン的なものの基準が指定してある訳ではなく何かをシームレスにするCSSが記述されている模様

<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>

またAMPページのCSSをデザインする場合は<style amp-custom>~</style>内にインラインで記述します

<style amp-custom>
CSSコード
</style>

Schema.orgでの構造化データ

AMPページを有効にするための必須項目ではありませんが、構造化データのマークアップはAMPページの利点を得る為に順必須といったところです

構造化データのマークアップ方法はSchema.orgをJSON-LDで記述するのがgoogle公式では推奨されています(他も可)

AMP

Posted by Yousuke.U