Google AMP - Syntaxe de base

Dans ce chapitre, nous discuterons des exigences de base pour démarrer avec les pages Google AMP.

Exemple de page d'ampli

Un exemple de base pour une page d'ampli est montré ci-dessous -

<!doctype html>
<html amp>
   <head>
      <meta charset = "utf-8">
      <title>Amp Sample Page</title>
      <link rel = "canonical" href = "./regular-html-version.html">
      <meta name = "viewport" content = "width = device-width,
      minimum-scale = 1,initial-scale = 1">
      <style amp-custom>
         h1 {color: red}
      </style>
      
      <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>
      <script async src = "https://cdn.ampproject.org/v0.js">
      </script>
   </head>
   <body>
      <h1>Amp Sample Page</h1>
      <p>
         <amp-img 
            src = "images/christmas1.jpg" 
            width = "300" 
            height = "300" 
            layout = "responsive">
         </amp-img>
      </p>
   </body>
</html>

Balises obligatoires

Il y a des balises obligatoires à inclure dans une page amp. Cette section les aborde en détail -

  • Nous devons nous assurer que nous ajoutons amp ou ⚡ à la balise html comme indiqué ci-dessous

<html amp>
   OR 
<html ⚡>
  • Nous devrions ajouter les balises <head> et <body> à la page html.

La validation d'ampli peut échouer si vous manquez l'une des balises meta obligatoires. Certaines balises mets obligatoires qui doivent être ajoutées dans la section principale de la page sont affichées ici -

<meta charset="utf-8">
   <meta name  =  "viewport" 
      content = "width = device-width,
      minimum-scale = 1,
      initial-scale = 1">
  • Lien de rel = "canonical" à ajouter à l'intérieur de la balise head

<link rel = "canonical" href = "./regular-html-version.html">
  • Étiquette de style avec ampli-passe-partout -

<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>
  • Etiquette Noscript avec ampli-passe-partout -

<noscript>
   <style amp-boilerplate>
      body{
         -webkit-animation:none;
         -moz-animation:none;
         -ms-animation:none;
         animation:none}
   </style>
</noscript>
  • La balise de script amp avec async ajouté comme indiqué ci-dessous. C'est la balise la plus importante de toutes -

<script async src = "https://cdn.ampproject.org/v0.js">
</script>
  • Vous devez utiliser cette balise au cas où vous voudriez ajouter un CSS personnalisé à la page. Veuillez noter ici que nous ne pouvons pas appeler de feuille de style externe dans les pages amp. Pour ajouter un css personnalisé, tout votre css doit aller ici -

<style amp-custom>
   //all your styles here
</style>

Vous pouvez valider la page ci-dessus dans votre navigateur en utilisant # developement = 1 à la fin de la page-url.

Maintenant, testons la même chose dans le navigateur. J'ai hébergé la page localement et l'ai enregistrée sous le nom amppage.html.

L'url ci-dessus à tester est

http://localhost/googleamp/amppage.html#development=1

Exemple

<!doctype html>
<html amp>
   <head>
      <meta charset = "utf-8">
      <title>Amp Sample Page</title>
      <link rel = "canonical" href = "./regular-html-version.html">
      <meta name = "viewport" content = "width=device-width,
      minimum-scale = 1,initial-scale = 1">
      <style amp-custom>
         h1 {color: red}
      </style>
      <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>
      <script async src = "https://cdn.ampproject.org/v0.js">
      </script>
   </head>
   <body>
      <h1>Amp Sample Page</h1>
      <p>
         <amp-img 
            src = "images/christmas1.jpg" 
            width = "300" 
            height = "250" 
            layout = "responsive">
         </amp-img>
      </p>
   </body>
</html>

Production

Vous pouvez voir l'état de validation de l'ampli dans la console du développeur comme suit -

Cela nous donne une validation AMP réussie car nous avons ajouté toutes les balises obligatoires requises pour une page amp valide.