Skip to content

Commit 7615b44

Browse files
committed
add SSR support for styled-components
1 parent f919581 commit 7615b44

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

.babelrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": ["next/babel"],
3+
"plugins": [
4+
[
5+
"styled-components",
6+
{
7+
"ssr": true,
8+
"displayName": true,
9+
"preprocess": false
10+
}
11+
]
12+
]
13+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"swiper": "^8.2.2"
3434
},
3535
"devDependencies": {
36+
"babel-plugin-styled-components": "^2.1.1",
3637
"husky": "^8.0.0",
3738
"lint-staged": "^13.0.3",
3839
"prettier": "^2.7.1",

pages/_document.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
import { Html, Head, Main, NextScript } from 'next/document';
1+
import Document, { Html, Head, Main, NextScript } from 'next/document';
2+
import { ServerStyleSheet } from 'styled-components';
3+
import App from 'next/app';
24

3-
export default function Document() {
4-
return (
5-
<Html>
6-
<Head>
7-
<link
8-
rel='stylesheet'
9-
href='https://fonts.googleapis.com/css2?family=Assistant:wght@400;700&family=Open+Sans:wght@700&display=swap'
10-
/>
11-
</Head>
12-
<body>
13-
<Main />
14-
<NextScript />
15-
</body>
16-
</Html>
17-
);
5+
export default class MyDocument extends Document {
6+
static getInitalProps({ renderPage }) {
7+
const sheet = new ServerStyleSheet();
8+
const page = renderPage(
9+
App => props => sheet.collectStyles(<App {...props} />)
10+
);
11+
const styleTags = sheet.getStyleElement();
12+
return { ...page, styleTags };
13+
}
14+
15+
render() {
16+
return (
17+
<Html>
18+
<Head>
19+
<link
20+
rel='stylesheet'
21+
href='https://fonts.googleapis.com/css2?family=Assistant:wght@400;700&family=Open+Sans:wght@700&display=swap'
22+
/>
23+
{this.props.styleTags}
24+
</Head>
25+
<body>
26+
<Main />
27+
<NextScript />
28+
</body>
29+
</Html>
30+
);
31+
}
1832
}

yarn.lock

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,17 @@ babel-plugin-polyfill-regenerator@^0.3.1:
14391439
lodash "^4.17.11"
14401440
picomatch "^2.3.0"
14411441

1442+
babel-plugin-styled-components@^2.1.1:
1443+
version "2.1.1"
1444+
resolved "https://registry.yarnpkg.com/babel-plugin-styled-components/-/babel-plugin-styled-components-2.1.1.tgz#cd977cc0ff8410d5cbfdd142e42576e9c8794b87"
1445+
integrity sha512-c8lJlszObVQPguHkI+akXv8+Jgb9Ccujx0EetL7oIvwU100LxO6XAGe45qry37wUL40a5U9f23SYrivro2XKhA==
1446+
dependencies:
1447+
"@babel/helper-annotate-as-pure" "^7.16.0"
1448+
"@babel/helper-module-imports" "^7.16.0"
1449+
babel-plugin-syntax-jsx "^6.18.0"
1450+
lodash "^4.17.21"
1451+
picomatch "^2.3.0"
1452+
14421453
babel-plugin-syntax-jsx@^6.18.0:
14431454
version "6.18.0"
14441455
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
@@ -2595,7 +2606,7 @@ lodash.sortby@^4.7.0:
25952606
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
25962607
integrity sha512-HDWXG8isMntAyRF5vZ7xKuEvOhT4AhlRt/3czTSjvGUxjYCBVRQY48ViDHyfYz9VIoBkW4TMGQNapx+l3RUwdA==
25972608

2598-
lodash@^4.17.11, lodash@^4.17.20:
2609+
lodash@^4.17.11, lodash@^4.17.20, lodash@^4.17.21:
25992610
version "4.17.21"
26002611
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
26012612
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

0 commit comments

Comments
 (0)