-
Notifications
You must be signed in to change notification settings - Fork 26.3k
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
Development server has a flash of unstyled content (FOUC) #13058
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. |
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:
|
We previously used to remove our FOUC helper inside of the style injection to ensure content was shown as fast as possible. This behavior, however, was problematic for a few reasons: 1. Large JavaScript chunks would take longer than an animation frame to parse, causing FOUC 1. Rendering would sometimes complete before an animation frame, causing improper effects To fix the latter, we started removing the no FOUC helper **before** rendering, however, we never fixed the former by removing the dead code. There's not a great way to test this because the FOUC is so fast and flaky, however, this code really shouldn't exist and isn't likely to be re-added (regress). Also, we already have FOUC tests that occasionally flake, probably due to this. Fixes #12448 Fixes #13058 Fixes #11195 Fixes #10404
This should be fixed in |
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). |
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 |
We previously used to remove our FOUC helper inside of the style injection to ensure content was shown as fast as possible. This behavior, however, was problematic for a few reasons: 1. Large JavaScript chunks would take longer than an animation frame to parse, causing FOUC 1. Rendering would sometimes complete before an animation frame, causing improper effects To fix the latter, we started removing the no FOUC helper **before** rendering, however, we never fixed the former by removing the dead code. There's not a great way to test this because the FOUC is so fast and flaky, however, this code really shouldn't exist and isn't likely to be re-added (regress). Also, we already have FOUC tests that occasionally flake, probably due to this. Fixes vercel#12448 Fixes vercel#13058 Fixes vercel#11195 Fixes vercel#10404
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. |
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? |
Actually as a quick fix you can do the following:
.no-fouc {
visibility: hidden;
opacity: 0;
}
.fouc {
visibility: visible;
opacity: 1;
}
<html className="no-fouc">
...
</html>
...
componentDidMount() {
const removeFouc = (foucElement) => {
foucElement.className = foucElement.className.replace('no-fouc', 'fouc');
};
removeFouc(document.documentElement);
}
... This will hide the content of the page until the App component gets fully loaded. At that time all the CSS is parsed already. |
I happen's to me with latest version |
It's happening to me both in production and dev. |
Same for me in production with scss modules and next 10.0.8 |
I am still getting the same problem of unstyled content showing when the page loads in production. I haven't changed Is anyone working on fixing this?
This is a great solution, but the problem though is that one of the reasons to use nextjs is because it prerenders the page the user is trying to load, which allows the user to see the page way faster because they don't have to wait for the javascript to both load and finish running. This solution is counterproductive to that. Please fix this |
I found I was getting this FOUC because I had miscorrectly used |
I was getting FOUC on Firefox in production. I found out it was a Firefox bug which can be solved by adding a dummy // _document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head />
<body>
<script>0</script>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument Sounds weird but it did indeed solve the FOUC problem! 🎉 |
Please can you tell me how you mis-correctly used it? |
from what I can remember I believe I was importing it incorrectly as a standalone item rather than |
Thank you for that. I'm relatively new to nextjs so thanks for that. I'm using the MUI nextjs example and I can see that the document.tsx (or .js in the template) uses The nextjs documentation shows the use of Here's some related links in case it helps anyone: |
I think as Next.js has evolved maybe the correct use / implementation of |
I'm still getting FOUC when using the official MUI example. Their example shows the Then I'm using the |
could be something else then - I copied a fresh instance of |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
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:
README.md
) by running: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:
Notice that it's gone on our production website:
System information
The text was updated successfully, but these errors were encountered: