Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 9057e90

Browse files
committed
fix(styled-components): call sheet.seal() to avoid memory leaks
see vercel/next.js#6107
1 parent 5e46b36 commit 9057e90

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

pages/_document.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@ import { ServerStyleSheet } from 'styled-components'
33

44
/* eslint-disable */
55
export default class MyDocument extends Document {
6-
static getInitialProps({ renderPage }) {
6+
static async getInitialProps(ctx) {
77
const sheet = new ServerStyleSheet()
8-
const page = renderPage(App => props =>
9-
sheet.collectStyles(<App {...props} />)
10-
)
11-
const styleTags = sheet.getStyleElement()
12-
return { ...page, styleTags }
8+
const originalRenderPage = ctx.renderPage
9+
10+
try {
11+
ctx.renderPage = () =>
12+
originalRenderPage({
13+
enhanceApp: App => props => sheet.collectStyles(<App {...props} />),
14+
})
15+
16+
const initialProps = await Document.getInitialProps(ctx)
17+
return {
18+
...initialProps,
19+
styles: [...initialProps.styles, ...sheet.getStyleElement()],
20+
}
21+
} finally {
22+
sheet.seal()
23+
}
1324
}
1425

1526
render() {
16-
/*
17-
const sheet = new ServerStyleSheet()
18-
const main = sheet.collectStyles(<Main />)
19-
const styleTags = sheet.getStyleElement()
20-
*/
21-
2227
return (
2328
<html>
2429
<Head>

0 commit comments

Comments
 (0)