Skip to content

Commit

Permalink
Update document.js
Browse files Browse the repository at this point in the history
  • Loading branch information
majakomel committed Feb 23, 2023
1 parent 8ae9d18 commit 4f40115
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions pages/_document.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import React from 'react'
import Document, { Html, Head, Main, NextScript } from 'next/document'
// updated based on documentation: https://github.com/vercel/next.js/blob/canary/examples/with-styled-components/pages/_document.tsx
import Document, { DocumentContext, Html, Head, Main, NextScript } from 'next/document'
import { ServerStyleSheet } from 'styled-components'

export default class MyDocument extends Document {
static async getInitialProps (ctx) {
const sheet = new ServerStyleSheet()
const page = ctx.renderPage(App => props => sheet.collectStyles(<App {...props} />))
const styleTags = sheet.getStyleElement()
const initialProps = await Document.getInitialProps(ctx)
return { ...page, styleTags, ...initialProps }
}
const originalRenderPage = ctx.renderPage

try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})

render () {
return (
<Html>
<Head>
{this.props.styleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: [initialProps.styles, sheet.getStyleElement()],
}
} finally {
sheet.seal()
}
}
}

1 comment on commit 4f40115

@vercel
Copy link

@vercel vercel bot commented on 4f40115 Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

explorer – ./

explorer-ooni1.vercel.app
explorer-one.vercel.app
explorer-git-master-ooni1.vercel.app

Please sign in to comment.