Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Styles With Webpack causes styles to blink on initial load

Tags:

sass

webpack

I'm currently working on a project and Im attempting to load my scss/sass through webpack. I'm currently loading it in successfully using the following libs:

  • node-sass
  • sass-loader
  • css loader
  • style-loader

I am able to require/import the styles in successfully but the problem occurs that when I load up the application the page loads without the styles for about 1.5 seconds and then after the page "blinks" and the styles finally load in.

Is there a way to get around this through webpack? I have heard of ExtractTextPlugin and a few others but I've tried to implement it by looking at article examples and github examples but they don't seem to work by using require/import where they are needed. I'd like to only require the styles based on my react component needs. Not loading any styles that the components don't need.

like image 529
kennet postigo Avatar asked Mar 11 '16 16:03

kennet postigo


People also ask

What is webpack style-loader?

Loaders are the node-based utilities built for webpack to help webpack to compile and/or transform a given type of resource that can be bundled as a javascript module. css-loader is the npm module that would help webpack to collect CSS from all the css files referenced in your application and put it into a string.

How do I load a CSS file into webpack?

In the following code block, css-loader and style-loader are used together. Similar to babel-loader , we can load CSS files to style our pages like so: module: { rules: [ { test: /\\. js$/, loader: "babel-loader", exclude: "/node_modules/", }, // CSS rules { test: /\\.

How does style-loader work?

style-loader takes CSS you've imported in your JavaScript files, and injects them as <style></style> tags into the DOM. It's particularly useful for inlining Critical CSS into the <head> of your page.


1 Answers

You have (at least) two options to prevent this FOUC (Flash of unstyled content):

  1. Use a plugin like mini-css-extract-plugin or extract-text-webpack-plugin to extract the compiled CSS into a separate file that you can load normally in your <head> (read more) or
  2. Hide your content, using CSS, until styles are ready, the loaded styles should contain the styles that will make the content visible.
like image 56
tsi Avatar answered Nov 16 '22 03:11

tsi