【问题标题】:Not working Server side rendering Styled-Components with Next JS无法使用 Next JS 进行服务器端渲染 Styled-Components
【发布时间】:2021-10-30 14:20:01
【问题描述】:

我尝试通过代码和调试来解决我的问题,但是我找不到原因,所以我发布了一个问题,并附上了_document.js和_app.js和babel的内容。

另外在 Chrome 中应用了禁用 Javasrcipt。

如果您有更多信息,请告诉我。

任何帮助将不胜感激。

截图

网络

调试器

通天塔

    {
  "presets": ["next/babel"],
  "plugins": [
    [
      "babel-plugin-styled-components",
      {
        "fileName": true,
        "displayName": true,
        "pure": true,
        "ssr": true,
        "preprocess": false
      }
    ]
  ]
}

_document.js

import React from 'react';
import Document, { Html, Head, Main, NextScript } from "next/document"
import { ServerStyleSheet } from 'styled-components';

export default class MyDocument extends Document {
    static async getInitialProps(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()}
                    </>
                ),
            };
        } catch (error) {
            console.error(error);
        } finally {
            sheet.seal();
        }
    }

    render() {
        return (
            <Html>
                <Head />
                <body>
                    <script src="https://polyfill.io/v3/polyfill.min.js?features=default%2Ces2015%2Ces2016%2Ces2017%2Ces2018%2Ces2019"></script>
                    <Main />
                    <NextScript />
                </body>
            </Html>
        );
    }
}

_app.js

import React from 'react';
import Prototype from 'prop-types';
import Head from 'next/head';
import 'antd/dist/antd.css';

import wrapper from '../store/configureStore';

const App = ({ Component }) => {

    return (
        <>
            <Head>
                <meta charSet='utf-8'></meta>
                <title>NodeBird</title>
            </Head>
            <Component />
        </>

    );
}

App.Prototype = {
    Component: Prototype.elementType.isRequired,
}

export default wrapper.withRedux(App);

【问题讨论】:

  • 看不出您的styled-components 设置有什么问题。不过,该错误似乎源自antd 库。你在哪里使用antd?
  • @juliomalves Antd 是从_app.js 导入的,我看了其他问题,Antd 看到部署后可以检查 SSR。 styled component 本身似乎对 SSR 没有问题。我通过单独配置页面进行了检查。

标签: javascript reactjs next.js styled-components server-side-rendering


【解决方案1】:

安装

npm i babel-plugin-styled-components --save-dev

.babelrc

{
  "presets": [
    "next/babel"
  ],
  "plugins": [
   [
     "styled-components",
     {
        "ssr": true,
        "displayName": true,
        "preprocess": false
      }
   ]
  ]
}

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 2020-08-21
  • 2021-11-20
  • 2021-03-30
  • 1970-01-01
  • 2021-09-19
  • 2023-03-24
  • 2017-04-04
  • 2018-03-06
  • 2019-02-12
相关资源
最近更新 更多