Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dburles/modulepreload-koa
Koa middleware for generating modulepreload link relations based on a JavaScript module import graph.
https://github.com/dburles/modulepreload-koa
esm esmodules header http koa link middleware modulepreload
Last synced: 3 months ago
JSON representation
Koa middleware for generating modulepreload link relations based on a JavaScript module import graph.
- Host: GitHub
- URL: https://github.com/dburles/modulepreload-koa
- Owner: dburles
- License: mit
- Created: 2023-10-05T09:41:09.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-31T05:51:09.000Z (5 months ago)
- Last Synced: 2024-10-01T02:04:44.116Z (3 months ago)
- Topics: esm, esmodules, header, http, koa, link, middleware, modulepreload
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/modulepreload-koa
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# modulepreload-koa
Koa middleware for generating [modulepreload](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/modulepreload) link relations for a [Link](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link) entity-header based on the requested JavaScript modules import graph. This will prevent request waterfalls for nested module imports. Supports import maps.
## Install
```sh
npm i modulepreload-koa
```## Usage example
```js
import Koa from "koa";
import serve from "koa-static";
import createModulePreloadMiddleware from "modulepreload-koa/createModulePreloadMiddleware.mjs";const app = new Koa();
const APP_ROOT = "app";
app.use(createModulePreloadMiddleware(APP_ROOT));
app.use(serve(APP_ROOT));app.listen(3000);
```## API
### `createModulePreloadMiddleware(path[, options])`
- `path` {string} Path to the application root directory, eg "app".
- `options` {Object}
- `extensions` {Array<string>} The file extensions to consider for module scripts. Defaults to: `["mjs", "js"]`.
- `importMap` {string} Import map string.
- `cache` {Object} A custom (map-like) cache.