简体   繁体   English

Next.js 未在服务器端渲染中渲染 CSS

[英]Next.js is not rendering CSS in Server Side Rendering

I have created next.js application using npx create-next-app and have not made any changes to it.我已经使用 npx npx create-next-app创建了 next.js 应用程序,并且没有对其进行任何更改。

I noticed that imported.css styles are being rendered properly in Client Side Render but not on Server Side Render.我注意到imported.css 8CBA22E28EB17B5F5C6AE2A266AZ styles 在客户端渲染中正确渲染,但在服务器端渲染中没有正确渲染。

According to Next.js document imported.css should work just fine.根据Next.js 文件导入。css 应该可以正常工作。

_app.js _app.js

import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

package.json package.json

{
  "name": "next-test",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "10.0.9",
    "react": "17.0.1",
    "react-dom": "17.0.1"
  }
}

CSR:企业社会责任:

在此处输入图像描述

SSR固态继电器

在此处输入图像描述

If you are not in production mode, it's a normal behavior.如果您未处于生产模式,这是正常行为。

The doc stated this: if you disable JavaScript the CSS will still be loaded in the production build (next start). During development, we require JavaScript to be enabled to provide the best developer experience with Fast Refresh.该文档说明了这一点: if you disable JavaScript the CSS will still be loaded in the production build (next start). During development, we require JavaScript to be enabled to provide the best developer experience with Fast Refresh. if you disable JavaScript the CSS will still be loaded in the production build (next start). During development, we require JavaScript to be enabled to provide the best developer experience with Fast Refresh.

Reference 参考

I could reproduce this broken css you shared by removing this line:我可以通过删除此行来重现您共享的这个损坏的 css:

// pages/index.js
import styles from '../styles/Home.module.css'

and not:并不是:

import '../styles/globals.css'

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

相关问题 Next.js static 服务器端渲染和 Gatsby.js - Next.js static server side rendering and Gatsby.js 在服务器端渲染中使用 Next.js 的 Tailwind css 的意外加载时间 - Unexpected loading time for Tailwind css with Next.js in Server-side rendering 使用 Next.js 对服务器端渲染进行 React Query - React Query with server side rendering using Next.js 使用 next.js 与传统 SSR 进行服务器端渲染 - Server side rendering with next.js vs traditional SSR 如何让 Next.js 做 SSR(服务器端渲染)? - How to make Next.js do SSR (Server Side Rendering)? 用于服务器端渲染的 Next.js 路由器中的匹配和参数对象 - Match and Param object in Next.js router for Server side rendering Video.js 在带有服务器端渲染的 next.js 中不起作用 - Video.js doesn't work in next.js with server side rendering 无法在 Next.js 中获取动态组件以跳过服务器端渲染并仅在客户端上显示 - Unable to get dynamic component in Next.js to skip server side rendering and show only on the client 禁用 next.js 服务器端呈现以防止 window 未定义错误 - disabling next.js server side rendering to prevent window is undefined errors react-html5video是否与服务器端渲染兼容? (使用next.js) - Is react-html5video is compatible with server side rendering ?? (Using next.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM