Skip to content

Commit a935796

Browse files
authored
better ssr (#97)
1 parent 579f595 commit a935796

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export default App
3939
### Theming
4040
Components use Hack4Impact's Design theme by default (`src/theme.js`) but you can provide your own theme using styled-component's [<ThemeProvider>](https://styled-components.com/docs/advanced). You can either fully replace the theme or replace parts of the theme by placing your custom theme through a prop to our components or through `<ThemeProvider>` in the root of the application.
4141

42+
### Server-side Rendering
43+
44+
This library depends on [styled-components](https://styled-components.com/). Thus, if you are server rendering React or using a framework like next.js or gatsby, follow styled-component's [server rendering setup](https://styled-components.com/docs/advanced#server-side-rendering).
4245

4346
## Local Development
4447
We use Storybook to hot-reload and test our components during development. You can edit the [`playground/Playground.js`](playground/Playground.js) file to import and test the components you are working on. Please do not commit any work on the file so other developers can use the same file to test.

docs/pages/_document.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11
import Document, {
22
Html, Head, Main, NextScript,
33
} from 'next/document';
4+
import { ServerStyleSheet } from 'styled-components';
5+
46

57
class DocSite extends Document {
8+
static async getInitialProps(ctx) {
9+
const sheet = new ServerStyleSheet();
10+
const originalRenderPage = ctx.renderPage;
11+
12+
try {
13+
ctx.renderPage = () => originalRenderPage({
14+
enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />),
15+
});
16+
17+
const initialProps = await Document.getInitialProps(ctx);
18+
return {
19+
...initialProps,
20+
styles: (
21+
<>
22+
{initialProps.styles}
23+
{sheet.getStyleElement()}
24+
</>
25+
),
26+
};
27+
} finally {
28+
sheet.seal();
29+
}
30+
}
31+
632
render() {
733
return (
834
<Html lang="en">

docs/pages/components/index.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export default App
4343
#### Theming
4444
Components use Hack4Impact's Design theme by default (`src/theme.js`) but you can provide your own theme using styled-component's [ThemeProvider](https://styled-components.com/docs/advanced). You can either fully replace the theme or replace parts of the theme by placing your custom theme through a prop to our components or through `<ThemeProvider>` in the root of the application.
4545

46+
#### Server-side Rendering
47+
48+
This library depends on [styled-components](https://styled-components.com/). Thus, if you are server rendering React or using a framework like next.js or gatsby, follow styled-component's [server rendering setup](https://styled-components.com/docs/advanced#server-side-rendering).
49+
4650
### Local Development
4751
We use Storybook to hot-reload and test our components during development. You can edit the `playground/Playground.js` file to import and test the components you are working on. Please do not commit any work on the file so other developers can use the same file to test.
4852

0 commit comments

Comments
 (0)