使用 Next.js 渲染 normalize.css + 情感风格

Render normalize.css + emotion styles with Next.js

我正在尝试将 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 }]
  ]
}

添加Normalize.css

在我的 _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>
    );
  }
}

shown here

相同

添加我的 css 带有情感的模块

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>);
  }
}

shown here

相同

问题

Error: StyleSheet: insertRule accepts only strings. at invariant (/home/riderman/WebstormProjects/tmp/node_modules/styled-jsx/dist/lib/stylesheet.js:274:11) at 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 at Array.map (native) at StyleSheetRegistry.add (/home/riderman/WebstormProjects/tmp/node_modules/styled-jsx/dist/stylesheet-registry.js:87:27) at JSXStyle.componentWillMount (/home/riderman/WebstormProjects/tmp/node_modules/styled-jsx/dist/style.js:58:26) at resolve (/home/riderman/WebstormProjects/tmp/node_modules/react-dom/cjs/react-dom-server.node.development.js:2616:12) at ReactDOMServerRenderer.render (/home/riderman/WebstormProjects/tmp/node_modules/react-dom/cjs/react-dom-server.node.development.js:2746:22) at ReactDOMServerRenderer.read (/home/riderman/WebstormProjects/tmp/node_modules/react-dom/cjs/react-dom-server.node.development.js:2722:19) at renderToStaticMarkup (/home/riderman/WebstormProjects/tmp/node_modules/react-dom/cjs/react-dom-server.node.development.js:2991:25)

已添加

检查源代码here

https://gitlab.com/problems/test-emotion-plus-global-nextjs

在 Zeit 的 styled-jsx 页面上似乎有一个问题:https://github.com/zeit/styled-jsx/issues/298

根据这个问题,它要么是外部样式,要么是您需要将 css 标记添加到您的模板文字中。

查看您的代码,您正在使用 css 标记,没有看到任何导致此问题的外部样式。如果您没有得到明确的答复,我建议您跟进 Zeit 的第 298 期。 HTH,干杯!

编辑

摆脱其中的 jsx 样式,只需将规范化添加到您的全局模板字符串中:

injectGlobal`
    ${normalize}
    html, body {
      padding: 3rem 1rem;
      margin: 0;
      background: papayawhip;
      min-height: 100%;
      font-family: Helvetica, Arial, sans-serif;
      font-size: 24px;
    }
  `;

我还有一个在 TypeScriptNextJS 9 中运行良好的额外答案。它还会直接根据您的 node_modules.

保留您的导入

Import raw-loader 模块:

yarn add raw-loader

global.d.ts中,为raw-loader定义一个钩子

declare module '!!raw-loader!*' {
  const contents: string;
  export = contents;
}

在我的 _document.tsx 中有一个名为 Meta 的组件(_app.tsx 也可以,但是 _document 确保 SSR),我有这个

import normalizeCss from '!!raw-loader!normalize.css';

const Meta = () => (
  <div>
    <Global
      styles={css`
        ${normalizeCss}
        body {
          // ...
        }
      `}
     ></Global>
  </div>
);

export default Meta;