fix: frontend css and js didn't support caching
Massimo Melina committed
Jan 15, 2022 at 19:31 UTC
c4c79726a9380453dac2597d2d48c86c60813076
2 files changed
+9
-9
src/serveFile.ts
+4
-1
@@ -14,7 +14,7 @@ export function serveFileNode(node: VfsNode) : Koa.Middleware {
14
return serveFile(source||'', mime)
15
}
16
17
-export function serveFile(source:string, mime?:string) : Koa.Middleware {
17
+export function serveFile(source:string, mime?:string, modifier?:(s:string)=>string) : Koa.Middleware {
18
return async (ctx) => {
19
if (!source)
20
return
@@ -40,6 +40,9 @@ export function serveFile(source:string, mime?:string) : Koa.Middleware {
40
ctx.status = 200
41
if (ctx.fresh)
42
return ctx.status = 304
43
+
44
+ if (modifier)
45
+ return ctx.body = modifier(String(await fs.readFile(source)))
46
if (!range) {
47
ctx.body = createReadStream(source)
48
ctx.response.length = stats.size
src/serveFrontend.ts
+5
-8
@@ -30,18 +30,15 @@ function serveStaticFrontend() : Koa.Middleware {
30
}
31
if (method !== 'GET')
32
return ctx.status = METHOD_NOT_ALLOWED
33
- if (path.endsWith('/')) {
33
+ if (path.endsWith('/')) { // we don't cache the index as it's small and may prevent plugins change to apply
34
ctx.body = treatIndex(String(await fs.readFile(BASE + 'index.html')))
35
ctx.type = 'html'
36
} else {
37
const fullPath = BASE + path.slice(1)
38
- if (path.includes('static/js')) {// webpack
39
- ctx.body = String(await fs.readFile(fullPath))
40
- .replace(/(return")(static\/)/g, '$1' + FRONTEND_URI.substring(1) + '$2')
41
- ctx.type = 'js'
42
- }
43
- else
44
- return serveFile(fullPath, 'auto')(ctx, next)
38
+ const modifier = path.includes('static/js') ? // webpack
39
+ (s:string) => s.replace(/(return")(static\/)/g, '$1' + FRONTEND_URI.substring(1) + '$2')
40
+ : undefined
41
+ return serveFile(fullPath, 'auto', modifier)(ctx, next)
42
}
43
await next()
44
}