How to stop FOUC when using css loaded by webpack


Tags: css,webpack,fouc

Problem :

I am getting FOUC when loading css inside of my entry point when using webpack. If I remove my css from being loaded by webpack and just include it in my html file as a normal link then the problem with FOUC goes away.

Note: This not just with bootstrap framework, I have tested with Foundation and Materialize with the same results

The code posted below is just a test example of my problem using Bootstrap.

Html code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<div class="container">
    <div class="jumbotron">
        <h1>Navbar example</h1>
    </div>
</div> <!-- /container -->

<script src="build/bundle.js"></script>
</body>
</html>

bootstrap.js main entry point

import "../node_modules/bootstrap/dist/css/bootstrap.css";
import bootstrap from 'bootstrap'

$(document).ready(function () {
   console.log('bootstrap loaded')
});

webpack.config.js

var path = require('path');
const ProvidePlugin = require('webpack/lib/ProvidePlugin');
const webpack = require("webpack");

module.exports = {
  entry: './src/bootstrap.js',
  output: {
    path: path.join(__dirname, 'build'),
    filename: 'bundle.js'
  },
    resolve: {
        extensions: ['', '.js']
    },
    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            'window.jQuery': 'jquery'
        })
    ],
  devtool: 'inline-source-map',
  module: {
      resolve: {
          modulesDirectories: ['node_modules']
      },
    loaders: [
      {
        test: path.join(__dirname, 'src'),
        loader: 'babel-loader',
          query: {
              presets: ['es2015']
          }
      },
        { test: /\.css?$/, loader: 'style!css'},
        { test: /\.html$/, loader: 'html' },
        { test: /\.(png|gif|jpg)$/, loader: 'url', query: { limit: 8192 } },
        { test: /\.woff2(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url', query: { limit: 10000, mimetype: 'application/font-woff2' } },
        { test: /\.woff(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url', query: { limit: 10000, mimetype: 'application/font-woff' } },
        { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file' },
    ]
  }
};


Solution :

ExtractTextWebpackPlugin will allow you to output your CSS as a separate file rather than having it embedded in your JS bundle. You can then include this file in your HTML, which as you said, prevents the flash of unstyled content.

I'd recommend only using this in production environments, as it stops hot-loading from working and makes your compile take longer. I have my webpack.config.js set up to only apply the plugin when process.env.NODE_ENV === "production"; you still get the FOUC when you're doing a development build/running the dev server, but I feel like this is a fair trade off.

For more information on how to set this up, take a look at SurviveJS's guide.


    CSS Howto..

    Coding the slideshow

    How to apply different CSS style to child elements as they occur inside one another?

    How to automatically resize a div based on background image size?

    How to select 3rd div which has specific css class

    How to make JS & CSS inline work for eBay template?

    How to scale image block using only css?

    How to debug CSS when using GWT

    How to display CSS files in Django blog?

    Show popup after page load

    How to make div's width fit content not window

    How to make a
    container width and height for each same as “the value of clientWidth and clientHeight in maximized window”?

    how do you link static pages in rails to your css files? [closed]

    How to set background image for body tag using css?

    How to set a child's element 50px narrower to its parent element in css? [closed]

    How to centre a div vertically and horizontallly?

    How to place a div right below another div?

    How to set the CSS of an img inside a li inside a ul inside a class?

    how to make the file input field wrap?

    How to implement two sets of CSS Tabs on one page?

    Picture thumbnail shown in Google image search output is
    not , why? [closed]
    It basically comes down to preference. It is considered outdated to use table for data layouts. Use table for just that - actual tables. For the layout and such tasks,...
    Read more

    How to disable a YUI MenuBarItem from markup

    How to make a sliding dashboard similar to medium.com? [closed]

    How to shade every other table row that spans across multiple rows?

    How to use Css to make CellList flow Horizontally rather than Vertically? (GWT & CSS)

    How do I make a nested
    expand to its contents?

    How to divide content with CSS even, odd?

    PHP: How to add a CSS text-align property to an already present inline style attribute of an element using simple_html_dom parser?

    How to make text-overflow with ellipsis work with long strings that have no breaks

    How do I create bookmarklet to overlay html/div layer and CSS from external file

    How to extend this navigation bar off the page?

    using CSS?
    How to detect redraw rate (fps) of a css animation?
    How to rotate a picture using css and javascript
    How to align links on top of each other inside of a box with CSS?
    How can I create CSS sprites from images stored in the database?
    How to select span element inside multiple div classes and id's?
    2 Sidebar Columns Layout (How)?
    How could I center a div that is inside a div? Auto margins aren't working
    Font-family not showing correctly on my website
    CSS: How to toggle switch action on the link?
    How to set CSS class to Button control using Session value in design page
    How to overlap divs using CSS?
    How to make jquery autocomplete to not cover other content below it?
    show image in aspect ratio and all image size same height and width
    how to pass jquery variable value into css property value?
    how to make dropdown using html and css?