A webpack plugin to allow preloading or prefetching of fonts.
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 />
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.
Type: String
Default: index.html
Name of the index file which needs modification.
// in your webpack.config.js
new FontPreloadPlugin({
index: "index.html",
});
Type: Array
Default: ['woff', 'ttf', 'eot']
Default font extensions which should be used.
// in your webpack.config.js
new FontPreloadPlugin({
extensions: ["woff", "ttf", "eot"],
});
Type: Boolean
Default: true
Is the font request crossorigin or not.
// in your webpack.config.js
new FontPreloadPlugin({
crossorigin: true,
});
Type: String
Default: preload
Type of load. It can be either preload
or prefetch
.
// in your webpack.config.js
new FontPreloadPlugin({
loadType: "preload",
});
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)",
});
Type: Function
Default: undefined
Callback for doing custom manipulations to index.html for special use cases like templating or server side rendering. This callback would be passed an object
as parameter with 2 keys:
indexSource
: Full source string of the index.html.linksAsString
:<link>
tags for preloading fonts as a string.
The consuming app can use this information to generate the final index.html and must return an updated string which would be used as index.html after webpack build.
// in your webpack.config.js
new FontPreloadPlugin({
replaceCallback: ({ indexSource, linksAsString }) => {
return indexSource.replace("{{{links}}}", linksAsString);
},
});