lws-conditional-get middleware

raw JSON →
3.0.0 verified Thu Apr 23 auth: no javascript

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.

error Error: Cannot find module 'lws' or 'lws-conditional-get'
cause The required `lws` or `lws-conditional-get` package has not been installed, or the Node.js module resolution path is incorrect.
fix
Run npm install lws lws-conditional-get in your project directory to ensure all dependencies are present.
error TypeError: conditionalGet is not a function
cause This error typically occurs if you're attempting to import `lws-conditional-get` using named imports (e.g., `import { conditionalGet } from 'lws-conditional-get'`) when the package exports a default function.
fix
Use a default import for ESM (import conditionalGet from 'lws-conditional-get') or directly require the module for CommonJS (const conditionalGet = require('lws-conditional-get')).
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.
fix If unexpected caching occurs, ensure the `--no-conditional-get` flag is not present. For explicit control, you might need to manage the middleware's inclusion in the `lws` stack programmatically.
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.
fix If experiencing unexpected behavior, consult the release notes and issue trackers for both `lws-conditional-get` and `koa-conditional-get` for any known incompatibilities or required updates.
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.
fix Always review the release notes for major `lws` versions and update your `lws` configuration and middleware instantiation patterns accordingly.
npm install lws-conditional-get
yarn add lws-conditional-get
pnpm add lws-conditional-get

This quickstart demonstrates how to set up an `lws` server with `lws-conditional-get` middleware to enable HTTP Conditional-GET caching for static files.

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`