url-minify/frontend/pages/_document.js

31 lines
729 B
JavaScript
Raw Normal View History

import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'
2022-02-01 15:50:14 +00:00
export default class MyDocument extends Document {
2022-02-13 08:28:33 +00:00
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
2022-02-01 15:50:14 +00:00
2022-02-13 08:28:33 +00:00
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})
2022-02-01 15:50:14 +00:00
const initialProps = await Document.getInitialProps(ctx)
2022-02-13 08:28:33 +00:00
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
2022-02-13 08:28:33 +00:00
} finally {
sheet.seal()
2022-02-01 15:50:14 +00:00
}
2022-02-13 08:28:33 +00:00
}
}