Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FOUC only in production on latest Firefox #22465

Open
nicholaschiang opened this issue Feb 23, 2021 · 16 comments
Open

FOUC only in production on latest Firefox #22465

nicholaschiang opened this issue Feb 23, 2021 · 16 comments
Labels
bug Issue was opened via the bug report template. good first issue Easy to fix issues, good for newcomers Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.).

Comments

@nicholaschiang
Copy link
Contributor

nicholaschiang commented Feb 23, 2021

What version of Next.js are you using?

10.0.7

What version of Node.js are you using?

12.8.3

What browser are you using?

Firefox 85.0.1 (64-bit)

What operating system are you using?

Pop_OS! 20.04 (Ubuntu 20.04)

How are you deploying your application?

Vercel

Describe the Bug

I see a FOUC when accessing my production deployment on Firefox:

fouc

It seems to disappear when using Chrome or Firefox on mobile.

I also appear to get the same issue for my portfolio website on Firefox:

fouc-portfolio

Expected Behavior

There should be no FOUC.

To Reproduce

  1. Visit my production deployment.
  2. See FOUC.

Or, to reproduce locally:

  1. Clone this repository.
  2. Install dependencies (using yarn).
  3. Start a development server (using yarn dev:next).
  4. Visit http://localhost:3000 and see FOUC.

You'll need some env files for that reproduction to work correctly. Instead, you can just reproduce the same FOUC using my portfolio website (which is a lot smaller than that repo anyways):

  1. Clone this repository.
  2. Install dependencies (using yarn).
  3. Start a development server (using yarn dev).
  4. Visit http://localhost:3000 and see FOUC.
@nicholaschiang nicholaschiang added the bug Issue was opened via the bug report template. label Feb 23, 2021
@nicholaschiang
Copy link
Contributor Author

I've also confirmed this behavior with the latest next@canary for tutorbook/tutorbookapp. It still shows a FOUC.

@nicholaschiang nicholaschiang changed the title Hideous FOUC on latest Firefox FOUC only in production on latest Firefox Feb 24, 2021
@nicholaschiang
Copy link
Contributor Author

nicholaschiang commented Feb 24, 2021

The FOUC seems to go away when using next@canary for the development server. But it's still there for next build (i.e. in production when deployed on Vercel).

@elinlutz
Copy link

elinlutz commented Mar 2, 2021

This might help: After hours of troubleshooting sudden FOUC in my next.js app (deployed at Vercel with styled-jsx, problems in production mode only) I discovered that I had styled-jsx installed as a separate package in my app. This created some conflict with next.js built-in styled-jsx. Deleting the package fixed it.

nicholaschiang added a commit to tutorbookapp/tutorbook that referenced this issue Mar 2, 2021
@nicholaschiang
Copy link
Contributor Author

nicholaschiang commented Mar 2, 2021

@elinlutz Thanks for the tip! Didn't seem to work though.... still got a FOUC in production while using Firefox. I'm using mostly SCSS modules (styled-jsx only for a couple of icons I think).

nicholaschiang added a commit to tutorbookapp/tutorbook that referenced this issue Mar 7, 2021
@muntasirsyed
Copy link

Also having this issue + also using SCSS modules. Is there a trend there or anyone else experiencing this and not using SCSS?

@nikoladev
Copy link

nikoladev commented Mar 11, 2021

@muntasirsyed I'm also using SCSS modules and having the issue. Haven't tried it without SCSS or modules yet

@developit
Copy link
Contributor

not sure if I'm stating the obvious here, but FWIW both of the videos just show a cursor moving around a black screen.

@nicholaschiang
Copy link
Contributor Author

@developit Yup, sorry. Just updated the original issue comment with GIFs that should show the FOUC properly.

@Dammic
Copy link

Dammic commented Mar 24, 2021

I have the same issue on the project I'm working on. And that happens also on chrome (I see that only on your portfolio example as well).

We're using styled-components, and nothing I've done is correcting this behaviour (tried many things already). I don't have any conflicting styled-jsx, I'm using 10.0.9 version of nextjs). I've finally opted to just hide the content until the js is loaded, but that's not a good solution for SSR.

@nicholaschiang
Copy link
Contributor Author

Weird update: The FOUC seems to go away when using a Firefox private browsing window. Perhaps it has something to do with local storage data or service workers or something... 🤷

@nicholaschiang
Copy link
Contributor Author

nicholaschiang commented Mar 28, 2021

I'm guessing that the issue was introduced due to a Next.js change in how scripts are loaded.

See: https://bugzilla.mozilla.org/show_bug.cgi?id=1404468

This issue also appears on Vercel and the Next.js docs when JavaScript is disabled:

Layout was forced before the page was fully loaded. If stylesheets are not yet loaded this may cause a flash of unstyled content.

@nicholaschiang
Copy link
Contributor Author

After some investigation, I think there are a couple of bugs at play here:

  1. Firefox's devtools inspector updates the layout of the page (so you can inspect stuff) before stylesheets have loaded.
    I'm guessing this is what causes the FOUC when devtools are open (and JavaScript is disabled). Note that this error seems to always occur if the React Developer Tools extension is active. Notice how the "forced layout" console warning doesn't come from any of the page scripts, but from a node.js:354 Mozilla devtools source file (specifically view-source:resource://devtools/server/actors/inspector/node.js):

image

  1. Any other extension that updates the page before stylesheets have loaded will cause a FOUC.
    Similar to how the devtools inspector causes a FOUC, any other Firefox extension that tries to access the page layout before stylesheets have loaded will also cause a FOUC. This is why the FOUC goes away when using a private browsing window without any extensions installed.

  2. Next.js doesn't wait for stylesheets to load before executing scripts.
    This causes a FOUC when one of those scripts tries to access the page layout before stylesheets have been loaded. This is the main issue here that should be addressed by Next.js maintainers. Notice how the "forced layout" console warning comes from one of my page scripts that tries to use window.innerWidth and window.innerHeight:

image
image

@pcmaffey
Copy link

A hack workaround: add a dummy <script>0</script> to your _document.js page, like so:

class MyDocument extends Document {
  render() {
    return (
      <Html>
        <Head />
        <body>
          <script>0</script>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

Here's the 7 year old issues with FF lol https://stackoverflow.com/questions/21147149/flash-of-unstyled-content-fouc-in-firefox-only-is-ff-slow-renderer

@nicholaschiang
Copy link
Contributor Author

@pcmaffey Thanks! That looks like the workaround a lot of us are using (I also saw that StackOverflow answer). But I'm wondering if it has to do with Next.js changes... because it was working previously (e.g. before 10.x I think) without a FOUC.

@apoorvHearth
Copy link

apoorvHearth commented May 5, 2021

Facing the same issue, but what I have found out is that styles-jsx and material UI styles do get rendered on the server, while SCSS modules are loaded later through JS which causes a FOUC when styling your content with SCSS modules.

nicholaschiang added a commit to tutorbookapp/tutorbook that referenced this issue May 11, 2021
nicholaschiang added a commit to nicholaschiang/hammock that referenced this issue Jun 10, 2021
rampage1212 pushed a commit to rampage1212/saintmichaeltrio that referenced this issue Sep 17, 2022
Addresses vercel/next.js#22465 until it's fixed upstream (in Firefox).
@jankaifer
Copy link
Contributor

Just checked with pnpm create next-app 22465 it's still an issue.
Another SO post that might be relevant: https://stackoverflow.com/a/57675785
Disabling JS in the browser fixes the issue so some script might be forcing layout which renders the page in ff.

@jankaifer jankaifer added type: needs investigation good first issue Easy to fix issues, good for newcomers labels Nov 28, 2022
@balazsorban44 balazsorban44 added Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.). and removed area: Ecosystem labels Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. good first issue Easy to fix issues, good for newcomers Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.).
Projects
None yet
Development

No branches or pull requests

10 participants