All Projects → GoogleChromeLabs → Preload Webpack Plugin

GoogleChromeLabs / Preload Webpack Plugin

Licence: apache-2.0
Please use https://github.com/vuejs/preload-webpack-plugin instead.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Preload Webpack Plugin

Guess
🔮 Libraries & tools for enabling Machine Learning driven user-experiences on the web
Stars: ✭ 6,762 (+211.04%)
Mutual labels:  performance, prefetch
Bundle Buddy Webpack Plugin
🐐🐐🐐🐐 bundle-buddy-webpack-plugin 🐐🐐🐐🐐
Stars: ✭ 199 (-90.85%)
Mutual labels:  webpack-plugin, performance
Quicklink
⚡️Faster subsequent page-loads by prefetching in-viewport links during idle time
Stars: ✭ 9,176 (+322.08%)
Mutual labels:  performance, prefetch
wp-better-resource-hints
A WordPress plugin to help better manage resource hinting (preloading, prefetching, server pushing).
Stars: ✭ 18 (-99.17%)
Mutual labels:  preload, prefetch
Webpack.js.org
Repository for webpack documentation and more!
Stars: ✭ 2,049 (-5.75%)
Mutual labels:  webpack-plugin, performance
Emojify Webpack Plugin
🦄 Turn your code into emoji
Stars: ✭ 178 (-91.81%)
Mutual labels:  webpack-plugin
Scouter Paper
scouter-paper is a web client software for scouter
Stars: ✭ 183 (-91.58%)
Mutual labels:  performance
Performance Plugin
Performance Test Running and Reporting for Jenkins CI
Stars: ✭ 176 (-91.9%)
Mutual labels:  performance
Browser Interaction Time
⏰ A JavaScript library (written in TypeScript) to measure the time a user is active on a website
Stars: ✭ 175 (-91.95%)
Mutual labels:  performance
Fast Histogram
⚡️ Fast 1D and 2D histogram functions in Python ⚡️
Stars: ✭ 187 (-91.4%)
Mutual labels:  performance
Easy Monitor
企业级 Node.js 应用性能监控与线上故障定位解决方案
Stars: ✭ 2,451 (+12.74%)
Mutual labels:  performance
Clusterrunner
ClusterRunner makes it easy to parallelize test suites across your infrastructure in the fastest and most efficient way possible.
Stars: ✭ 181 (-91.67%)
Mutual labels:  performance
Vue Grid Canvas
A vue component, it can generate a similar excel form for you
Stars: ✭ 179 (-91.77%)
Mutual labels:  performance
Xdebug Handler
Restart a CLI process without loading the xdebug extension.
Stars: ✭ 2,276 (+4.69%)
Mutual labels:  performance
Torchfunc
PyTorch functions and utilities to make your life easier
Stars: ✭ 177 (-91.86%)
Mutual labels:  performance
Front End Performance Checklist
🎮 The only Front-End Performance Checklist that runs faster than the others
Stars: ✭ 13,815 (+535.46%)
Mutual labels:  performance
Antd Scss Theme Plugin
A Webpack plugin for customizing Ant Design with an SCSS theme file and using Ant Design's compiled variables in SCSS files throughout your project.
Stars: ✭ 176 (-91.9%)
Mutual labels:  webpack-plugin
Android article
Android热更新、异步并发、性能优化、编译打包、适配相关等文档 by yang。huh...The palest ink is better than the best memory.
Stars: ✭ 181 (-91.67%)
Mutual labels:  performance
Myperf4j
High performance Java APM. Powered by ASM. Try it. Test it. If you feel its better, use it.
Stars: ✭ 2,281 (+4.92%)
Mutual labels:  performance
Lighthouse Check Action
GitHub Action for running @GoogleChromeLabs Lighthouse audits with all the bells and whistles 🔔 Multiple audits, Slack notifications, and more!
Stars: ✭ 175 (-91.95%)
Mutual labels:  performance

preload-webpack-plugin

DEPRECATED: A fork of this project, https://github.com/vuejs/preload-webpack-plugin can be used instead.

A webpack plugin for automatically wiring up asynchronous (and other types) of JavaScript chunks using <link rel='preload'>. This helps with lazy-loading.

Note: This is an extension plugin for html-webpack-plugin - a plugin that simplifies the creation of HTML files to serve your webpack bundles.

Introduction

Preload is a web standard aimed at improving performance and granular loading of resources. It is a declarative fetch that can tell a browser to start fetching a source because a developer knows the resource will be needed soon. Preload: What is it good for? is a recommended read if you haven't used the feature before.

In simple web apps, it's straight-forward to specify static paths to scripts you would like to preload - especially if their names or locations are unlikely to change. In more complex apps, JavaScript can be split into "chunks" (that represent routes or components) at with dynamic names. These names can include hashes, numbers and other properties that can change with each build.

For example, chunk.31132ae6680e598f8879.js.

To make it easier to wire up async chunks for lazy-loading, this plugin offers a drop-in way to wire them up using <link rel='preload'>.

Prerequisites

This module requires webpack v4 and above. It also requires that you're using html-webpack-plugin in your webpack project.

Installation

First, install the package as a dependency in your package.json:

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

Usage

In your webpack config, require() the preload plugin as follows:

const PreloadWebpackPlugin = require('preload-webpack-plugin');

and finally, add the plugin to your webpack configuration's plugins array after HtmlWebpackPlugin:

plugins: [
  new HtmlWebpackPlugin(),
  new PreloadWebpackPlugin()
]

