A utility for generating modulepreload link relations based on a JavaScript modules import graph. This will prevent module request waterfalls.
It can be used for HTTP server middleware and generating <link> elements in static HTML.
npm i modulepreload-link-relations
This package exports two functions:
resolveLinkRelations
- Returns an array of modules that can be preloaded for a module. An in-memory cache persists the resulting module import graph.
formatLinkHeaderRelations
- A formatter that can be used to generate link relations for an HTTP Link entity-header.
import resolveLinkRelations from "modulepreload-link-relations/resolveLinkRelations.mjs";
import formatLinkHeaderRelations from "modulepreload-link-relations/formatLinkHeaderRelations.mjs";
const linkRelations = await resolveLinkRelations({
// The application path.
appPath: "./app",
// The requested module.
url: "/lib/a.js",
}); // => ['/lib/c.js', '/lib/d.js']
// Optionally format the result:
const formattedLinkRelations = formatLinkHeaderRelations(linkRelations); // => </lib/c.js>; rel="modulepreload", </lib/d.js>; rel="modulepreload"
Middleware is available for the following Node.js servers:
- Express - modulepreload-express
- Koa - modulepreload-koa