7d3bfac39f
Adds additional tests for material-ui and stitches based on questions in the Twitter thread. Fixes TypeScript types when `LinkProps` is imported and used in combination with `<button>`, added a TODO to change this at a later point when the new behavior is the default instead of opt-in. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
88 lines
2.6 KiB
JavaScript
88 lines
2.6 KiB
JavaScript
import * as React from 'react'
|
|
import Document, { Html, Head, Main, NextScript } from 'next/document'
|
|
import createEmotionServer from '@emotion/server/create-instance'
|
|
import theme from '../src/theme'
|
|
import createEmotionCache from '../src/createEmotionCache'
|
|
|
|
export default class MyDocument extends Document {
|
|
render() {
|
|
return (
|
|
<Html lang="en">
|
|
<Head>
|
|
{/* PWA primary color */}
|
|
<meta name="theme-color" content={theme.palette.primary.main} />
|
|
<link rel="shortcut icon" href="/static/favicon.ico" />
|
|
<link
|
|
rel="stylesheet"
|
|
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
|
|
/>
|
|
{/* Inject MUI styles first to match with the prepend: true configuration. */}
|
|
{this.props.emotionStyleTags}
|
|
</Head>
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
</body>
|
|
</Html>
|
|
)
|
|
}
|
|
}
|
|
|
|
// `getInitialProps` belongs to `_document` (instead of `_app`),
|
|
// it's compatible with static-site generation (SSG).
|
|
MyDocument.getInitialProps = async (ctx) => {
|
|
// Resolution order
|
|
//
|
|
// On the server:
|
|
// 1. app.getInitialProps
|
|
// 2. page.getInitialProps
|
|
// 3. document.getInitialProps
|
|
// 4. app.render
|
|
// 5. page.render
|
|
// 6. document.render
|
|
//
|
|
// On the server with error:
|
|
// 1. document.getInitialProps
|
|
// 2. app.render
|
|
// 3. page.render
|
|
// 4. document.render
|
|
//
|
|
// On the client
|
|
// 1. app.getInitialProps
|
|
// 2. page.getInitialProps
|
|
// 3. app.render
|
|
// 4. page.render
|
|
|
|
const originalRenderPage = ctx.renderPage
|
|
|
|
// You can consider sharing the same emotion cache between all the SSR requests to speed up performance.
|
|
// However, be aware that it can have global side effects.
|
|
const cache = createEmotionCache()
|
|
const { extractCriticalToChunks } = createEmotionServer(cache)
|
|
|
|
ctx.renderPage = () =>
|
|
originalRenderPage({
|
|
enhanceApp: (App) =>
|
|
function EnhanceApp(props) {
|
|
return <App emotionCache={cache} {...props} />
|
|
},
|
|
})
|
|
|
|
const initialProps = await Document.getInitialProps(ctx)
|
|
// This is important. It prevents emotion to render invalid HTML.
|
|
// See https://github.com/mui/material-ui/issues/26561#issuecomment-855286153
|
|
const emotionStyles = extractCriticalToChunks(initialProps.html)
|
|
const emotionStyleTags = 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 }}
|
|
/>
|
|
))
|
|
|
|
return {
|
|
...initialProps,
|
|
emotionStyleTags,
|
|
}
|
|
}
|