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

Fix document styles with react 18 #35760

Merged
merged 2 commits into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 17 additions & 7 deletions packages/next/server/render.tsx
Expand Up @@ -1431,23 +1431,33 @@ export async function renderToHTML(
})
}

const styles = jsxStyleRegistry.styles()
jsxStyleRegistry.flush()
const hasDocumentGetInitialProps = !(
isServerComponent ||
process.browser ||
!Document.getInitialProps
)

const documentInitialPropsRes =
isServerComponent || process.browser || !Document.getInitialProps
? {}
: await documentInitialProps()
const documentInitialPropsRes = hasDocumentGetInitialProps
? await documentInitialProps()
: {}
if (documentInitialPropsRes === null) return null

const { docProps } = (documentInitialPropsRes as any) || {}
const documentElement = () => {
if (isServerComponent || process.browser) {
return (Document as any)()
}

const { docProps } = (documentInitialPropsRes as any) || {}
return <Document {...htmlProps} {...docProps} />
}
let styles

if (hasDocumentGetInitialProps) {
styles = docProps.styles
} else {
styles = jsxStyleRegistry.styles()
jsxStyleRegistry.flush()
}

return {
bodyResult,
Expand Down
8 changes: 7 additions & 1 deletion test/development/basic/styled-components.test.ts
Expand Up @@ -2,7 +2,7 @@ import { join } from 'path'
import webdriver from 'next-webdriver'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'

describe('styled-components SWC transform', () => {
let next: NextInstance
Expand Down Expand Up @@ -69,4 +69,10 @@ describe('styled-components SWC transform', () => {
)
).toBe('rgb(255, 255, 255)')
})

it('should contain styles in initial HTML', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('background:transparent')
expect(html).toContain('color:white')
})
})