From 4f40115e11e8a0c01ea89167c721a9637fe78b39 Mon Sep 17 00:00:00 2001 From: Maja Komel Date: Thu, 23 Feb 2023 23:26:27 +0100 Subject: [PATCH] Update document.js --- pages/_document.js | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/pages/_document.js b/pages/_document.js index a3615cc17..c645f3c12 100644 --- a/pages/_document.js +++ b/pages/_document.js @@ -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()) - 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(), + }) - render () { - return ( - - - {this.props.styleTags} - - -
- - - - ) + const initialProps = await Document.getInitialProps(ctx) + return { + ...initialProps, + styles: [initialProps.styles, sheet.getStyleElement()], + } + } finally { + sheet.seal() + } } }