Unverified Commit fe012ff9 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix document styles with react 18 (#35760)

This is a follow-up to https://github.com/vercel/next.js/pull/35736 that ensures we use the styles from `_document.getInitialProps` instead of only applying the `styled-jsx` styles. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes: https://github.com/vercel/next.js/issues/35758
x-ref: https://github.com/vercel/next.js/pull/35736
parent 2f114134
...@@ -1431,23 +1431,33 @@ export async function renderToHTML( ...@@ -1431,23 +1431,33 @@ export async function renderToHTML(
}) })
} }
const styles = jsxStyleRegistry.styles() const hasDocumentGetInitialProps = !(
jsxStyleRegistry.flush() isServerComponent ||
process.browser ||
!Document.getInitialProps
)
const documentInitialPropsRes = const documentInitialPropsRes = hasDocumentGetInitialProps
isServerComponent || process.browser || !Document.getInitialProps ? await documentInitialProps()
? {} : {}
: await documentInitialProps()
if (documentInitialPropsRes === null) return null if (documentInitialPropsRes === null) return null
const { docProps } = (documentInitialPropsRes as any) || {}
const documentElement = () => { const documentElement = () => {
if (isServerComponent || process.browser) { if (isServerComponent || process.browser) {
return (Document as any)() return (Document as any)()
} }
const { docProps } = (documentInitialPropsRes as any) || {}
return <Document {...htmlProps} {...docProps} /> return <Document {...htmlProps} {...docProps} />
} }
let styles
if (hasDocumentGetInitialProps) {
styles = docProps.styles
} else {
styles = jsxStyleRegistry.styles()
jsxStyleRegistry.flush()
}
return { return {
bodyResult, bodyResult,
......
...@@ -2,7 +2,7 @@ import { join } from 'path' ...@@ -2,7 +2,7 @@ import { join } from 'path'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { createNext, FileRef } from 'e2e-utils' import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' 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', () => { describe('styled-components SWC transform', () => {
let next: NextInstance let next: NextInstance
...@@ -69,4 +69,10 @@ describe('styled-components SWC transform', () => { ...@@ -69,4 +69,10 @@ describe('styled-components SWC transform', () => {
) )
).toBe('rgb(255, 255, 255)') ).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')
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment