Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced: SSR styles encapsulated in a Higher-Order Component #9

Draft
wants to merge 1 commit into
base: fix/ssr-styles
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/features/ui/lib/withStyledComponents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable @next/next/no-document-import-in-page */
import type * as NextDocumentTypes from 'next/document'
import NextDocument from 'next/document'
import { ServerStyleSheet } from 'styled-components'

type TDocument = React.ComponentType<NextDocumentTypes.DocumentProps> & {
getInitialProps?: (
ctx: NextDocumentTypes.DocumentContext
) => Promise<NextDocumentTypes.DocumentInitialProps>
}

/**
* A Higher-Order Component to add Server-Side Rendering capabilities
* to a Next.js Document (_document.tsx) component.
*/
const withServerSideStyles = (document: TDocument) => {
// Resolve getInitialProps either from the custom document, or from Next.js' default.
const getInitialProps =
// eslint-disable-next-line @typescript-eslint/unbound-method
document.getInitialProps ?? NextDocument.getInitialProps

// Implement getInitialProps on behalf of document.
document.getInitialProps = async (ctx) => {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage

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

const initialProps = await getInitialProps(ctx)
const styles = [initialProps.styles, sheet.getStyleElement()]

return { ...initialProps, styles }
} finally {
sheet.seal()
}
}

return document
}

export { withServerSideStyles }
39 changes: 5 additions & 34 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import type { DocumentContext, DocumentInitialProps } from 'next/document'
import NextDocument, { Html, Head, Main, NextScript } from 'next/document'
import { ServerStyleSheet } from 'styled-components'
/* eslint-disable @next/next/no-page-custom-font */
import { Html, Head, Main, NextScript } from 'next/document'

type TDocument = {
(): JSX.Element
getInitialProps: (ctx: DocumentContext) => Promise<DocumentInitialProps>
}
import { withServerSideStyles } from '~/features/ui/lib/withStyledComponents'

const Document: TDocument = () => (
const Document = () => (
<Html>
<Head>
<link rel="preconnect" href="https://fonts.googleapis.com" />
Expand All @@ -24,29 +20,4 @@ const Document: TDocument = () => (
</Html>
)

Document.getInitialProps = async (ctx) => {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage

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

const initialProps = await NextDocument.getInitialProps(ctx)
return {
...initialProps,
styles: [
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>,
],
}
} finally {
sheet.seal()
}
}

export default Document
export default withServerSideStyles(Document)