next.jsantdserver-side-rendering

Ant Design V5 causing flash unstyled component at first load


The problem is very similar to the problem years back. Below are the link to navigate to the discussion.

Github Discussion

I'm using Next.js 13 and Ant Design V5, ant design component will have flash unstyled component at first load. After that it will back to normal, according to the discussion, seems like is the SSR issue.

Other than using the solutions inside the discussion, any perfect solution to solve it?


Solution

  • export default function App({ Component, pageProps }: AppProps) {
        const [mounted, setMounted] = useState(false);
        useEffect(() => setMounted(true), []);
    
        if (typeof window !== 'undefined') {
            window.onload = () => {
                document.getElementById('holderStyle')!.remove();
            };
        }
    
        return (
            <ConfigProvider>
                <style
                    id="holderStyle"
                    dangerouslySetInnerHTML={{
                        __html: `
                        *, *::before, *::after {
                            transition: none!important;
                        }
                        `,
                    }}
                />
                <div style={{ visibility: !mounted ? 'hidden' : 'visible' }}>
                    <Component {...pageProps} />
                </div>
            </ConfigProvider>
        );
    }
    

    After tested it one by one, so far this is the solution I found to solve the issue without disabling the SSR.