Skip to content

Commit 2eba114

Browse files
Added Google AMP example
1 parent 807d4be commit 2eba114

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ Supported options:
406406
<p><details>
407407
<summary><b>Examples</b></summary>
408408
<ul><li><a href="./examples/with-styled-components">Styled components custom document</a></li></ul>
409+
<ul><li><a href="./examples/with-amp">Google AMP</a></li></ul>
409410
</details></p>
410411

411412
Pages in `Next.js` skip the definition of the surrounding document's markup. For example, you never include `<html>`, `<body>`, etc. But we still make it possible to override that:

examples/with-amp/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
# Google AMP
3+
4+
## How to use
5+
6+
Download the example [or clone the repo](https://github.com/zeit/next.js.git):
7+
8+
```bash
9+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-amp
10+
cd with-amp
11+
```
12+
13+
Install it and run:
14+
15+
```bash
16+
npm install
17+
npm run dev
18+
```
19+
20+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
21+
22+
```bash
23+
now
24+
```
25+
26+
## The idea behind the example
27+
28+
Next.js allows the construction of custom Documents. This feature enable the usage of custom attributes and elements. In this case, AMP tags and attributes.

examples/with-amp/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "amp",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"dev": "next",
6+
"build": "next build",
7+
"start": "next start"
8+
},
9+
"dependencies": {
10+
"next": "^2.0.0-beta"
11+
},
12+
"author": "",
13+
"license": "ISC"
14+
}

examples/with-amp/pages/_document.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Document, { Head } from 'next/document'
2+
import { DOMProperty } from 'react-dom/../lib/ReactInjection'
3+
4+
DOMProperty.injectDOMPropertyConfig({
5+
Properties: { amp: DOMProperty.MUST_USE_ATTRIBUTE },
6+
isCustomAttribute: attributeName => attributeName.startsWith('amp-')
7+
})
8+
9+
export default class MyDocument extends Document {
10+
render () {
11+
const { html } = this.props
12+
return (
13+
<html amp=''>
14+
<Head>
15+
<meta charset='utf-8' />
16+
<link rel='canonical' href='/' />
17+
<meta name='viewport' content='width=device-width,minimum-scale=1' />
18+
<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>
19+
<script async src='https://cdn.ampproject.org/v0.js' />
20+
</Head>
21+
<body>
22+
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
23+
</body>
24+
</html>
25+
)
26+
}
27+
}

examples/with-amp/pages/index.js

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/with-amp/static/cat.jpg

39.2 KB
Loading

0 commit comments

Comments
 (0)