Unverified Commit 64a3697d authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

[Docs] Update examples to favour functional _document (#39871)



## Changes

- Migrate class based `_document`s to functional documents 
- Removed `_document.js` where default
- Removed `MyDocument.getInitialProps` where default

## Motivation

This removes some boilerplate and prepares examples for server components.

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent b8562839
import Document, { import { Html, Head, Main, NextScript } from 'next/document'
Html,
Head,
Main,
NextScript,
DocumentContext,
} from 'next/document'
class MyDocument extends Document { export default function Document() {
static async getInitialProps(ctx: DocumentContext) { return (
const initialProps = await Document.getInitialProps(ctx) <Html>
return { ...initialProps } <Head>
} <link
rel="stylesheet"
render() { href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;700&family=IBM+Plex+Sans:wght@400;500;600&display=swap"
return ( />
<Html> </Head>
<Head> <body>
<link <Main />
rel="stylesheet" <NextScript />
href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;700&family=IBM+Plex+Sans:wght@400;500;600&display=swap" </body>
/> </Html>
</Head> )
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
} }
export default MyDocument
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { import { Html, Head, Main, NextScript } from 'next/document'
Html,
Head,
Main,
NextScript,
DocumentContext,
} from 'next/document'
class MyDocument extends Document { export default function Document() {
static async getInitialProps(ctx: DocumentContext) { const meta = {
const initialProps = await Document.getInitialProps(ctx) title: 'Next.js Blog Starter Kit',
return { ...initialProps } description: 'Clone and deploy your own Next.js portfolio in minutes.',
image: 'https://assets.vercel.com/image/upload/q_auto/front/vercel/dps.png',
} }
render() { return (
const meta = { <Html lang="en">
title: 'Next.js Blog Starter Kit', <Head>
description: 'Clone and deploy your own Next.js portfolio in minutes.', <meta name="robots" content="follow, index" />
image: <meta name="description" content={meta.description} />
'https://assets.vercel.com/image/upload/q_auto/front/vercel/dps.png', <meta property="og:site_name" content={meta.title} />
} <meta property="og:description" content={meta.description} />
<meta property="og:title" content={meta.title} />
return ( <meta property="og:image" content={meta.image} />
<Html lang="en"> <meta name="twitter:card" content="summary_large_image" />
<Head> <meta name="twitter:site" content="@yourname" />
<meta name="robots" content="follow, index" /> <meta name="twitter:title" content={meta.title} />
<meta name="description" content={meta.description} /> <meta name="twitter:description" content={meta.description} />
<meta property="og:site_name" content={meta.title} /> <meta name="twitter:image" content={meta.image} />
<meta property="og:description" content={meta.description} /> </Head>
<meta property="og:title" content={meta.title} /> <body>
<meta property="og:image" content={meta.image} /> <Main />
<meta name="twitter:card" content="summary_large_image" /> <NextScript />
<meta name="twitter:site" content="@yourname" /> </body>
<meta name="twitter:title" content={meta.title} /> </Html>
<meta name="twitter:description" content={meta.description} /> )
<meta name="twitter:image" content={meta.image} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
} }
export default MyDocument
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html lang="en">
<Html lang="en"> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document({ css }) {
render() { return (
return ( <Html>
<Html> <Head>
<Head> <style dangerouslySetInnerHTML={{ __html: css }} />
<style dangerouslySetInnerHTML={{ __html: this.props.css }} /> </Head>
</Head> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
import { FB_PIXEL_ID } from '../lib/fpixel' import { FB_PIXEL_ID } from '../lib/fpixel'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html>
<Html> <Head>
<Head> <noscript>
<noscript> <img
<img height="1"
height="1" width="1"
width="1" style={{ display: 'none' }}
style={{ display: 'none' }} src={`https://www.facebook.com/tr?id=${FB_PIXEL_ID}&ev=PageView&noscript=1`}
src={`https://www.facebook.com/tr?id=${FB_PIXEL_ID}&ev=PageView&noscript=1`} />
/> </noscript>
</noscript> </Head>
</Head> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
import { useAmp } from 'next/amp' import { useAmp } from 'next/amp'
import { GA_TRACKING_ID } from '../lib/gtag' import { GA_TRACKING_ID } from '../lib/gtag'
...@@ -10,63 +10,61 @@ function AmpWrap({ ampOnly, nonAmp }) { ...@@ -10,63 +10,61 @@ function AmpWrap({ ampOnly, nonAmp }) {
return !isAmp && nonAmp return !isAmp && nonAmp
} }
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html>
<Html> <Head />
<Head /> <body>
<body> <Main />
<Main /> <NextScript />
<NextScript />
{/* AMP - Google Analytics */} {/* AMP - Google Analytics */}
<AmpWrap <AmpWrap
ampOnly={ ampOnly={
<AmpAnalytics <AmpAnalytics
type="googleanalytics" type="googleanalytics"
script={{ script={{
vars: { vars: {
account: GA_TRACKING_ID, account: GA_TRACKING_ID,
gtag_id: GA_TRACKING_ID, gtag_id: GA_TRACKING_ID,
config: { config: {
[GA_TRACKING_ID]: { groups: 'default' }, [GA_TRACKING_ID]: { groups: 'default' },
},
}, },
triggers: { },
trackPageview: { triggers: {
on: 'visible', trackPageview: {
request: 'pageview', on: 'visible',
}, request: 'pageview',
}, },
}} },
/> }}
} />
/> }
/>
{/* Non-AMP - Google Analytics */} {/* Non-AMP - Google Analytics */}
<AmpWrap <AmpWrap
nonAmp={ nonAmp={
<> <>
<script <script
async async
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`} src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
/> />
<script <script
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: ` __html: `
window.dataLayer = window.dataLayer || []; window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);} function gtag(){dataLayer.push(arguments);}
gtag('js', new Date()); gtag('js', new Date());
gtag('config', '${GA_TRACKING_ID}'); gtag('config', '${GA_TRACKING_ID}');
`, `,
}} }}
/> />
</> </>
} }
/> />
</body> </body>
</Html> </Html>
) )
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
import { GTM_ID } from '../lib/gtm' import { GTM_ID } from '../lib/gtm'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html>
<Html> <Head />
<Head /> <body>
<body> <noscript>
<noscript> <iframe
<iframe src={`https://www.googletagmanager.com/ns.html?id=${GTM_ID}`}
src={`https://www.googletagmanager.com/ns.html?id=${GTM_ID}`} height="0"
height="0" width="0"
width="0" style={{ display: 'none', visibility: 'hidden' }}
style={{ display: 'none', visibility: 'hidden' }} />
/> </noscript>
</noscript> <Main />
<Main /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document { export default function Document() {
static async getInitialProps(ctx) { return (
const initialProps = await Document.getInitialProps(ctx) <Html>
return { ...initialProps } <Head>
} <link rel="manifest" href="/manifest.json" />
</Head>
render() { <body>
return ( <Main />
<Html> <NextScript />
<Head> </body>
<link rel="manifest" href="/manifest.json" /> </Html>
</Head> )
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
} }
export default MyDocument
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
static async getInitialProps(ctx) { return (
const initialProps = await Document.getInitialProps(ctx) <Html lang="en">
return { ...initialProps } <Head>
} <link
rel="stylesheet"
render() { href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css"
return ( integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy"
<Html lang="en"> crossOrigin="anonymous"
<Head> />
<link <style>{`
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css"
integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy"
crossOrigin="anonymous"
/>
<style>{`
.page { .page {
height: 100vh; height: 100vh;
} }
`}</style> `}</style>
</Head> </Head>
<body> <body>
<Main /> <Main />
<NextScript /> <NextScript />
</body> </body>
</Html> </Html>
) )
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document { export default function Document() {
render() { return (
return ( <Html>
<Html> <Head />
<Head /> <body>
<body> <Main />
<Main /> {/* Here we will mount our modal portal */}
{/* Here we will mount our modal portal */} <div id="modal" />
<div id="modal" /> <NextScript />
<NextScript /> </body>
</body> </Html>
</Html> )
)
}
} }
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
import { ServerStyleSheet } from 'styled-components' import { ServerStyleSheet } from 'styled-components'
export default class MyDocument extends Document { export default function Document() {
render() { const sheet = new ServerStyleSheet()
const sheet = new ServerStyleSheet() const main = sheet.collectStyles(<Main />)
const main = sheet.collectStyles(<Main />) const styleTags = sheet.getStyleElement()
const styleTags = sheet.getStyleElement()
return ( return (
<Html> <Html>
<Head>{styleTags}</Head> <Head>{styleTags}</Head>
<body> <body>
<div className="root">{main}</div> <div className="root">{main}</div>
<NextScript /> <NextScript />
</body> </body>
</Html> </Html>
) )
}
} }
import crypto from 'crypto' import crypto from 'crypto'
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
const cspHashOf = (text) => { const cspHashOf = (text) => {
const hash = crypto.createHash('sha256') const hash = crypto.createHash('sha256')
hash.update(text) hash.update(text)
return `'sha256-${hash.digest('base64')}'` return `'sha256-${hash.digest('base64')}'`
} }
export default class MyDocument extends Document { export default function Document() {
render() { let csp = `default-src 'self'; script-src 'self' ${cspHashOf(
let csp = `default-src 'self'; script-src 'self' ${cspHashOf( NextScript.getInlineScriptSource(this.props)
)}`
if (process.env.NODE_ENV !== 'production') {
csp = `style-src 'self' 'unsafe-inline'; font-src 'self' data:; default-src 'self'; script-src 'unsafe-eval' 'self' ${cspHashOf(
NextScript.getInlineScriptSource(this.props) NextScript.getInlineScriptSource(this.props)
)}` )}`
if (process.env.NODE_ENV !== 'production') {
csp = `style-src 'self' 'unsafe-inline'; font-src 'self' data:; default-src 'self'; script-src 'unsafe-eval' 'self' ${cspHashOf(
NextScript.getInlineScriptSource(this.props)
)}`
}
return (
<Html>
<Head>
<meta httpEquiv="Content-Security-Policy" content={csp} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
} }
return (
<Html>
<Head>
<meta httpEquiv="Content-Security-Policy" content={csp} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
} }
import Document, { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<style dangerouslySetInnerHTML={{ __html: this.props.css }} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
import Document, { Html, Head, Main, NextScript } from 'next/document' import { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document { export default function Document() {
static async getInitialProps(ctx) { return (
const initialProps = await Document.getInitialProps(ctx) <Html lang="en">
return { ...initialProps } <Head />
} <body>
<Main />
render() { <NextScript />
return ( </body>
<Html lang="en"> </Html>
<Head /> )
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
} }
export default MyDocument
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