fix: allow forcing static frontend while developing

Massimo Melina committed Dec 19, 2021 at 12:54 UTC ace83c87c4f448b97487bf19755c930645cedaaa
1 file changed +3 -4
src/frontend.ts
+3 -4
@@ -5,8 +5,6 @@ import mime from 'mime-types'
5 import { createReadStream, readFile } from 'fs'
6 import { DEV, FRONTEND_URI } from './const'
7
8 -const FRONTEND = __dirname + '/frontend/'
9 -
8 export const serveFrontend = DEV ? serveProxyFrontend() : serveStaticFrontend()
9
10 function serveProxyFrontend() {
@@ -21,15 +19,16 @@ function serveProxyFrontend() {
19 }
20
21 function serveStaticFrontend() : Koa.Middleware {
22 + const BASE = __dirname + (DEV ? '/../dist' : '') + '/frontend/'
23 const cache = new MemoMap()
24 return async (ctx, next) => {
25 let file = ctx.path
26 if (file.startsWith('/'))
27 file = file.slice(1)
28 const untouched = Boolean(file)
30 - ctx.body = untouched ? createReadStream(FRONTEND + file)
29 + ctx.body = untouched ? createReadStream(BASE + file)
30 : await cache.getOrSet(file, () =>
32 - filePromise(FRONTEND + (file || 'index.html')).then(res =>
31 + filePromise(BASE + (file || 'index.html')).then(res =>
32 replaceFrontEndRes(res.toString('utf8')) ))
33 ctx.type = file ? (mime.lookup(file) || 'application/octet-stream') : 'html'
34 await next()