Skip to content

A webpack plugin to allow preloading or prefetch of fonts.

License

Notifications You must be signed in to change notification settings

cato/webpack-font-preload-plugin

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm

webpack-font-preload-plugin

A webpack plugin to allow preloading or prefetching of fonts.

Introduction

The preload value of the <link> element's rel attribute lets you declare fetch requests in the HTML's <head>, specifying resources that your page will need very soon, which you want to start loading early in the page lifecycle, before browsers' main rendering machinery kicks in. This ensures they are available earlier and are less likely to block the page's render, improving performance.

This plugin specifically targets fonts used with the application which are bundled using webpack. The plugin would add <link> tags in the begining of <head> of your html:

<link rel="preload" href="/font1.woff" as="font" crossorigin>
<link rel="preload" href="/font2.woff" as="font" crossorigin>

Getting Started

To begin, you'll need to install webpack-font-preload-plugin:

$ npm install webpack-font-preload-plugin --save-dev

Then add the plugin to your webpack config. For example:

webpack.config.js

const FontPreloadPlugin = require('webpack-font-preload-plugin');

module.exports = {
  plugins: [new FontPreloadPlugin()],
};

And run webpack via your preferred method.

Options

index

Type: String Default: index.html

Name of the index file which needs modification.

// in your webpack.config.js
new FontPreloadPlugin({
  index: 'index.html',
});

extensions

Type: Array Default: ['woff', 'ttf', 'eot']

Default font extensions which should be used.

// in your webpack.config.js
new FontPreloadPlugin({
  extensions: ['woff', 'ttf', 'eot'],
});

crossorigin

Type: Boolean Default: true

Is the font request crossorigin or not.

// in your webpack.config.js
new FontPreloadPlugin({
  crossorigin: true,
});

loadType

Type: String Default: preload

Type of load. It can be either preload or prefetch.

// in your webpack.config.js
new FontPreloadPlugin({
  loadType: 'preload',
});

insertBefore

Type: String Default: head > title

The selector for node before which the preload/prefetch links should be added.

// in your webpack.config.js
new FontPreloadPlugin({
  // Add the preload statements before any other <link> tag present in html
  insertBefore: 'head > link:nth-child(1)',
});

License

MIT

About

A webpack plugin to allow preloading or prefetch of fonts.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%