{"library":"netjet","title":"Netjet Express Preload Middleware","description":"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.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install netjet"],"cli":null},"imports":["const netjet = require('netjet');","/** @typedef {import('netjet').NetjetOptions} NetjetOptions */","app.use(netjet({ directives: ['nopush'] }));"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const express = require('express');\nconst netjet = require('netjet');\nconst path = require('path');\nconst fs = require('fs');\nconst app = express();\nconst port = 1337;\nconst root = path.join(__dirname, 'public'); // Assuming a 'public' folder\n\n// Create a 'public' directory and dummy files if they don't exist\nif (!fs.existsSync(root)) {\n  fs.mkdirSync(root);\n}\nfs.writeFileSync(path.join(root, 'style.css'), 'body { color: blue; }');\nfs.writeFileSync(path.join(root, 'image.png'), '/* dummy image content */');\nfs.writeFileSync(path.join(root, 'script.js'), 'console.log(\"Script loaded!\");');\n\napp\n  .use(netjet({\n    // Enable parsing for images, scripts, styles (often true by default, but explicit for clarity)\n    images: true,\n    scripts: true,\n    styles: true,\n    // Example option from v1.3.0 notes to add custom attributes\n    directives: ['nopush'],\n    // Recommended: configure an LRU cache to prevent unbounded memory usage\n    cache: { max: 500 }\n  }))\n  .use(express.static(root)) // Serve static files after Netjet has a chance to process responses\n  .get('/', (req, res) => {\n    res.send(`\n      <!DOCTYPE html>\n      <html>\n      <head>\n        <title>Netjet Demo</title>\n        <link rel=\"stylesheet\" href=\"/style.css\">\n      </head>\n      <body>\n        <h1>Hello from Netjet!</h1>\n        <img src=\"/image.png\" alt=\"demo image\">\n        <script src=\"/script.js\"></script>\n      </body>\n      </html>\n    `);\n  })\n  .listen(port, () => {\n    console.log(`Netjet demo app listening on http://localhost:${port}`);\n    console.log(`Open http://localhost:${port} in your browser and inspect network/headers for preload links.`);\n  });\n","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}