Use a proxy to set cache-control header on responses
This commit is contained in:
parent
1b04583ae8
commit
693d0c0501
@ -1,8 +1,16 @@
|
|||||||
// const esbuild = require('esbuild');
|
|
||||||
// const fs = require('node:fs');
|
|
||||||
|
|
||||||
import * as esbuild from 'esbuild';
|
import * as esbuild from 'esbuild';
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
|
import http from 'node:http';
|
||||||
|
|
||||||
|
const colors = {
|
||||||
|
reset: '\x1b[0m',
|
||||||
|
dim: '\x1b[2m',
|
||||||
|
bright: '\x1b[1m',
|
||||||
|
normal: '\x1b[22m',
|
||||||
|
red: '\x1b[31m',
|
||||||
|
green: '\x1b[32m',
|
||||||
|
yellow: '\x1b[33m',
|
||||||
|
}
|
||||||
|
|
||||||
const regex = new RegExp('mapsheets\..+\.svg');
|
const regex = new RegExp('mapsheets\..+\.svg');
|
||||||
|
|
||||||
@ -19,84 +27,10 @@ const svgUseCacheBust = {
|
|||||||
files.forEach(fn => fs.unlinkSync(`./public/assets/images/${fn}`));
|
files.forEach(fn => fs.unlinkSync(`./public/assets/images/${fn}`));
|
||||||
fs.copyFileSync('./public/assets/images/mapsheets.svg', `./public/assets/images/mapsheets.${version}.svg`);
|
fs.copyFileSync('./public/assets/images/mapsheets.svg', `./public/assets/images/mapsheets.${version}.svg`);
|
||||||
})
|
})
|
||||||
|
|
||||||
// version = Math.random();
|
|
||||||
// build.onResolve({ filter: /svg/ }, args => {
|
|
||||||
// console.log('onresolve', version, args);
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// {
|
const ctx = await esbuild.context({
|
||||||
// bundle: true,
|
|
||||||
// define: {
|
|
||||||
// 'env': `"${process.env.NODE_ENV || 'dev'}"`,
|
|
||||||
// },
|
|
||||||
// entryPoints: ['src/index.js', 'src/map.js', 'src/soldier_record_block.js'],
|
|
||||||
// // outdir: 'build',
|
|
||||||
// ...(process.env.NODE_ENV !== 'test') && {
|
|
||||||
// outdir: 'build'
|
|
||||||
// },
|
|
||||||
// assetNames: `[name]${version}`,
|
|
||||||
// plugins: [svgUseCacheBust],
|
|
||||||
// loader: {
|
|
||||||
// '.svg': 'file'
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
|
|
||||||
// let ctx = await esbuild.context({
|
|
||||||
// entryPoints: ['src/*.js'],
|
|
||||||
// outdir: 'public',
|
|
||||||
// bundle: true,
|
|
||||||
// plugins: [svgUseCacheBust],
|
|
||||||
// loader: {
|
|
||||||
// '.svg': 'file'
|
|
||||||
// },
|
|
||||||
// })
|
|
||||||
|
|
||||||
// let { host, port } = await ctx.serve({
|
|
||||||
// servedir: 'public',
|
|
||||||
// })
|
|
||||||
|
|
||||||
const colors = {
|
|
||||||
reset: '\x1b[0m',
|
|
||||||
dim: '\x1b[2m',
|
|
||||||
bright: '\x1b[1m',
|
|
||||||
normal: '\x1b[22m',
|
|
||||||
red: '\x1b[31m',
|
|
||||||
green: '\x1b[32m',
|
|
||||||
yellow: '\x1b[33m',
|
|
||||||
}
|
|
||||||
|
|
||||||
// esbuild.context({
|
|
||||||
// entryPoints: ['src/*.js'],
|
|
||||||
// outdir: 'public',
|
|
||||||
// bundle: true,
|
|
||||||
// plugins: [svgUseCacheBust],
|
|
||||||
// loader: {
|
|
||||||
// '.svg': 'file'
|
|
||||||
// },
|
|
||||||
// logLevel: 'info',
|
|
||||||
// }).then(ctx => {
|
|
||||||
// console.log('context', ctx);
|
|
||||||
|
|
||||||
// ctx.serve({
|
|
||||||
// servedir: 'public',
|
|
||||||
// onRequest: function({ remoteAddress, method, path, status, timeInMS }) {
|
|
||||||
// let statusColor = colors.red;
|
|
||||||
|
|
||||||
// if (status >= 200 && status <= 299) {
|
|
||||||
// statusColor = colors.green;
|
|
||||||
// } else if (status >= 300 && status <= 399) {
|
|
||||||
// statusColor = colors.yellow;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// console.log(`${colors.dim}${remoteAddress} - "${method} ${path}" ${colors.normal}${statusColor}${status}${colors.reset}${colors.dim} [${timeInMS}ms]${colors.reset}`);
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// });
|
|
||||||
|
|
||||||
let ctx = await esbuild.context({
|
|
||||||
entryPoints: ['src/*.js'],
|
entryPoints: ['src/*.js'],
|
||||||
bundle: true,
|
bundle: true,
|
||||||
outdir: 'public',
|
outdir: 'public',
|
||||||
@ -105,9 +39,9 @@ let ctx = await esbuild.context({
|
|||||||
|
|
||||||
await ctx.watch();
|
await ctx.watch();
|
||||||
|
|
||||||
let { host, port } = await ctx.serve({
|
const { host, port } = await ctx.serve({
|
||||||
servedir: 'public',
|
servedir: 'public',
|
||||||
port: 8080,
|
port: 3000,
|
||||||
onRequest: function({ remoteAddress, method, path, status, timeInMS }) {
|
onRequest: function({ remoteAddress, method, path, status, timeInMS }) {
|
||||||
let statusColor = colors.red;
|
let statusColor = colors.red;
|
||||||
|
|
||||||
@ -120,3 +54,25 @@ let { host, port } = await ctx.serve({
|
|||||||
console.log(`${colors.dim}${remoteAddress} - "${method} ${path}" ${colors.normal}${statusColor}${status}${colors.reset}${colors.dim} [${timeInMS}ms]${colors.reset}`);
|
console.log(`${colors.dim}${remoteAddress} - "${method} ${path}" ${colors.normal}${statusColor}${status}${colors.reset}${colors.dim} [${timeInMS}ms]${colors.reset}`);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
http.createServer((req, res) => {
|
||||||
|
const options = {
|
||||||
|
hostname: host,
|
||||||
|
port: port,
|
||||||
|
path: req.url,
|
||||||
|
method: req.method,
|
||||||
|
headers: req.headers,
|
||||||
|
}
|
||||||
|
|
||||||
|
const proxyReq = http.request(options, proxyRes => {
|
||||||
|
for (const k in proxyRes.headers) {
|
||||||
|
res.setHeader(k, proxyRes.headers[k]);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.writeHead(proxyRes.statusCode, { 'Cache-Control': 'no-cache, no-store, must-revalidate' });
|
||||||
|
|
||||||
|
proxyRes.pipe(res, { end: true });
|
||||||
|
})
|
||||||
|
|
||||||
|
req.pipe(proxyReq, { end: true });
|
||||||
|
}).listen(8080);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user