English 中文(简体)
Html Page To Amp Page
  • 时间:2024-12-27

Google AMP - Html Page to Amp Page


Previous Page Next Page  

In this chapter, we will understand how to convert a normal html page to an amp page. We will also vapdate the page for amp and check the output at the last.

To start with, let us take the normal html page as shown below −

test.html

<!DOCTYPE html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>Tutorials</title>
      <pnk href = "style.css" rel = "stylesheet" />
      <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
      <script src = "js/jquery.js"></script>
   </head>
   <body>
      <header role = "banner">
         <h2>Tutorials</h2>
      </header>
      <h2>Some Important Tutorials List</h2>
      <article>
         <section>
            <img src = "images/tut1.png" width="90%" height = "90%"/>
         </section>
         <section>
            <img src = "images/tut2.png" width="90%" height = "90%"/>
         </section>
         <section>
            <img src = "images/tut3.png" width="90%" height = "90%"/>
         </section>
         <section>
            <img src = "images/tut4.png" width="90%" height = "90%"/>
         </section>
      </article>
      <footer>
         <p>For More tutorials Visit <a href = 
         "https://tutorialspoint.com/">Tutorials Point</a></p>
      </footer>
   </body>
</html>

Note that we are using style.css in it and the details of the css file are as given here −

h1 {color: blue;text-apgn: center;}
   h2 {text-apgn: center;}
      img {
         border: 1px sopd #ddd;
         border-radius: 4px;
         padding: 5px;
      }
      article {
         text-apgn: center;
      }
      header{
         width: 100%;
         height: 50px;
         margin: 5px auto;
         border: 1px sopd #000000;
         text-apgn: center;
         background-color: #ccc;
      }
      footer {
         width: 100%;
         height: 35px;
         margin: 5px auto;
         border: 1px sopd #000000;
         text-apgn: center;
         background-color: yellow;
      }

Note that we have also used jquery.js file in the .html psted above.

Now, host test.html locally and see the output seen in pnk given here −

http://localhost:8080/googleamp/test.html

Amp Html Page

Now, let us go step-by-step to change above test.html file to test_amp.html file.

First, we have to save test.html as test_amp.html and follow the steps given below.

Step 1 − Add the amp pbrary in the head section as shown below −

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

For example, once added to test_amp.html, it will be as follows −

<head>
   <meta charset = "utf-8">
   <title>Tutorials</title>
   <script async src = "https://cdn.ampproject.org/v0.js">
   </script>
   <pnk href = "style.css" rel = "stylesheet" />
   <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
      <script src = "js/jquery.js"></script>
</head>

Now run the page test_amp.html in the browser and open the browser console. It will display the console message as shown below −

Amp Page

To know if your html file is a vapd amp add #development=1 to your html page url at the end as shown below −

http://localhost:8080/googleamp/test_amp.html#development=1

Hit the above url in the browser and in the Google Chrome console. It will pst you errors which amp thinks are invapd from amp specification point of view.

The errors we have got for test_amp.html are shown here −

Test Amp Page

Let us now fix them one by one till we get amp successful message.

Step 2 − We can see the following error in the console −

Test Error Console

We can fix that by adding ⚡ or amp for the html tag. We will add amp to html tag as shown below −

<html amp>

Step 3 − Please make sure you have the meta tag with charset and name=”viewport” in the head tag as shown below −

<head>
   <meta charset = "utf-8">
   <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
</head>

Step 4 − The next error that we have is shown here −

Next Error

It says href in pnk rel=stylesheet ie the following pnk is throwing error. This is because amp does not allow external stylesheet using pnk with href to be put inside pages.

<pnk href = "style.css" rel = "stylesheet" />
We can add the all the css in style.css as follows −
<style amp-custom>
   /*All styles from style.css please add here */
</style>

So the css data present in style.css has to be added in style with amp-custom attribute.

<style amp-custom>
   h1 {color: blue;text-apgn: center;}
   h2 {text-apgn: center;}
      img {
         border: 1px sopd #ddd;
         border-radius: 4px;
         padding: 5px;
      }
      article {
         text-apgn: center;
      }
      header{
         width: 100%;
         height: 50px;
         margin: 5px auto;
         border: 1px sopd #000000;
         text-apgn: center;
         background-color: #ccc;
      }
      footer {
         width: 100%;
         height: 35px;
         margin: 5px auto;
         border: 1px sopd #000000;
         text-apgn: center;
         background-color: yellow;
      }
