From 029869f8a07e89938fac216ed2a9ab2d13780c37 Mon Sep 17 00:00:00 2001 From: Maddison Hellstrom Date: Thu, 19 Nov 2020 23:39:27 -0800 Subject: [PATCH] [tools/gulp-serve:feat] make responses cacheable --- gulpfile.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index bb3979f..4075aa8 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -333,11 +333,18 @@ const serve = (done) => { const handler = (allowedOrigin) => async (req, res) => { console.log(`${new Date().toISOString()} ${req.method} ${req.url}`) // eslint-disable-line no-console - res.writeHead(200, { - "Content-Type": "text/javascript; charset=UTF-8", - "Access-Control-Allow-Origin": allowedOrigin, - }) - res.end(await fs.readFile(path.join("build", paths.scriptOut))) + try { + res.sendFile(path.resolve(__dirname, "build", paths.scriptOut), { + headers: { + "Content-Type": "text/javascript; charset=UTF-8", + "Access-Control-Allow-Origin": allowedOrigin, + }, + maxAge: 2000, + }) + } catch (e) { + console.log(e) // eslint-disable-line no-console + res.status(500).send("Error reading config file.\n") + } } app.get("/", handler("chrome-extension://mffcegbjcdejldmihkogmcnkgbbhioid"))