Adding AMP Components

The AMP community provides many components to make AMP pages more interactive. Next.js will automatically import all components used on a page and there is no need to manually import AMP component scripts:

  1. export const config = { amp: true }
  2. function MyAmpPage() {
  3. const date = new Date()
  4. return (
  5. <div>
  6. <p>Some time: {date.toJSON()}</p>
  7. <amp-timeago
  8. width="0"
  9. height="15"
  10. datetime={date.toJSON()}
  11. layout="responsive"
  12. >
  13. .
  14. </amp-timeago>
  15. </div>
  16. )
  17. }
  18. export default MyAmpPage

The above example uses the amp-timeago component.

By default, the latest version of a component is always imported. If you want to customize the version, you can use next/head, as in the following example:

  1. import Head from 'next/head'
  2. export const config = { amp: true }
  3. function MyAmpPage() {
  4. const date = new Date()
  5. return (
  6. <div>
  7. <Head>
  8. <script
  9. async
  10. key="amp-timeago"
  11. custom-element="amp-timeago"
  12. src="https://cdn.ampproject.org/v0/amp-timeago-0.1.js"
  13. />
  14. </Head>
  15. <p>Some time: {date.toJSON()}</p>
  16. <amp-timeago
  17. width="0"
  18. height="15"
  19. datetime={date.toJSON()}
  20. layout="responsive"
  21. >
  22. .
  23. </amp-timeago>
  24. </div>
  25. )
  26. }
  27. export default MyAmpPage