|
|
--- |
|
|
title: How to create AMP pages in Next.js |
|
|
nav_title: AMP |
|
|
description: With minimal config, and without leaving React, you can start adding AMP and improve the performance and speed of your pages. |
|
|
--- |
|
|
|
|
|
<details> |
|
|
<summary>Examples</summary> |
|
|
|
|
|
- [AMP](https://github.com/vercel/next.js/tree/canary/examples/amp) |
|
|
|
|
|
</details> |
|
|
|
|
|
With Next.js you can turn any React page into an AMP page, with minimal config, and without leaving React. |
|
|
|
|
|
You can read more about AMP in the official [amp.dev](https://amp.dev/) site. |
|
|
|
|
|
|
|
|
|
|
|
To enable AMP support for a page, and to learn more about the different AMP configs, read the [API documentation for `next/amp`](/docs/pages/guides/amp). |
|
|
|
|
|
|
|
|
|
|
|
- Only CSS-in-JS is supported. [CSS Modules](/docs/app/getting-started/css) aren't supported by AMP pages at the moment. You can [contribute CSS Modules support to Next.js](https://github.com/vercel/next.js/issues/10549). |
|
|
|
|
|
|
|
|
|
|
|
The AMP community provides [many components](https://amp.dev/documentation/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: |
|
|
|
|
|
```jsx |
|
|
export const config = { amp: true } |
|
|
|
|
|
function MyAmpPage() { |
|
|
const date = new Date() |
|
|
|
|
|
return ( |
|
|
<div> |
|
|
<p>Some time: {date.toJSON()}</p> |
|
|
<amp-timeago |
|
|
width="0" |
|
|
height="15" |
|
|
datetime={date.toJSON()} |
|
|
layout="responsive" |
|
|
> |
|
|
. |
|
|
</amp-timeago> |
|
|
</div> |
|
|
) |
|
|
} |
|
|
|
|
|
export default MyAmpPage |
|
|
``` |
|
|
|
|
|
The above example uses the [`amp-timeago`](https://amp.dev/documentation/components/amp-timeago/?format=websites) 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: |
|
|
|
|
|
```jsx |
|
|
import Head from 'next/head' |
|
|
|
|
|
export const config = { amp: true } |
|
|
|
|
|
function MyAmpPage() { |
|
|
const date = new Date() |
|
|
|
|
|
return ( |
|
|
<div> |
|
|
<Head> |
|
|
<script |
|
|
async |
|
|
key="amp-timeago" |
|
|
custom-element="amp-timeago" |
|
|
src="https://cdn.ampproject.org/v0/amp-timeago-0.1.js" |
|
|
/> |
|
|
</Head> |
|
|
|
|
|
<p>Some time: {date.toJSON()}</p> |
|
|
<amp-timeago |
|
|
width="0" |
|
|
height="15" |
|
|
datetime={date.toJSON()} |
|
|
layout="responsive" |
|
|
> |
|
|
. |
|
|
</amp-timeago> |
|
|
</div> |
|
|
) |
|
|
} |
|
|
|
|
|
export default MyAmpPage |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
AMP pages are automatically validated with [amphtml-validator](https://www.npmjs.com/package/amphtml-validator) during development. Errors and warnings will appear in the terminal where you started Next.js. |
|
|
|
|
|
Pages are also validated during [Static HTML export](/docs/pages/guides/static-exports) and any warnings / errors will be printed to the terminal. Any AMP errors will cause the export to exit with status code `1` because the export is not valid AMP. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Skip AMP Validation |
|
|
|
|
|
To turn off AMP validation add the following code to `next.config.js` |
|
|
|
|
|
```js |
|
|
experimental: { |
|
|
amp: { |
|
|
skipValidation: true |
|
|
} |
|
|
} |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|