gatsby-plugin-fix-fouc
Version:
Gatsby Plugin Fix FOUC is a plugin that solves flicker of unstyled content
114 lines (79 loc) • 6.28 kB
Markdown
# Gatsby Plugin Fix FOUC
[](https://github.com/bicstone/gatsby-plugin-fix-fouc/actions/workflows/test.yml)
[](https://github.com/bicstone/gatsby-plugin-fix-fouc/actions/workflows/linter.yml)
[](https://github.com/bicstone/gatsby-plugin-fix-fouc/actions/workflows/publish-latest.yml)
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fbicstone%2Fgatsby-plugin-fix-fouc?ref=badge_shield)
[](https://sonarcloud.io/summary/new_code?id=bicstone_gatsby-plugin-fix-fouc)
[](https://deepsource.io/gh/bicstone/gatsby-plugin-fix-fouc/?ref=repository-badge)
[](https://codecov.io/gh/bicstone/gatsby-plugin-fix-fouc)
[](https://www.npmjs.com/package/gatsby-plugin-fix-fouc)

Gatsby Plugin Fix FOUC is a plugin that solves flash of unstyled content.
Supports Gatsby v3 - v5
<details>
<summary>Screen records when plugin is used (note, used gif animation images)</summary>
| Without Plugin | With Plugin |
| ---------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
|  |  |
</details>
## 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](https://developers.google.com/optimize/). 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
```bash
yarn add gatsby-plugin-fix-fouc
# or
npm install gatsby-plugin-fix-fouc
```
## Usage
```js
// gatsby-config.js
module.exports = {
plugins: [`gatsby-plugin-fix-fouc`],
};
```
## Advanced
```ts
// 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 | `4000` | 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](https://allcontributors.org/docs/en/emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://bicstone.me/"><img src="https://avatars.githubusercontent.com/u/47806818?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Oishi Takanori</b></sub></a><br /><a href="#ideas-bicstone" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/bicstone/gatsby-plugin-fix-fouc/commits?author=bicstone" title="Code">💻</a> <a href="#maintenance-bicstone" title="Maintenance">🚧</a> <a href="#question-bicstone" title="Answering Questions">💬</a> <a href="https://github.com/bicstone/gatsby-plugin-fix-fouc/commits?author=bicstone" title="Documentation">📖</a> <a href="https://github.com/bicstone/gatsby-plugin-fix-fouc/pulls?q=is%3Apr+reviewed-by%3Abicstone" title="Reviewed Pull Requests">👀</a></td>
</tr>
</table>
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
## LICENCE
MIT License
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fbicstone%2Fgatsby-plugin-fix-fouc?ref=badge_large)