Giter Site home page Giter Site logo

sdtm1016 / critters Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlechromelabs/critters

0.0 2.0 0.0 126 KB

๐Ÿฆ” A Webpack plugin to inline your critical CSS and lazy-load the rest.

Home Page: https://npm.im/critters-webpack-plugin

License: Apache License 2.0

JavaScript 100.00%

critters's Introduction

critters-webpack-plugin

Critters

Critters is a Webpack plugin that inlines your app's critical CSS and lazy-loads the rest.

critters-webpack-plugin npm

It's a little different from other options, because it doesn't use a headless browser to render content. This tradeoff allows Critters to be very fast and lightweight. It also means Critters inlines all CSS rules used by your document, rather than only those needed for above-the-fold content. For alternatives, see Similar Libraries.

Critters' design makes it a good fit when inlining critical CSS for prerendered/SSR'd Single Page Applications. It was developed to be an excellent compliment to prerender-loader, combining to dramatically improve first paint time for most Single Page Applications.

Features

  • Fast - no browser, few dependencies
  • Integrates with html-webpack-plugin
  • Works with webpack-dev-server / webpack serve
  • Supports preloading and/or inlining critical fonts
  • Prunes unused CSS keyframes and media queries

Installation

First, install Critters as a development dependency:

npm i -D critters-webpack-plugin

Then, import Critters into your Webpack configuration and add it to your list of plugins:

// webpack.config.js
+const Critters = require('critters-webpack-plugin');

module.exports = {
  plugins: [
+    new Critters({
+      // optional configuration (see below)
+    })
  ]
}

That's it! Now when you run Webpack, the CSS used by your HTML will be inlined and the imports for your full CSS will be converted to load asynchrnously.

Usage

Critters

Create a Critters plugin instance with the given options.

Parameters

  • options Options Options to control how Critters inlines CSS.

Examples

// webpack.config.js
module.exports = {
  plugins: [
    new Critters({
      // Outputs: <link rel="preload" onload="this.rel='stylesheet'">
      preload: 'swap',

      // Don't inline critical font-face rules, but preload the font URLs:
      preloadFonts: true
    })
  ]
}

Options

All optional. Pass them to new Critters({ ... }).

Type: Object

Properties

  • external Boolean Inline styles from external stylesheets (default: true)
  • preload String Which preload strategy to use
  • noscriptFallback Boolean Add <noscript> fallback to JS-based strategies
  • inlineFonts Boolean Inline critical font-face rules (default: false)
  • preloadFonts Boolean Preloads critical fonts (default: true)
  • fonts Boolean Shorthand for setting inlineFonts+preloadFonts- values:
    • true to inline critical font-face rules and preload the fonts
    • false to don't inline any font-face rules and don't preload fonts
  • compress Boolean Compress resulting critical CSS (default: true)

PreloadStrategy

The mechanism to use for lazy-loading stylesheets. [JS] indicates that a strategy requires JavaScript (falls back to <noscript>).

  • default: Move stylesheet links to the end of the document and insert preload meta tags in their place.
  • "body": Move all external stylesheet links to the end of the document.
  • "media": Load stylesheets asynchronously by adding media="not x" and removing once loaded. [JS]
  • "swap": Convert stylesheet links to preloads that swap to rel="stylesheet" once loaded. [JS]
  • "js": Inject an asynchronous CSS loader similar to LoadCSS and use it to load stylesheets. [JS]
  • "js-lazy": Like "js", but the stylesheet is disabled until fully loaded.

Type: (default | "body" | "media" | "swap" | "js" | "js-lazy")

Similar Libraries

There are a number of other libraries that can inline Critical CSS, each with a slightly different approach. Here are a few great options:

License

Apache 2.0

This is not an official Google product.

critters's People

Contributors

developit avatar stereobooster avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.