From c6faa6079a49502b204607f1ba9cae9b72ef69f7 Mon Sep 17 00:00:00 2001 From: Tony Lea Date: Tue, 27 Feb 2024 17:35:29 -0500 Subject: [PATCH] Adding updates and fixes to allow custom headers --- package.json | 2 +- src/dev.js | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 082251d..16e4902 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@devdojo/static", - "version": "1.0.25", + "version": "1.0.26", "description": "Static Site Generator", "main": "index.js", "scripts": { diff --git a/src/dev.js b/src/dev.js index c2a1dac..86f910d 100644 --- a/src/dev.js +++ b/src/dev.js @@ -48,20 +48,26 @@ module.exports = { }); // TODO: Allow user to specify setting headers in the dev server via a config file - // app.use(function(req, res, next) { - // res.setHeader("Cross-Origin-Opener-Policy", "same-origin"); - // res.setHeader("Cross-Origin-Embedder-Policy", "require-corp"); - // next(); - // }); + let staticJSON = {}; + + const staticJsonPath = path.join(currentDirectory, 'static.json'); + if (fs.existsSync(staticJsonPath)) { + const staticJsonContent = fs.readFileSync(staticJsonPath, 'utf8'); + staticJSON = JSON.parse(staticJsonContent); + } + + app.use(function(req, res, next) { + if (staticJSON.hasOwnProperty('headers')) { + for (const header in staticJSON.headers) { + res.setHeader(header, staticJSON.headers[header]); + } + } + next(); + }); app.use(connectLiveReload(liveReloadOptions)); - // let options = { - // setHeaders: function (res, path, stat) { - // res.set("Cross-Origin-Opener-Policy", "same-origin"); - // res.set("Cross-Origin-Embedder-Policy", "require-corp"); - // } - // } + app.use('/assets', express.static(path.join(currentDirectory, '_site/assets'))) app.use('/', express.static(path.join(currentDirectory, 'public/')))