Skip to content

Commit 6713c52

Browse files
authored
Add --cache option for npm run dev server (#574)
- --cache without args uses cache-control 3600 header - --cache=DURATION for explcit cache duration - use --cache for test server to speed up runs a bit
1 parent 663dc26 commit 6713c52

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

tests/helper.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import commandLineUsage from "command-line-usage";
22
import commandLineArgs from "command-line-args";
3-
import serve from "./server.mjs";
3+
import serve, { DEFAULT_CACHE_DURATION } from "./server.mjs";
44

55
import firefox from "selenium-webdriver/firefox.js";
66
import chrome from "selenium-webdriver/chrome.js";
@@ -73,7 +73,8 @@ export default async function testSetup(helpText) {
7373
}
7474
}
7575
const PORT = options.port;
76-
const server = await serve(PORT);
76+
77+
const server = await serve(PORT, DEFAULT_CACHE_DURATION);
7778
let driver, logInspector;
7879

7980
process.on("unhandledRejection", (err) => {

tests/server.mjs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,34 @@ import "lws-log";
1010
import "lws-static";
1111

1212
const ROOT_DIR = path.join(process.cwd(), "./");
13+
export const DEFAULT_CACHE_DURATION = 3600;
1314

14-
export default async function serve(port) {
15+
class CacheControlPlugin {
16+
middleware(config) {
17+
return async (ctx, next) => {
18+
ctx.set("Cache-Control", `max-age=${config.cache}`);
19+
await next();
20+
};
21+
}
22+
}
23+
24+
export default async function serve(port, cacheDuration) {
1525
if (!port)
1626
throw new Error("Port is required");
27+
28+
const stack = ["lws-log", "lws-cors", "lws-static", "lws-index"];
29+
if (cacheDuration !== undefined)
30+
stack.unshift(CacheControlPlugin);
31+
1732
const ws = await LocalWebServer.create({
1833
port: port,
1934
hostname: "127.0.0.1",
2035
directory: ROOT_DIR,
2136
corsOpenerPolicy: "same-origin",
2237
corsEmbedderPolicy: "require-corp",
2338
logFormat: "dev",
24-
stack: ["lws-log", "lws-cors", "lws-static", "lws-index"],
39+
stack,
40+
cache: cacheDuration,
2541
});
2642
await verifyStartup(ws, port);
2743

@@ -51,9 +67,16 @@ async function verifyStartup(ws, port) {
5167
}
5268

5369
function main() {
54-
const optionDefinitions = [{ name: "port", type: Number, defaultValue: 8080, description: "Set the test-server port, The default value is 8080." }];
70+
const optionDefinitions = [
71+
{ name: "port", type: Number, defaultValue: 8080, description: "Set the test-server port, The default value is 8080." },
72+
{ name: "cache", type: Number, description: `Set the cache duration in seconds. If flag is present without a value, defaults to ${DEFAULT_CACHE_DURATION}.` },
73+
];
5574
const options = commandLineArgs(optionDefinitions);
56-
serve(options.port);
75+
let cacheDuration = undefined;
76+
if ("cache" in options)
77+
cacheDuration = options.cache ?? DEFAULT_CACHE_DURATION;
78+
79+
serve(options.port, cacheDuration);
5780
}
5881

5982
if (esMain(import.meta))

0 commit comments

Comments
 (0)