lws-conditional-get middleware
raw JSON →lws-conditional-get is a specialized middleware designed for integration with the `lws` (local-web-server) ecosystem. Its core function is to add robust HTTP Conditional-GET caching capabilities to `lws`-powered web servers, leveraging the well-regarded `koa-conditional-get` library internally. This enables efficient resource delivery by utilizing `If-None-Match` and `If-Modified-Since` HTTP headers, thereby reducing redundant data transfers and improving client-side performance. The package is currently at version 3.0.0 and appears to be actively maintained, with a likely release cadence tied to updates in its upstream dependencies or the `lws` project itself. Its primary value lies in offering a low-configuration solution for implementing standard HTTP caching directly within the `lws` environment, abstracting away the complexities of conditional request handling.
Common errors
error Error: Cannot find module 'lws' or 'lws-conditional-get' ↓
npm install lws lws-conditional-get in your project directory to ensure all dependencies are present. error TypeError: conditionalGet is not a function ↓
import conditionalGet from 'lws-conditional-get') or directly require the module for CommonJS (const conditionalGet = require('lws-conditional-get')). Warnings
gotcha While `lws-conditional-get` is enabled by default if included in the `lws` stack, its behavior can be explicitly disabled via the `--no-conditional-get` command-line flag or equivalent configuration, which developers might overlook if expecting direct control. ↓
gotcha `lws-conditional-get` relies on `koa-conditional-get`. Issues or breaking changes in specific versions of `koa-conditional-get` could indirectly affect the behavior or stability of this middleware. Always check for upstream dependency release notes. ↓
breaking Upgrading `lws` itself to a new major version might introduce changes to its middleware API, potentially requiring updates to how `lws-conditional-get` is integrated or configured, even if `lws-conditional-get` itself hasn't had a major release. ↓
Install
npm install lws-conditional-get yarn add lws-conditional-get pnpm add lws-conditional-get Imports
- conditionalGet wrong
import { conditionalGet } from 'lws-conditional-get'correctimport conditionalGet from 'lws-conditional-get' - conditionalGet
const conditionalGet = require('lws-conditional-get') - lws-conditional-get (as part of lws stack)
const lws = require('lws'); lws.create({ stack: [ require('lws-conditional-get')() ] });
Quickstart
import lws from 'lws';
import conditionalGet from 'lws-conditional-get';
import fs from 'fs';
import path from 'path';
// Ensure a 'public' directory exists for static files
const publicDir = path.join(process.cwd(), 'public');
if (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir);
}
// Create some dummy files to serve
fs.writeFileSync(path.join(publicDir, 'index.html'), '<h1>Hello from conditional-get!</h1>\n');
fs.writeFileSync(path.join(publicDir, 'style.css'), 'body { font-family: sans-serif; }\n');
const server = lws.create({
port: 8000,
stack: [
conditionalGet(), // Include the conditional-get middleware
lws.static({ directory: publicDir }) // Serve static files from 'public'
]
});
server.listen(8000, () => {
console.log('lws-conditional-get demo server running on http://localhost:8000');
console.log('Access http://localhost:8000/index.html in your browser.');
console.log('Subsequent requests (e.g., refresh) should show ' + '"304 Not Modified" for static assets in network inspector.');
});
// To run this example:
// 1. Create a file named `server.js`
// 2. `npm install lws lws-conditional-get`
// 3. `node server.js`