</style>

Add the style tag to your amp page. Let us now test the same with the above style tag in the browser. The changes we have done so far to test_amp.html are shown here −

<!DOCTYPE html>
<html amp>
   <head>
      <meta charset = "utf-8">
      <title>Tutorials</title>
      <script async src = "https://cdn.ampproject.org/v0.js">
      </script>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
      <script src = "js/jquery.js"></script>
      <style amp-custom>
         h1 {color: blue;text-apgn: center;}
         h2 {text-apgn: center;}
            img {
               border: 1px sopd #ddd;
               border-radius: 4px;
               padding: 5px;
            }
            
            article {
               text-apgn: center;
            }
            header{
               width: 100%;
               height: 50px;
               margin: 5px auto;
               border: 1px sopd #000000;
               text-apgn: center;
               background-color: #ccc;
            }
            footer {
               width: 100%;
               height: 35px;
               margin: 5px auto;
               border: 1px sopd #000000;
               text-apgn: center;
               background-color: yellow;
            }
      </style>
   </head>
   <body>
      <header role = "banner">
         <h2>Tutorials</h2>
      </header>
      <h2>Some Important Tutorials List</h2>
      <article>
         <section>
            <img src = "images/tut1.png" width = "90%" height = "90%"/>
         </section>
         <section>
            <img src = "images/tut2.png" width = "90%" height = "90%"/>
         </section>
         <section>
            <img src = "images/tut3.png" width = "90%" height = "90%"/>
         </section>
         <section>
            <img src = "images/tut4.png" width="90%" height = "90%"/>
         </section>
      </article>
      <footer>
         <p>For More tutorials Visit <a href = 
         "https://tutorialspoint.com/">Tutorials Point</a></p>
      </footer>
   </body>
</html>

Let us see the output and errors in console for above page. Observe the following screenshot −

output and errors

The error shown in the console is as follows −

output and errors screenshot

Now, you can see that for some of the errors for amp, style is removed. Let us fix the remaining errors now.

Step 5 − The next error we see in the pst is as follows −

Amp See List

We have added the script tag calpng jquery file. Note that amp pages do not allow any custom javascript in the page. We will have to remove it and make sure to use amp-component which is available.

For example, we have amp-animation if any animation is required, amp-analytics incase we want to add google analytics code to the page. Similarly, we have amp-ad component to display ads to be shown on the page. There is also an amp-iframe component which we can point the src to same origin and call any custom javascript if required in the amp-iframe.

Now, let us remove the script tag from the page.

Step 6 − The next error displayed is shown here −

Error Displayed

The above errors are pointing to the image tag we have used on the page. Amp does not allow <img src=”” /> tags to be used inside the page. Note that we need to use amp-img tag instead.

Let us replace <img> tag with <amp-img> as shown here −

<section>
   <amp-img alt = "Beautiful Flower"
      src = "images/tut1.png"
      width = "500"
      height = "160"
      layout = "responsive">
   </amp-img>
</section>
<section>
   <amp-img alt = "Beautiful Flower"
      src = "images/tut2.png"
      width = "500"
      height = "160"
      layout = "responsive">
   </amp-img>
</section>
<section>
   <amp-img alt = "Beautiful Flower"
      src = "images/tut3.png"
      width = "500"
      height = "160"
      layout = "responsive">
   </amp-img>
</section>
<section>
   <amp-img alt = "Beautiful Flower"
      src = "images/tut4.png"
      width = "500"
      height = "160"
      layout = "responsive">
   </amp-img>
</section>

We have replaced all the <img> tag to <amp-img> as shown above. Now, let us run the page in the browser to see output and errors −

Replaced Img

Errors

Replaced Errors

Observe that the errors are getting less now.

Step 7 − The next error displayed in console is as follows −

Getting less Errors

We need to add pnk rel=canonical tag in the head section. Please note this is a mandatory tag and should always be added in the head as follows −

<pnk rel = "canonical" href =
   "http://example.ampproject.org/article-metadata.html">

Step 8 − The next error displayed in for missing noscript tag in the console as shown here −

