我想<html lang="...">用对象表示法在 NextJs 应用程序上设置标签。
我为 Styled-components 渲染了一个 serverStylesheet,所以我当前的_document文件看起来像这样:
class CustomDocument extends Document {
static getInitialProps = async (ctx) => {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () => originalRenderPage({
enhanceApp: App => props => sheet.collectStyles(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{ initialProps.styles }
{ sheet.getStyleElement() }
</>
),
};
} finally {
sheet.seal();
}
}}
Run Code Online (Sandbox Code Playgroud)
我看过很多关于如何渲染常规 JSX 组件的文章,但我想用对象表示法来做。