Netjet is an Express.js middleware designed to automatically inject `<link rel="preload">` headers into HTML responses. These headers enable web browsers to perform early resource fetching, which can significantly improve page load performance by fetching critical assets like scripts, styles, and images before the browser's main parsing engine discovers them. The package is currently at version 1.4.0 and appears to be in an active maintenance state, with recent updates focusing on bug fixes and additional preload attributes. It differentiates itself by offering automatic, configurable scanning of HTML responses for subresources to preload, and leverages an `lru-cache` for efficient caching of resource analysis. Its primary use case is optimizing web application delivery for HTTP/2 and modern browsers.
npm install netjetVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to integrate Netjet middleware into an Express.js application to automatically generate `Link` preload headers for static assets like CSS, images, and JavaScript files, including custom attributes and cache configuration.
Regularly review release notes for updates and thoroughly test against new versions before deploying to production. Consider pinning to specific patch versions (`~1.x.x`) to mitigate unexpected changes.
Upgrade to `netjet@1.1.3` or higher to ensure proper URL encoding in `Link` headers.
Ensure `app.use(netjet(options))` is invoked early in your Express middleware chain, preceding any middleware responsible for serving the HTML content it needs to analyze.
Always configure a `cache.max` option with an appropriate integer value (e.g., `netjet({ cache: { max: 500 } })`) to prevent unbounded cache growth and potential out-of-memory errors in long-running applications.You must invoke `netjet()` to obtain the middleware function, even if you don't pass any options. Correct usage is `app.use(netjet())` or `app.use(netjet({ /* options */ }))`.Verify that `netjet` is placed *before* your static file server (`express.static`) and other HTML-serving routes. Also, check the `netjet` options; ensure that `images: true`, `scripts: true`, and `styles: true` are explicitly set if you want those resources to be preloaded.
Ensure `netjet` is placed early in the middleware chain, specifically before any middleware or route handlers that might explicitly call `res.send()`, `res.end()`, or `res.setHeaders()` in a way that bypasses Netjet's interception mechanism.