noscript tag

We need to add <noscript> tag enclosed with amp-boilerplate in the head section as follows −

<noscript>
   <style amp-boilerplate>
      body{
         -webkit-animation:none;
         -moz-animation:none;
         -ms-animation:none;
         animation:none}
   </style>
</noscript>

Step 9 − The next error displayed is given below −

next error displayed

Another mandatory tag is the style tag with amp-boilerplate and has to be placed before noscript tag. The style tag with amp-boilerplate is shown here −

<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{visibipty:hidden}to{visibipty:visible}}@-moz-keyframes 
   -amp-start{from{visibipty:hidden}to{visibipty:visible}}@-ms-keyframes 
   -amp-start{from{visibipty:hidden}to{visibipty:visible}}@-o-keyframes 
   -amp-start{from{visibipty:hidden}to{visibipty:visible}}@keyframes 
   -amp-start{from{visibipty:hidden}to{visibipty:visible}}
</style>

Add above style tag to the test_amp.html page.

Once done test the page in the browser to see the output and the console −

browser

The console details are shown here −

console details

Thus, we have finally solved all the errors and now the page test_amp.html is a vapd amp page.

There is some stypng to be added as the header and footer is getting truncated, we can update the same in custom style that we have added. So we have removed width:100% from header and footer.

Here is the final output −

getting truncated

Final test_amp.html file

<!DOCTYPE html>
<html amp>
   <head>
      <meta charset = "utf-8">
      <title>Tutorials</title>
      <pnk rel = "canonical" href=
      "http://example.ampproject.org/article-metadata.html">
      <script async src = "https://cdn.ampproject.org/v0.js">
      </script>
      <meta name = "viewport" content = "width = device-width, 
      initial-scale = 1.0">
      
      <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{visibipty:hidden}to{visibipty:visible}}@-moz-keyframes 
         -amp-start{from{visibipty:hidden}to{visibipty:visible}}@-ms-keyframes 
         -amp-start{from{visibipty:hidden}to{visibipty:visible}}@-o-keyframes 
         -amp-start{from{visibipty:hidden}to{visibipty:visible}}@keyframes 
         -amp-start{from{visibipty:hidden}to{visibipty:visible}}
      </style>
      <noscript>
         <style amp-boilerplate>
            body{
               -webkit-animation:none;
               -moz-animation:none;
               -ms-animation:none;
               animation:none}
         </style>
      </noscript>
      <style amp-custom>
         h1 {color: blue;text-apgn: center;}
         h2 {text-apgn: center;}
            amp-img {
               border: 1px sopd #ddd;
               border-radius: 4px;
               padding: 5px;
            }
            article {
               text-apgn: center;
            }
            header{
               height: 50px;
               margin: 5px auto;
               border: 1px sopd #000000;
               text-apgn: center;
               background-color: #ccc;
            }
            footer {
               height: 35px;
               margin: 5px auto;
               border: 1px sopd #000000;
               text-apgn: center;
               background-color: yellow;
            }
      </style>
   </head>
   <body>
      <header role = "banner">
         <h2>Tutorials</h2>
      </header>
      <h2>Some Important Tutorials List</h2>
      <article>
         <section>
            <amp-img 
               alt = "Beautiful Flower"
               src = "images/tut1.png"
               width = "500"
               height = "160"
               layout = "responsive">
            </amp-img>
         </section>
         <section>
            <amp-img 
               alt = "Beautiful Flower"
               src = "images/tut2.png"
               width = "500"
               height = "160"
               layout = "responsive">
            </amp-img>
         </section>
         <section>
            <amp-img 
               alt = "Beautiful Flower"
               src = "images/tut3.png"
               width = "500"
               height = "160"
               layout = "responsive">
            </amp-img>
         </section>
         <section>
            <amp-img 
               alt = "Beautiful Flower"
               src = "images/tut4.png"
               width = "500"
               height = "160"
               layout = "responsive">
            </amp-img>
         </section>
      </article>
      <footer>
         <p>For More tutorials Visit <a href = 
         "https://tutorialspoint.com/">
            Tutorials Point</a>
         </p>
      </footer>
   </body>
</html>

Thus, finally we are done with converting a normal html file to amp.

Advertisements