Skip to content

Commit 15889fe

Browse files
committed
fix: Configure Styled Components SSR with SWC
Switched from Babel to SWC for Styled Components SSR transformation by removing Babel config/plugin, ensuring compiler.styledComponents is true in next.config.js, and adding/integrating the StyledComponentsRegistry.
1 parent e726880 commit 15889fe

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

app/layout.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Metadata } from 'next';
22
import { lexend, inter, manrope, workSans, ibmPlex } from '../src/styles/fonts';
33
import ClientLayout from '@components/ClientLayout';
44
import { ThemeProvider } from '../src/providers/ThemeProvider';
5+
import StyledComponentsRegistry from '@providers/StyledComponentsRegistry';
56
import '../src/styles/globals.css';
67
import '../src/styles/print.css';
78
import '@mantine/core/styles.css';
@@ -20,9 +21,11 @@ export default function RootLayout({
2021
return (
2122
<html lang="en" className={`${lexend.variable} ${inter.variable} ${manrope.variable} ${workSans.variable} ${ibmPlex.variable}`}>
2223
<body style={{ overflowX: 'hidden' }}>
23-
<ThemeProvider>
24-
<ClientLayout>{children}</ClientLayout>
25-
</ThemeProvider>
24+
<StyledComponentsRegistry>
25+
<ThemeProvider>
26+
<ClientLayout>{children}</ClientLayout>
27+
</ThemeProvider>
28+
</StyledComponentsRegistry>
2629
</body>
2730
</html>
2831
);

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@
116116
"@vitest/browser": "^3.0.8",
117117
"@vitest/coverage-v8": "^3.0.8",
118118
"babel-loader": "^10.0.0",
119-
"babel-plugin-styled-components": "^2.1.4",
120119
"concurrently": "^9.1.2",
121120
"eslint": "^9",
122121
"eslint-config-next": "15.2.1",

pnpm-lock.yaml

Lines changed: 0 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client'
2+
3+
import React, { useState } from 'react'
4+
import { useServerInsertedHTML } from 'next/navigation'
5+
import { ServerStyleSheet, StyleSheetManager } from 'styled-components'
6+
7+
export default function StyledComponentsRegistry({
8+
children,
9+
}: {
10+
children: React.ReactNode
11+
}) {
12+
// Only create stylesheet once with lazy initial state
13+
// x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
14+
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet())
15+
16+
useServerInsertedHTML(() => {
17+
const styles = styledComponentsStyleSheet.getStyleElement()
18+
styledComponentsStyleSheet.instance.clearTag()
19+
return <>{styles}</>
20+
})
21+
22+
if (typeof window !== 'undefined') return <>{children}</>
23+
24+
return (
25+
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}>
26+
{children}
27+
</StyleSheetManager>
28+
)
29+
}

0 commit comments

Comments
 (0)