question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v2] Webpack V4 style-loader FOUC in development

See original GitHub issue

Description

Since #6008 and #6009, CSS styles are no longer inlined in development (they are still inlined in production). In development, this causes a flash of unstyled content followed by a layout jump when the styles are applied.

Steps to reproduce

Create a Gatsby v2 site using CSS (rather than CSS-in-JS). In development, the styles will be applied via a link tag (causing a flash of unstyled content). In production, the styles are inlined, leading to the same smooth loading experience as in v1.

Expected result

To prevent developer confusion, the visual behavior of a Gatsby site should be the same in development and production. The timing of loading and applying CSS styles affects this consistency. V1 achieved this consistency by always inlining styles.

Actual result

When using CSS, V2 offers an inconsistent experience in development vs. production by switching between inlining and not-inlining CSS styles.

(If I’ve misunderstood any of this, please let me know!)

Environment

  System:
    OS: macOS High Sierra 10.13.5
    CPU: x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 8.7.0 - /usr/local/bin/node
    Yarn: 1.5.1 - /usr/local/bin/yarn
    npm: 6.1.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 67.0.3396.99
    Firefox: 60.0.2
    Safari: 11.1.1
  npmPackages:
    gatsby: next => 2.0.0-beta.9 
    gatsby-cli: next => 2.0.0-beta.2 
    gatsby-image: next => 2.0.0-beta.2 
    gatsby-plugin-favicon: ^2.1.1 => 2.1.1 
    gatsby-plugin-manifest: next => 2.0.2-beta.2 
    gatsby-plugin-netlify: next => 2.0.0-beta.2 
    gatsby-plugin-netlify-cache: ^0.1.0 => 0.1.0 
    gatsby-plugin-offline: next => 2.0.0-beta.2 
    gatsby-plugin-react-helmet: next => 3.0.0-beta.2 
    gatsby-plugin-robots-txt: ^1.0.2 => 1.0.2 
    gatsby-plugin-sharp: next => 2.0.0-beta.2 
    gatsby-plugin-sitemap: next => 2.0.0-beta.2 
    gatsby-plugin-webpack-bundle-analyzer: ^0.1.1 => 0.1.1 
    gatsby-source-filesystem: next => 2.0.1-beta.3 
    gatsby-transformer-sharp: next => 2.1.1-beta.2 
    gatsby-transformer-yaml: next => 2.1.1-beta.2 
  npmGlobalPackages:
    gatsby-cli: 2.0.0-beta.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:41 (30 by maintainers)

github_iconTop GitHub Comments

2reactions
Mesoptiercommented, Sep 28, 2018

Hi all! Not a Gatsby user here, but figured I’d chime in anyway since I have been dealing with a similar issue. According to this related issue on the style-loader package the FOUC can be fixed by disabling the sourceMap option for style-loader. (Or by enabling singleton, but I was still getting FOUC after doing so.)

1reaction
jlengstorfcommented, Jun 18, 2019

@sarneeh the page components get unmounted/remounted on each navigation, which is probably the culprit here.

I’d try this:

  1. Remove the styles from the page (here: https://github.com/sarneeh/gatsby-issue-6211/blob/master/src/pages/index.js#L3)
  2. Create a gatsby-browser.js in the project root and import them there instead:
    import './src/styles/index.css';
    

This will make the styles globally available and won’t unmount/remount them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

style-loader - webpack
Automatically injects styles into the DOM using multiple <style></style> . It is default behaviour. ... import styles from "./styles.css"; const divElement = ...
Read more >
A tale of Webpack 4 and how to finally configure it in the right ...
Webpack itself has been evolving really fast and a lot of loaders and plugins ... In webpack 4 you have modes: production and...
Read more >
Introduction to modern Web development with Webpack 4
We need to install 2 NPM packages: npm i css-loader style-loader -D. css-loader is quite obvious, it allows Webpack to read and transform...
Read more >
How to Create a Production-Ready Webpack 4 Config From ...
We'll use this in our production config while still just using style-loader in our development config. First, let's install the dependency in ...
Read more >
Webpack 2 Finalized with Focus on Improved Documentation
use: [ "style-loader", "css-loader", "less-loader" ]. Because webpack 2 supports ES2015 modules out of the box, it supports tree shaking by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found