Next.js: Development server has a flash of unstyled content (FOUC)

Created on 18 May 2020  路  20Comments  路  Source: vercel/next.js

Bug report

Describe the bug

A clear and concise description of what the bug is.

When using Next.js, it appears that the CSS isn't fully hydrated into the <head> when the <div id="__next"> element first becomes visible.

This causes a flash of unstyled content (or FOUC) when running our development server. It seems like it's fine in production though (which seems strange to me).

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Clone this repository by running:
$ git clone https://github.com/tutorbookapp/covid-tutoring
  1. Install our dependencies (more details in our README.md) by running:
$ npm i && lerna bootstrap --hoist
  1. Start a development server by running:
$ npm run dev
  1. Notice the FOUC when first loading the page.

Expected behavior

A clear and concise description of what you expected to happen.

The <div id="__next"> element should only be visible after the necessary above-the-fold stylesheets (i.e. the style sheets that are included in the React components that are visible above-the-fold) are inserted into <head>. The remaining stylesheets can then be loaded with the <div id="__next"> element visible.

Screenshots

If applicable, add screenshots to help explain your problem.

See the FOUC visible on our development server:

fouc

Notice that it's gone on our production website:

no-fouc

System information

  • OS: Ubuntu 18.04.2
  • Browser: Firefox 76.0.1
  • Version of Next.js: 9.4.0
  • Version of Node.js: 12.16.1
bug needs investigation

Most helpful comment

What about production? I'm still experiences the FOUC with StyledComponents + Material UI

All 20 comments

I am also facing this problem when I use css modules but when I use styled-jsx then It works fine.

I notice a similar issue that global css in _app.js doesn't seem to be loaded with javascript off while it's in dev mode. It makes SSR harder to test since there could be missing styles.

@robgraeber I have the exact issue you do. The CSS is being compiled into _app.js instead of a separate css file.

Same issue here.
I've created a minimum example reproducing this issue: https://github.com/dnaranjo89/next-css-ssr

Also sometimes I'll edit some global css in inspector and any change causes the whole page's css to be wrecked somehow. Has anyone experienced that?

@derskeal a workaround is to use this sass plugin and import your stylesheets via sass in your Layout component: https://github.com/giuseppeg/styled-jsx-plugin-sass

@Timer I could reproduce this issue in this repo https://github.com/yanv1991/demo-react-dom, this is not loading the styles using built in sass modules with a dynamic loaded component for ssr in prod mode

This project cannot be ran:

error - Error [FirebaseError]: projectId must be a string in FirebaseApp.options
    at new FirestoreError (/Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/firestore/dist/index.node.cjs.js:1223:28)
    at Function.Firestore.databaseIdFromApp (/Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/firestore/dist/index.node.cjs.js:21018:19)
    at new Firestore (/Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/firestore/dist/index.node.cjs.js:20850:42)
    at /Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/firestore/dist/index.node.cjs.js:22871:66
    at Component.instanceFactory (/Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/firestore/dist/index.node.cjs.js:22410:16)
    at Provider.getOrInitializeService (/Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/component/dist/index.cjs.js:219:39)
    at Provider.getImmediate (/Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/component/dist/index.cjs.js:120:33)
    at FirebaseAppImpl._getService (/Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/app/dist/index.node.cjs.js:228:49)
    at FirebaseAppImpl.firebaseAppImpl.<computed> [as firestore] (/Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/app/dist/index.node.cjs.js:440:39)
    at Object.serviceNamespace [as firestore] (/Users/joe/Desktop/scratch2/covid-tutoring/node_modules/@firebase/app/dist/index.node.cjs.js:420:45)
    at eval (webpack-internal:///./src/firebase/db.tsx:19:124)
    at Module../src/firebase/db.tsx (/Users/joe/Desktop/scratch2/covid-tutoring/.next/server/static/development/pages/_app.js:128:1)
    at __webpack_require__ (/Users/joe/Desktop/scratch2/covid-tutoring/.next/server/static/development/pages/_app.js:23:31)
    at eval (webpack-internal:///./src/firebase/user.tsx:12:61)
    at Module../src/firebase/user.tsx (/Users/joe/Desktop/scratch2/covid-tutoring/.next/server/static/development/pages/_app.js:152:1)
    at __webpack_require__ (/Users/joe/Desktop/scratch2/covid-tutoring/.next/server/static/development/pages/_app.js:23:31) {
  code: 'invalid-argument',
  toString: [Function]
}

This should be fixed in next@^9.4.5-canary.15! Please upgrade and let us know.

Thanks @Timer! 馃帀 I tested and the fix works for me.

I was experiencing this in both dev mode and production mode in a custom server (incrementally converting pages on enterprise site). ^9.4.5-canary.15 fixed it for me!

Hi guys. Checked the problem with the canary release, it fixes the issue in dev mode but in production it still remains. Does somebody experiences the same issue?

In the production build there is missing the <style data-next-hide-fouc="true">body{display:none}</style>.

Hi guys. Checked the problem with the canary release, it fixes the issue in dev mode but in production it still remains. Does somebody experiences the same issue?

In the production build there is missing the <style data-next-hide-fouc="true">body{display:none}</style>.

Same here, is this a different issue when it happens in prod or related, does anyone know?

Same here, is this a different issue when it happens in prod or related, does anyone know?

For me this issue was only occurring in dev mode, not production mode. So given that, I'd assume the prod issue may have a different cause.

Hi guys. Checked the problem with the canary release, it fixes the issue in dev mode but in production it still remains. Does somebody experiences the same issue?

In the production build there is missing the <style data-next-hide-fouc="true">body{display:none}</style>.

Was anyone able to fix this in production? Canary release fixed our dev build, but production is still broken.

so is canary latest version of next then?

Yes @jimmynames, canary is the WIP latest version (the term originated from miners using canary birds to test for toxic fumes... The canary version of Next.js could be sketch).

This fix is on the stable release of Next.js 9.5.0 and 9.5.1, or newer (applies to development server only)!

What about production? I'm still experiences the FOUC with StyledComponents + Material UI

Did anybody find any solution for this?

Was this page helpful?
0 / 5 - 0 ratings