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

Getting flash of unstyled content (FOUC) with styled-components #40122

Closed
1 task done
GuerrillaCoder opened this issue Aug 31, 2022 · 3 comments
Closed
1 task done

Getting flash of unstyled content (FOUC) with styled-components #40122

GuerrillaCoder opened this issue Aug 31, 2022 · 3 comments
Labels
bug Issue was opened via the bug report template.

Comments

@GuerrillaCoder
Copy link

GuerrillaCoder commented Aug 31, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Wed Aug 25 23:20:18 UTC 2021
Binaries:
  Node: 16.13.2
  npm: 8.15.0
  Yarn: 1.22.19
  pnpm: N/A
Relevant packages:
  next: 12.2.6-canary.7
  eslint-config-next: 12.2.5
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

Chrome Version 104.0.5112.102 (Official Build) (64-bit)

How are you deploying your application? (if relevant)

Vercel

Describe the Bug

I am getting a flash of unstyled content on my site that is quite bad. For 1-2 seconds my unstyled menu and page appears then the styles load on page refresh.

I created a new project and started copying stuff across to see what caused the issue and I discovered that adding fontawesome caused the issue to occur (quicker than on my site but still visible).

When refreshing page you can see the unstyled <ul> list of the menu. The menu is styled with the styled-components which I think might have something to do with it.

I am using SWC but I have tried installing babelrc plugin and the same behavior occurs.

Expected Behavior

The site should load its initial state with styles.

Link to reproduction

https://github.com/GuerrillaCoder/fouc-issue

To Reproduce

You can run yarn run dev in the project yourself or you can see it here on vercel: https://fouc-issue.vercel.app/

Open dev tools and disable cache then do a full page refresh and you should be able to see a flash of the unstyled menu.

Also if you load page without javascript enabled the styles do not load.

@GuerrillaCoder GuerrillaCoder added the bug Issue was opened via the bug report template. label Aug 31, 2022
@GuerrillaCoder GuerrillaCoder changed the title Fontawesome causes flash of unstyled content (FOUC) on styled-component Getting flash of unstyled content (FOUC) with styled-components Aug 31, 2022
@GuerrillaCoder
Copy link
Author

GuerrillaCoder commented Aug 31, 2022

I discovered the Issue was that I had created a custom `_document.tsx" without getting the styles and injecting them.

I updated the page to this:

import * as React from 'react';
import Document,{ DocumentContext, Html, Head, Main, NextScript } from 'next/document'
import { ServerStyleSheet } from 'styled-components'

export default class MyDocument extends Document {
    static async getInitialProps(ctx: DocumentContext) {
        const sheet = new ServerStyleSheet()
        const originalRenderPage = ctx.renderPage

        try {
            ctx.renderPage = () =>
                originalRenderPage({
                    enhanceApp: (App) => (props) =>
                        sheet.collectStyles(<App {...props} />),
                })

            const initialProps = await Document.getInitialProps(ctx)
            return {
                ...initialProps,
                styles: [initialProps.styles, sheet.getStyleElement()],
            }
        } finally {
            sheet.seal()
        }
    }
    render() {
        return (
            <Html>
                <Head>
                    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;500;600;700&display=swap" rel="stylesheet" />
                </Head>
                <body>
                <script>0</script>
                <Main />
                <NextScript />
                </body>
            </Html>
        )
    }
}

This works better and most of the styles now load without javascript enabled but not all of them.

I am guessing I need to inject something from the context into the head but I am not sure exactly what I should be doing, I can't find example of styled-components with modified _document.tsx render.

@GuerrillaCoder
Copy link
Author

Issue was that development mode doesn't include CSS includes, after I exported it CSS all loads now properly apart from a slight jump from the google font loading in

@github-actions
Copy link
Contributor

github-actions bot commented Oct 1, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

1 participant