From a91fc2f776336d770fbb92caead97624ee5c9059 Mon Sep 17 00:00:00 2001 From: Marija Najdova Date: Tue, 20 Jul 2021 13:01:11 +0200 Subject: [PATCH] [examples] Update gatsby example to use custom plugin (#27357) --- examples/gatsby/gatsby-config.js | 2 +- examples/gatsby/package.json | 1 + .../gatsby-browser.js | 10 +++++++ .../gatsby-plugin-mui-emotion/gatsby-ssr.js | 29 +++++++++++++++++++ .../getEmotionCache.js | 5 ++++ .../gatsby-plugin-mui-emotion/package.json | 3 ++ 6 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-browser.js create mode 100644 examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-ssr.js create mode 100644 examples/gatsby/plugins/gatsby-plugin-mui-emotion/getEmotionCache.js create mode 100644 examples/gatsby/plugins/gatsby-plugin-mui-emotion/package.json diff --git a/examples/gatsby/gatsby-config.js b/examples/gatsby/gatsby-config.js index 33f09fde769195..1c57408c4e725a 100644 --- a/examples/gatsby/gatsby-config.js +++ b/examples/gatsby/gatsby-config.js @@ -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', diff --git a/examples/gatsby/package.json b/examples/gatsby/package.json index 8b3f35ccdf48bb..b889f4bdc2c3cf 100644 --- a/examples/gatsby/package.json +++ b/examples/gatsby/package.json @@ -4,6 +4,7 @@ "private": true, "dependencies": { "@emotion/react": "latest", + "@emotion/server": "latest", "@emotion/styled": "latest", "@material-ui/core": "next", "gatsby": "latest", diff --git a/examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-browser.js b/examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-browser.js new file mode 100644 index 00000000000000..f61e0f298f8bb3 --- /dev/null +++ b/examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-browser.js @@ -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 {element}; +}; diff --git a/examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-ssr.js b/examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-ssr.js new file mode 100644 index 00000000000000..dc53a1d535d636 --- /dev/null +++ b/examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-ssr.js @@ -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({bodyComponent}), + ); + + setHeadComponents( + emotionStyles.styles.map((style) => ( +