disabled cache for frontend. We don't actually need it at this stage, and without a better logic we can't hot swap the files
Massimo Melina committed
Dec 21, 2021 at 14:28 UTC
a163d56fae0d0a8329a659b2c092b08a6c34c27a
2 files changed
+9
-25
src/MemoMap.ts
deleted
-12
@@ -1,12 +0,0 @@
1
-export default class MemoMap<T> {
2
- map = new Map()
3
- getOrSet(k: any, maker: () => T) : T {
4
- let ret = this.map.get(k)
5
- if (!ret) {
6
- ret = maker()
7
- this.map.set(k, ret)
8
- }
9
- return ret
10
- }
11
-}
12
-
src/serveFrontend.ts
+9
-13
@@ -1,6 +1,5 @@
1
import proxy from 'koa-better-http-proxy'
2
import Koa from 'koa'
3
-import MemoMap from './MemoMap'
3
import mime from 'mime-types'
4
import { createReadStream, readFile } from 'fs'
5
import { DEV, FRONTEND_URI } from './const'
@@ -20,19 +19,16 @@ function serveProxyFrontend() {
19
20
function serveStaticFrontend() : Koa.Middleware {
21
const BASE = __dirname + (DEV ? '/../dist' : '') + '/frontend/'
23
- const cache = new MemoMap()
22
return async (ctx, next) => {
25
- let file = ctx.path
26
- if (file.endsWith('/'))
27
- file = '/'
28
- if (file.startsWith('/'))
29
- file = file.slice(1)
30
- const untouched = Boolean(file)
31
- ctx.body = untouched ? createReadStream(BASE + file)
32
- : await cache.getOrSet(file, () =>
33
- filePromise(BASE + (file || 'index.html')).then(res =>
34
- replaceFrontEndRes(res.toString('utf8')) ))
35
- ctx.type = file ? (mime.lookup(file) || 'application/octet-stream') : 'html'
23
+ let { path } = ctx
24
+ if (path.endsWith('/')) {
25
+ const res = await filePromise(BASE + 'index.html')
26
+ ctx.body = replaceFrontEndRes(res.toString('utf8'))
27
+ ctx.type = 'html'
28
+ } else {
29
+ ctx.body = createReadStream(BASE + path.slice(1))
30
+ ctx.type = mime.lookup(path) || 'application/octet-stream'
31
+ }
32
await next()
33
}
34
}