Skip to content

Commit

Permalink
[examples] Update gatsby example to use custom plugin (#27357)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnajdova authored Jul 20, 2021
1 parent 19d81a3 commit a91fc2f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/gatsby/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
'gatsby-plugin-react-helmet',
// If you want to use styled components you should add the plugin here.
// 'gatsby-plugin-styled-components',
'gatsby-plugin-emotion',
'gatsby-plugin-mui-emotion',
],
siteMetadata: {
title: 'My page',
Expand Down
1 change: 1 addition & 0 deletions examples/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"@emotion/react": "latest",
"@emotion/server": "latest",
"@emotion/styled": "latest",
"@material-ui/core": "next",
"gatsby": "latest",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable import/prefer-default-export */
import React from 'react';
import { CacheProvider } from '@emotion/react';
import getEmotionCache from './getEmotionCache';

const cache = getEmotionCache();

export const wrapRootElement = ({ element }) => {
return <CacheProvider value={cache}>{element}</CacheProvider>;
};
29 changes: 29 additions & 0 deletions examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable import/prefer-default-export */
import * as React from 'react';
import { CacheProvider } from '@emotion/react';
import createEmotionServer from '@emotion/server/create-instance';
import { renderToString } from 'react-dom/server';
import getEmotionCache from './getEmotionCache';

export const replaceRenderer = ({ bodyComponent, setHeadComponents, replaceBodyHTMLString }) => {
const cache = getEmotionCache();
const { extractCriticalToChunks } = createEmotionServer(cache);

const emotionStyles = extractCriticalToChunks(
renderToString(<CacheProvider value={cache}>{bodyComponent}</CacheProvider>),
);

setHeadComponents(
emotionStyles.styles.map((style) => (
<style
data-emotion={`${style.key} ${style.ids.join(' ')}`}
key={style.key}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: style.css }}
/>
)),
);

// render the result from `extractCritical`
replaceBodyHTMLString(emotionStyles.html);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import createCache from '@emotion/cache';

export default function getEmotionCache() {
return createCache({ key: 'css', prepend: true });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "gatsby-plugin-mui-emotion"
}

0 comments on commit a91fc2f

Please sign in to comment.