FOUC when loading CSS
See original GitHub issueScenario
Given the following element: (See https://github.com/jaysoo/example-react-helmet)
<Helmet
style={[{
cssText: `
.message {
text-align: center;
font-size: 48px;
color: magenta;
}
`
}]}
/>
<p className="message">
Hello World!
</p>
</div>
I know this is a contrived example, and not how you should be styling React apps. The real world example involves allowing user customization via custom CSS.
Expected
The element is rendered, and the user sees “Hello World!” in large, magenta text.
Actual
The user will see a flash of unstyled content (FOUC) before the <style>
is inserted into <head>
. This seems to be caused by react-helmet v5’s use of requestIdleCallback
, which forces the head tag insertion to happen after the rendered element has been updated in the DOM.
Note: This does not happen with version 3.x.
Workaround
I don’t see any obvious workarounds, unless something clever is done using onChangeClientState
. Suggestions welcomed.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (8 by maintainers)
@cwelch5 I was able to get something working in this PR https://github.com/nfl/react-helmet/pull/297. I’d be glad to hear any feedback.
@jaysoo Hi, sorry for the delay. Just want to confirm some details and try to understand the issue, because we’ve had a FOUC in version 3.x and we tried to fix in 5.x. Is the Helmet example from above rendered in the root component of your app? If it is, then the
requestIdleCallback
may have introduced an issue. BUT, if this example is contained in a subcomponent of the app, there are multiple renders of Helmet on the client side, one of which is the root render that wouldn’t include your style.requestIdleCallback
was supposed to address this issue and instead of adding more API, I’d like to understand how we can use the callback to correctly fix this.Oh, and is this server-side rendered as well?
Thanks for the info.