我正在尝试将Normalize.css添加为全局,并为我的CSS模块使用情感。
首先是我的.babelrc
{
"presets": [
["env", {
"modules": false,
"useBuiltIns": true
}],
"next/babel"
],
"plugins": [
"syntax-dynamic-import",
"transform-runtime",
"transform-decorators-legacy",
"transform-class-properties",
"transform-object-rest-spread",
"es6-promise",
["module-resolver", {
"root": ["./src"],
"alias": {
"styles": "./styles",
"assets": "./assets",
},
"cwd": "babelrc"
}],
["inline-import", { "extensions": [".css"] } ],
["emotion", { "inline": true }]
]
}
在我的_document.js
我添加了规范化
import Document, { Head, Main, NextScript } from 'next/document';
import normalize from 'normalize.css/normalize.css';
import { extractCritical } from 'emotion-server';
export default class MyDocument extends Document {
static getInitialProps({ renderPage }) {
const page = renderPage();
const styles = extractCritical(page.html);
return { ...page, ...styles };
}
constructor(props) {
super(props);
const { __NEXT_DATA__, ids } = props;
if (ids) {
__NEXT_DATA__.ids = ids;
}
}
render() {
return (
<html>
<Head>
<title>SSR</title>
<style jsx global>{normalize}</style>
<style dangerouslySetInnerHTML={{ __html: this.props.css }} />
</Head>
<body>
<Main />
<NextScript />
</body>
</html>
);
}
}
相同
import React, { Component } from 'react';
import Breadcrumb from 'components/Breadcrumb';
import Link from 'next/link';
import styled, { hydrate, keyframes, css, injectGlobal } from 'react-emotion';
// Adds server generated styles to emotion cache.
// '__NEXT_DATA__.ids' is set in '_document.js'
if (typeof window !== 'undefined') {
hydrate(window.__NEXT_DATA__.ids);
}
const basicStyles = css`
background-color: white;
color: cornflowerblue;
margin: 3rem 0;
padding: 1rem 0.5rem;
`
const Basic = styled.div`
${basicStyles};
`
export default class extends Component {
render() {
return (
<Basic>
<p>Basic style rendered by emotion</p>
</Basic>);
}
}
相同
错误:StyleSheet:
insertRule
只接受字符串。 at invariant(/home/riderman/WebstormProjects/tmp/node_modules/styled-jsx/dist/lib/stylesheet.js:274:11) 在StyleSheet.insertRule(/home/riderman/WebstormProjects/tmp/node_modules/styled-jsx/dist/lib/stylesheet.js:125:7) at /home/riderman/WebstormProjects/tmp/node_modules/styled-jsx/dist/stylesheet-registry.js:88:29 在Array.map(本机) 在StyleSheetRegistry.add(/home/riderman/WebstormProjects/tmp/node_modules/styled-jsx/dist/stylesheet-registry.js:87:27) 在JSXStyle.componentWillMount(/home/riderman/WebstormProjects/tmp/node_modules/styled-jsx/dist/style.js:58:26) 决心(/home/riderman/WebstormProjects/tmp/node_modules/react-dom/cjs/react-dom-server.node.development.js:2616:12) 在ReactDOMServerRenderer.render(/home/riderman/WebstormProjects/tmp/node_modules/react-dom/cjs/react-dom-server.node.development.js:2746:22) 在ReactDOMServerRenderer.read(/home/riderman/WebstormProjects/tmp/node_modules/react-dom/cjs/react-dom-server.node.development.js:2722:19) 在renderToStaticMarkup(/home/riderman/WebstormProjects/tmp/node_modules/react-dom/cjs/react-dom-server.node.development.js:2991:25)
检查源代码here
答案 0 :(得分:1)
在Zeit的样式-jsx页面上看起来有一个问题:https://github.com/zeit/styled-jsx/issues/298
根据此问题,它是外部样式或您需要将css
标记添加到模板文字中。
查看您的代码,您使用的是css
标记,但没有看到任何导致此问题的外部风格。如果你没有得到明确的答案,我会说要跟着Zeit跟进问题298。 HTH,干杯!
去除那里的jsx样式,只需将normalize添加到全局模板字符串中:
injectGlobal`
${normalize}
html, body {
padding: 3rem 1rem;
margin: 0;
background: papayawhip;
min-height: 100%;
font-family: Helvetica, Arial, sans-serif;
font-size: 24px;
}
`;
答案 1 :(得分:0)
我对此有一个附加的答案,该答案在TypeScript
和NextJS 9
中效果很好。它还可以直接根据您的node_modules进行导入。
Import raw-loader
(针对模块):
yarn add raw-loader
在global.d.ts
中,为原始加载程序定义一个钩子
declare module '!!raw-loader!*' {
const contents: string;
export = contents;
}
在名为Meta的组件中,我将_document.tsx
放在其中(也可以使用_app.tsx
,但是_document
可确保SSR),
import normalizeCss from '!!raw-loader!normalize.css';
const Meta = () => (
<div>
<Global
styles={css`
${normalizeCss}
body {
// ...
}
`}
></Global>
</div>
);
export default Meta;