Skip to content

Commit fe012ff

Browse files
authored
Fix document styles with react 18 (#35760)
This is a follow-up to #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: #35758 x-ref: #35736
1 parent 2f11413 commit fe012ff

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

packages/next/server/render.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,23 +1431,33 @@ export async function renderToHTML(
14311431
})
14321432
}
14331433

1434-
const styles = jsxStyleRegistry.styles()
1435-
jsxStyleRegistry.flush()
1434+
const hasDocumentGetInitialProps = !(
1435+
isServerComponent ||
1436+
process.browser ||
1437+
!Document.getInitialProps
1438+
)
14361439

1437-
const documentInitialPropsRes =
1438-
isServerComponent || process.browser || !Document.getInitialProps
1439-
? {}
1440-
: await documentInitialProps()
1440+
const documentInitialPropsRes = hasDocumentGetInitialProps
1441+
? await documentInitialProps()
1442+
: {}
14411443
if (documentInitialPropsRes === null) return null
14421444

1445+
const { docProps } = (documentInitialPropsRes as any) || {}
14431446
const documentElement = () => {
14441447
if (isServerComponent || process.browser) {
14451448
return (Document as any)()
14461449
}
14471450

1448-
const { docProps } = (documentInitialPropsRes as any) || {}
14491451
return <Document {...htmlProps} {...docProps} />
14501452
}
1453+
let styles
1454+
1455+
if (hasDocumentGetInitialProps) {
1456+
styles = docProps.styles
1457+
} else {
1458+
styles = jsxStyleRegistry.styles()
1459+
jsxStyleRegistry.flush()
1460+
}
14511461

14521462
return {
14531463
bodyResult,

test/development/basic/styled-components.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { join } from 'path'
22
import webdriver from 'next-webdriver'
33
import { createNext, FileRef } from 'e2e-utils'
44
import { NextInstance } from 'test/lib/next-modes/base'
5-
import { fetchViaHTTP } from 'next-test-utils'
5+
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
66

77
describe('styled-components SWC transform', () => {
88
let next: NextInstance
@@ -69,4 +69,10 @@ describe('styled-components SWC transform', () => {
6969
)
7070
).toBe('rgb(255, 255, 255)')
7171
})
72+
73+
it('should contain styles in initial HTML', async () => {
74+
const html = await renderViaHTTP(next.url, '/')
75+
expect(html).toContain('background:transparent')
76+
expect(html).toContain('color:white')
77+
})
7278
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy