-
Notifications
You must be signed in to change notification settings - Fork 27.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
[NEXT-643] FOUC using CSS-in-JS libraries in appDir through useServerInsertedHTML #44125
Comments
@mghizzo Thank you so much for opening this issue. Especially for your temporary solution. I am experiencing the same problems. Using styled-components and following the instructions in https://beta.nextjs.org/docs/styling/css-in-js#styled-components leads to layout shifts. Adding the I have no idea if that is expected behavior or not. If it is expected it would be nice to add this to the documentation. |
@jesko-plitt I already have the "< head >" tag in my RootLayout and I still get this issue (using AntDesign v5). It was always there in Next.js 13 projects. Here is my
|
@IonelLupu This is my solution in Nextjs 13 with App Directory & Ant Design 5 layout.tsx
Edit: found a workaround so that we don't need an external library anymore |
@mghizzo I am facing this issue with the pages directory, ant design is causing FOUC, how did you fix it? |
@devagrawal09 check this issue: ant-design/ant-design#39891. Someone also proposed a fix on the Next.js side: #44015. Waiting to be merged |
@IonelLupu all the examples are with the app directory, I'm on the pages directory |
I have the same issue with Next.js 13 (pages directory) + Ant Design v5! |
No I pretty much gave up and moved to Tailwind |
So, no solution for Next 13 (pages) + Ant Design SSR? |
This would be a bummer, considering that |
Same here! Did anyone get a solution? |
Same issue here with Next.js 13 (pages directory) and Ant Design 5. Is there any example for the pages directory? |
Hey guys, I discovered that the antd team offers two solutions on their official page: https://ant.design/docs/react/customize-theme#server-side-render-ssr. |
Adding "use client";
import { ConfigProvider, theme } from "antd";
import React, {
createContext,
FC,
PropsWithChildren,
useContext,
useMemo,
useState,
} from "react";
import { StyleProvider, createCache, extractStyle } from "@ant-design/cssinjs";
import { useServerInsertedHTML } from "next/navigation";
const ThemeContext = createContext<{ }>({
} as any);
// eslint-disable-next-line react-hooks/rules-of-hooks
export const useTheme = () => useContext(ThemeContext);
const ThemeProvider: FC<PropsWithChildren> = ({ children }) => {
const [cache] = useState(() => createCache());
useServerInsertedHTML(() => {
return (
<style
id="antd-style"
dangerouslySetInnerHTML={{
__html: extractStyle(cache, true),
}}
/>
)
})
return (
<StyleProvider cache={cache} hashPriority="high">
<ConfigProvider
direction="ltr"
theme={{
algorithm: mode === "dark" ? theme.darkAlgorithm : theme.defaultAlgorithm,
token: {
colorPrimary: "#3f7291",
},
}}
>
<ThemeContext.Provider
value={{}}
>
{children}
</ThemeContext.Provider>
</ConfigProvider>
</StyleProvider>
);
};
export default ThemeProvider; |
@PandukaNandara, the problem with this approach is that the Time to First Byte (TTFB) will be 2-3 seconds. Your users won't be able to get styles until the server compiles them in runtime. |
Verify canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 22.2.0: Fri Nov 11 02:03:51 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T6000
Binaries:
Node: 16.16.0
npm: 8.11.0
Yarn: 1.22.19
pnpm: N/A
Relevant packages:
next: 13.0.8-canary.0
eslint-config-next: 13.0.6
react: 18.2.0
react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true)
Link to the code that reproduces this issue
https://github.com/mghizzo/next-reproduction-app
To Reproduce
Start the bug reproduction project and just navigate to
http://localhost:3000
to see FOUC, you can also disable javascript on the page to ensure that styles which should be injected are not there.Describe the Bug
Using
useServerInsertedHTML
inside a component won't inject result in the head if there's no<head>
tag.You can temporarily fix this issue by using
head
tag somewhere (head file if you have one or just put a<head />
in the Root Layout.I also found a commit which was meant for a fix but it was then reverted.
Expected Behavior
It is expected that the result of
useServerInsertedHTML
gets injected inside the head.Which browser are you using? (if relevant)
Google Chrome 108.0.5359.124 (ARM64)
How are you deploying your application? (if relevant)
No response
NEXT-643
The text was updated successfully, but these errors were encountered: