简体   繁体   English

NextJS 12 的样式化组件在服务器端不起作用(缺少 ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ 首次渲染)

[英]Styled components with NextJS 12 not working server side (missing css first render)

With the recent NextJS 12 release, the styled-components is now supported without needing any plugin on top of NextJS.在最近的 NextJS 12 版本中,现在支持 styled-components,无需在 NextJS 之上添加任何插件。

But i cant seem to have it work with server side rendring.但我似乎无法让它与服务器端渲染一起使用。

To enable it, i installed the styled-components package and added the following in my config file.为了启用它,我安装了 styled-components package 并在我的配置文件中添加了以下内容。

module.exports = {
  compiler: {
    reactStrictMode: true,
    styledComponents: true,
  },
};

The styles are working on the client but the first render is missing the css styles. styles 正在客户端上工作,但第一个渲染缺少 ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ styles。 If i view the source page, can't see any css styles added there.如果我查看源页面,看不到任何 ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ styles 添加在那里。

I have faced this exact issue but had to switch on another project.我遇到了这个确切的问题,但不得不打开另一个项目。 Maybe try this in your config也许在你的配置中试试这个

/** @type {import('next').NextConfig} */

const nextConfig = {
  reactStrictMode: true,
  compiler: {
    styledComponents: true,
  },
}

module.exports = nextConfig

Source: https://styled-components.com/docs/advanced#with-swc资料来源: https://styled-components.com/docs/advanced#with-swc

I recommend using NoSSR in nextjs.我建议在 nextjs 中使用 NoSSR。

So, you can use the following.因此,您可以使用以下内容。

import dynamic from "next/dynamic";
...
const SidebarWithNoSSR = dynamic(() => import("./Sidebar"), { ssr: false });
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM