Commit 5ff2deab authored by Joe Haddad's avatar Joe Haddad Committed by Tim Neutkens

Add Html tag for custom Document (#6372)

This introduces a new `<Html>` tag for a custom `Document` so that we can correctly toggle the `amp` flag (among other things in the future ... maybe).

This is already "tested" through every other test & the AMP validator -- but let me know if we want explicit tests.
parent ecbecb9e
...@@ -1235,7 +1235,7 @@ Pages in `Next.js` skip the definition of the surrounding document's markup. For ...@@ -1235,7 +1235,7 @@ Pages in `Next.js` skip the definition of the surrounding document's markup. For
// Event handlers like onClick can't be added to this file // Event handlers like onClick can't be added to this file
// ./pages/_document.js // ./pages/_document.js
import Document, { Head, Main, NextScript } from 'next/document' import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document { class MyDocument extends Document {
static async getInitialProps(ctx) { static async getInitialProps(ctx) {
...@@ -1245,7 +1245,7 @@ class MyDocument extends Document { ...@@ -1245,7 +1245,7 @@ class MyDocument extends Document {
render() { render() {
return ( return (
<html> <Html>
<Head> <Head>
<style>{`body { margin: 0 } /* custom! */`}</style> <style>{`body { margin: 0 } /* custom! */`}</style>
</Head> </Head>
...@@ -1253,7 +1253,7 @@ class MyDocument extends Document { ...@@ -1253,7 +1253,7 @@ class MyDocument extends Document {
<Main /> <Main />
<NextScript /> <NextScript />
</body> </body>
</html> </Html>
) )
} }
} }
......
...@@ -26,14 +26,31 @@ export default class Document extends Component { ...@@ -26,14 +26,31 @@ export default class Document extends Component {
} }
} }
render () { render() {
return <html amp={this.props.amphtml ? '' : null}> return (
<Html>
<Head /> <Head />
<body> <body>
<Main /> <Main />
<NextScript /> <NextScript />
</body> </body>
</html> </Html>
)
}
}
export class Html extends Component {
static contextTypes = {
_documentProps: PropTypes.any,
}
static propTypes = {
children: PropTypes.node.isRequired,
}
render() {
const { amphtml } = this.context._documentProps
return <html amp={amphtml ? '' : null}>{this.props.children}</html>
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment