Skip to content

Commit ca521b3

Browse files
lucasfelicianotimneutkens
authored andcommitted
Manually call sheet.seal() to avoid memory leaks on with-styled-components example (#6107)
I was noticing some bad memory leak on my company's application and I ended up finding this github issue ( styled-components/styled-components#1624 ) . This comment ( styled-components/styled-components#1624 (comment) ) caught my attention, which lead to this other issue on the repository of styled components website ( styled-components/styled-components-website#329 ) After applying the changes on my project I noticed a huge improvement on memory consumption. So would be nice to update the example or start a discussion on how to solve this properly
1 parent 9112f63 commit ca521b3

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

examples/with-styled-components/pages/_document.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ import { ServerStyleSheet } from 'styled-components'
44
export default class MyDocument extends Document {
55
static async getInitialProps (ctx) {
66
const sheet = new ServerStyleSheet()
7-
87
const originalRenderPage = ctx.renderPage
9-
ctx.renderPage = () =>
10-
originalRenderPage({
11-
enhanceApp: App => props => sheet.collectStyles(<App {...props} />)
12-
})
138

14-
const initialProps = await Document.getInitialProps(ctx)
15-
return {
16-
...initialProps,
17-
styles: [...initialProps.styles, ...sheet.getStyleElement()]
9+
try {
10+
ctx.renderPage = () =>
11+
originalRenderPage({
12+
enhanceApp: App => props => sheet.collectStyles(<App {...props} />)
13+
})
14+
15+
const initialProps = await Document.getInitialProps(ctx)
16+
return {
17+
...initialProps,
18+
styles: [...initialProps.styles, ...sheet.getStyleElement()]
19+
}
20+
} finally {
21+
sheet.seal()
1822
}
1923
}
2024
}

0 commit comments

Comments
 (0)