Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I noticed that imported .css styles are being rendered properly in Client Side Render but not on Server Side Render.

According to Next.js document imported .css should work just fine.

_app.js

import '../styles/globals.css'

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

export default MyApp

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:

enter image description here

SSR

enter image description here

like image 630
sravis Avatar asked Mar 21 '21 06:03

sravis


People also ask

Does NextJS use server-side rendering?

Next.js has two forms of pre-rendering: Static Generation and Server-side Rendering. The difference is in when it generates the HTML for a page. Static Generation (Recommended): The HTML is generated at build time and will be reused on each request. Server-side Rendering: The HTML is generated on each request.

Is NextJS good for client-side rendering?

Client-side data fetching with SWR The team behind Next. js has created a React hook library for data fetching called SWR. It is highly recommended if you are fetching data on the client-side. It handles caching, revalidation, focus tracking, refetching on intervals, and more.

Is NextJS server-side or client-side?

Next. js is used for server side rendering of react application . React along with other framework like angular and vue. js are traditional client side framework ,they run in browser but there are technology to run this framework on server side, and next.

Is NextJS SPA or MPA?

NextJS by default is not SPA based due to its hybrid nature, because it publishes each page as a separate entry point for everything under /pages . Of course if you only have one page index. js , then technically it's a SPA again.


1 Answers

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.

Reference

like image 99
enoch Avatar answered Oct 05 '22 21:10

enoch