Styled Components 렌더링 이슈
상황: index.tsx에서는 적용되었으며, theme provider(스타일을 모든 컴포넌트에 속성으로 넘기는 기능)으로 색상 적용에 성공함
문제1: 렌더링 초기에 잠깐 스타일이 적용되지 않은 화면이 렌더링 됨
시도1: Styled Components 서버사이드 렌더링 기능 적용
// _document.tsx
...
static async getInitialProps(ctx: DocumentContext) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles} {sheet.getStyleElement()}
</>
),
};
} finally {
sheet.seal();
}
}
...
//next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
compiler: {
styledComponents: true,
},
};
module.exports = nextConfig;
결과 : 정상적으로 스타일 데이터를 받아온 이후 렌더링 됨.
추가 문제 : 렌더링에 시간이 다소 소요되어, 유저에게 렌더링을 알리기 위한 로더 UI 추가 필요.
문제2: pages/_document.tsx에 적용한 레이아웃 컴포넌트에 스타일이 적용되지 않음
뚝딱뚝딱