Now 100x Faster

Read the latest from Gatsby CTO Kyle Mathews Re-introducing Gatsby, a Reactive Site Generator

ContactSign Up for Free
Community Plugin
View plugin on GitHub

Gatsby Plugin Fix FOUC

Build Linter Publish (latest) Publish (next) FOSSA Status Quality Gate Status DeepSource codecov npm

Gatsby Plugin Fix FOUC is a plugin that solves flash of unstyled content.

⚠ Not adequately tested. Please use this plugin at your own risk. We welcome your feedback.

How Does It Work

Adds a data attribute to the <body> element that hides the page until the initial render of Gatsby App is done on the client, so that the awful flicker is not shown to you.
This approach is also used by Google. Does not re-rendering and has no impact on performance and accessibility.

Trade-Off

Lighthouse scores will decrease. (First Contentful Paint, Largest Contentful Paint) as a result, it may affect SEO. (but, Cumulative Layout Shift scores improve.)
You can specify the width that does not hide the screen by setting minWidth.

Install

yarn add gatsby-plugin-fix-fouc

# or

npm install gatsby-plugin-fix-fouc

Usage

// gatsby-config.js

module.exports = {
  plugins: [`gatsby-plugin-fix-fouc`],
};

Advanced

// gatsby-config.ts

import { breakpoints } from "./src/themes";

import type { GatsbyConfig } from "gatsby";
import type { GatsbyPluginFixFoucRefOptions } from "gatsby-plugin-fix-fouc";

const config: GatsbyConfig = {
  plugins: [
    {
      resolve: `gatsby-plugin-fix-fouc`,
      options: {
        attributeName: "is-loading",
        minWidth: breakpoints.values.sm,
        timeout: 3000,
      } as GatsbyPluginFixFoucRefOptions,
    },
  ],
};

export default config;

Options

Property Type Default Description
attributeName string gatsby-plugin-fix-fouc-is-loading The data-* attribute name to be added.
minWidth number 0 The minimum width (px) of hides the page. If not set, hides regardless of width.
timeout number 2000 The time (milliseconds) of the timer should wait before shows the page as a fallback even if the initial render is not done.

Contributors

Thanks goes to these wonderful people (emoji key):


Oishi Takanori

🤔 💻 🚧 💬 📖 👀

LICENCE

MIT License

FOSSA Status

© 2022 Gatsby, Inc.