When preloading files, the plugin will use different as attribute depends on the type of each file. For each file ends with .css, the plugin will preload it with as=style, for each file ends with .woff2, the plugin will preload it with as=font, while for all other files, as=script will be used.

If you do not prefer to determine as attribute depends on suffix of filename, you can also explicitly name it using as:

plugins: [
  new HtmlWebpackPlugin(),
  new PreloadWebpackPlugin({
    rel: 'preload',
    as: 'script'
  })
]

In case you need more fine-grained control of the as attribute, you could also provide a function here. When using it, entry name will be provided as the parameter, and function itself should return a string for as attribute:

plugins: [
  new HtmlWebpackPlugin(),
  new PreloadWebpackPlugin({
    rel: 'preload',
    as(entry) {
      if (/\.css$/.test(entry)) return 'style';
      if (/\.woff$/.test(entry)) return 'font';
      if (/\.png$/.test(entry)) return 'image';
      return 'script';
    }
  })
]

Notice that if as=font is used in preload, the crossorigin will also be added. Explains can be found in this article, and a list of common as values can be found on MDN.

By default, the plugin will assume async script chunks will be preloaded. This is the equivalent of:

plugins: [
  new HtmlWebpackPlugin(),
  new PreloadWebpackPlugin({
    rel: 'preload',
    include: 'asyncChunks'
  })
]

For a project generating two async scripts with dynamically generated names, such as chunk.31132ae6680e598f8879.js and chunk.d15e7fdfc91b34bb78c4.js, the following preloads will be injected into the document <head>:

<link rel="preload" as="script" href="chunk.31132ae6680e598f8879.js">
<link rel="preload" as="script" href="chunk.d15e7fdfc91b34bb78c4.js">

You can also configure the plugin to preload all chunks (vendor, async, and normal chunks) using include: 'allChunks', or only preload initial chunks with include: 'initial'.

It is very common in webpack to use loaders such as file-loader to generate assets for specific types, such as fonts or images. If you wish to preload these files as well, even if they don't belong to a chunk, you can use include: 'allAssets'.

plugins: [
  new HtmlWebpackPlugin(),
  new PreloadWebpackPlugin({
    rel: 'preload',
    include: 'allChunks' // or 'initial', or 'allAssets'
  })
]

In case you work with named chunks, you can explicitly specify which ones to include by passing an array:

plugins: [
  new HtmlWebpackPlugin(),
  new PreloadWebpackPlugin({
    rel: 'preload',
    include: ['home']
  })
]

will inject just this:

<link rel="preload" as="script" href="home.31132ae6680e598f8879.js">

Filtering chunks

There may be chunks that you don't want to have preloaded (sourcemaps, for example). Before preloading each chunk, this plugin checks that the file does not match any regex in the fileBlacklist option. The default value of this blacklist is [/\.map/], meaning no sourcemaps will be preloaded. You can easily override this:

new PreloadWebpackPlugin({
  fileBlacklist: [/\.whatever/]
})

Passing your own array will override the default, so if you want to continue filtering sourcemaps along with your own custom settings, you'll need to include the regex for sourcemaps:

new PreloadWebpackPlugin({
  fileBlacklist: [/\.map/, /\.whatever/]
})

Filtering HTML

You may not want to preload resources in some of your HTML files. You can use excludeHtmlNames to tell this plugin to ignore one or more files.

plugins: [
  new HtmlWebpackPlugin({
    filename: 'index.html',
    template: 'src/index.html',
    chunks: ['main']
  }),
  new HtmlWebpackPlugin({
    filename: 'example.html',
    template: 'src/example.html',
    chunks: ['exampleEntry']
  }),
  // Only apply the plugin to index.html, not example.html.
  new PreloadWebpackPlugin({
    excludeHtmlNames: ['example.html'],
  })

Resource hints

Should you wish to use Resource Hints (such as prefetch) instead of preload, this plugin also supports wiring those up.

Prefetch:

plugins: [
  new HtmlWebpackPlugin(),
  new PreloadWebpackPlugin({
    rel: 'prefetch'
  })
]

For the async chunks mentioned earlier, the plugin would update your HTML to the following:

<link rel="prefetch" href="chunk.31132ae6680e598f8879.js">
<link rel="prefetch" href="chunk.d15e7fdfc91b34bb78c4.js">

Including media

<link> elements have the ability to accept media attributes. These can accept media types or full-blown media queries, allowing you to do responsive preloading.

You can pass the value for the media attribute in the media option:

plugins: [
  new HtmlWebpackPlugin(),
  new PreloadWebpackPlugin({
    rel: 'preload',
    media: '(min-width: 600px)'
  })
]

Support

If you've found an error or run into problems, please file an issue.

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub.

Contributing workflow

src/index.js and src/lib/ contains the primary source for the plugin. test/ contains tests.

Test the plugin:

$ npm install
$ npm run test

The project is written in ES2015, and is transpiled to support node 6 and above.

Additional notes

  • Be careful not to preload resources a user is unlikely to need. This can waste their bandwidth.
  • Use preload for the current session if you think a user is likely to visit the next page. There is no 100% guarantee preloaded items will end up in the HTTP Cache and read locally beyond this session.
  • If optimizing for future sessions, use prefetch and preconnect. Prefetched resources are maintained in the HTTP Cache for at least 5 minutes (in Chrome) regardless of the resource's cachability.

Alternative tools

License

Copyright 2019 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].