-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Update gatsby example to use custom plugin (#27357)
- Loading branch information
Showing
6 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-browser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-ssr.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
5 changes: 5 additions & 0 deletions
5
examples/gatsby/plugins/gatsby-plugin-mui-emotion/getEmotionCache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
3 changes: 3 additions & 0 deletions
3
examples/gatsby/plugins/gatsby-plugin-mui-emotion/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "gatsby-plugin-mui-emotion" | ||
} |