-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prerender시점에서 css도 포함해서 렌더링하도록 설정
- Loading branch information
Showing
2 changed files
with
52 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,54 @@ | ||
import { Html, Head, Main, NextScript } from 'next/document'; | ||
import Document, { DocumentContext } from 'next/document'; | ||
import { ServerStyleSheet } from 'styled-components'; | ||
|
||
export default function Document() { | ||
return ( | ||
<Html lang="ko"> | ||
<Head> | ||
<script | ||
defer | ||
src="https://developers.kakao.com/sdk/js/kakao.min.js" | ||
></script> | ||
</Head> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
<div id="modal"></div> | ||
</body> | ||
</Html> | ||
); | ||
class MyDocument extends Document { | ||
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()} | ||
</>, | ||
], | ||
}; | ||
} catch (error) { | ||
throw error; | ||
} finally { | ||
sheet.seal(); | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<Html lang="ko"> | ||
<Head> | ||
<script | ||
defer | ||
src="https://developers.kakao.com/sdk/js/kakao.min.js" | ||
></script> | ||
</Head> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
<div id="modal"></div> | ||
</body> | ||
</Html> | ||
); | ||
} | ||
} | ||
|
||
export default MyDocument; |