Skip to content

Commit 5e0be6b

Browse files
authored
Merge pull request #77 from Team-INSERT/feat/styled-components
스타일드 컴포넌트 SSR 이슈 해결
2 parents 098a9c4 + f4e0013 commit 5e0be6b

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

src/app/layout.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import Provider from "@/provider/provider.helper";
1+
import Provider from "@/provider/Provider.helper";
2+
import ServerStyleProvider from "@/provider/ServerStyleProvider";
23

34
export const metadata = {
45
title: "BSM",
@@ -13,7 +14,9 @@ export default function RootLayout({
1314
return (
1415
<html lang="en">
1516
<body>
16-
<Provider>{children}</Provider>
17+
<Provider>
18+
<ServerStyleProvider>{children}</ServerStyleProvider>
19+
</Provider>
1720
</body>
1821
</html>
1922
);

src/provider/ServerStyleProvider.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 ServerStyleProvider({
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+
// eslint-disable-next-line
20+
return <>{styles}</>;
21+
});
22+
23+
// eslint-disable-next-line
24+
if (typeof window !== "undefined") return <>{children}</>;
25+
26+
return (
27+
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}>
28+
{children}
29+
</StyleSheetManager>
30+
);
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from "react";
2+
import { ServerStyleSheet, StyleSheetManager } from "styled-components";
3+
4+
export function useStyledComponentsRegistry() {
5+
const [styledComponentsStyleSheet] = React.useState(
6+
() => new ServerStyleSheet(),
7+
);
8+
9+
const styledComponentsFlushEffect = () => {
10+
const styles = styledComponentsStyleSheet.getStyleElement();
11+
styledComponentsStyleSheet.instance.clearTag();
12+
// eslint-disable-next-line
13+
return <>{styles}</>;
14+
};
15+
16+
const StyledComponentsProvider = ({
17+
children,
18+
}: {
19+
children: React.ReactNode;
20+
}) => (
21+
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}>
22+
{children as React.ReactElement}
23+
</StyleSheetManager>
24+
);
25+
26+
return [StyledComponentsProvider, styledComponentsFlushEffect] as const;
27+
}

src/provider/provider.helper.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import React from "react";
44
import { RecoilRoot } from "recoil";
5-
import ReactQueryProvider from "./reactQueryProvider.helper";
6-
import LayoutProvider from "./layoutProvider.helper";
7-
import ApolloClientProvider from "./apolloClientProvider.helper";
5+
import ReactQueryProvider from "./ReactQueryProvider.helper";
6+
import ApolloClientProvider from "./ApolloClientProvider.helper";
7+
import LayoutProvider from "./LayoutProvider.helper";
88

99
const Provider = ({ children }: React.PropsWithChildren) => {
1010
return (

0 commit comments

Comments
 (0)