diff --git a/app/srv/api/prod.ts b/app/srv/api/prod.ts index 18694f98..fcca69c0 100644 --- a/app/srv/api/prod.ts +++ b/app/srv/api/prod.ts @@ -12,6 +12,7 @@ import { g } from "utils/global"; import brotliPromise from "brotli-wasm"; import mime from "mime"; +const encoder = new TextEncoder(); export const _ = { url: "/prod/:site_id/**", async api() { @@ -255,10 +256,15 @@ export const _ = { g.br = await brotliPromise; } setTimeout(() => { - g.route_cache[site_id] = g.br.compress(res); + if (!g.route_cache_compressing) + g.route_cache_compressing = new Set(); + if (g.route_cache_compressing.has(site_id)) return; + g.route_cache_compressing.add(site_id); + g.route_cache[site_id] = g.br.compress(encoder.encode(res)); + g.route_cache_compressing.delete(site_id); }, 100); - return new Response(res, { + return new Response(await gzipAsync(res), { headers: { "content-type": "application/json", "content-encoding": "gzip", diff --git a/pkgs/core/utils/global.ts b/pkgs/core/utils/global.ts index 934f3c64..f53a9491 100644 --- a/pkgs/core/utils/global.ts +++ b/pkgs/core/utils/global.ts @@ -11,6 +11,7 @@ import { apiProxy, } from "../../../app/web/src/base/load/api/api-proxy"; import { dbProxy } from "../../../app/web/src/base/load/db/db-proxy"; +import { BrotliWasmType } from "brotli-wasm"; type SingleRoute = { url: string; @@ -59,11 +60,12 @@ export const g = global as unknown as { Y: typeof Y; syncronize: typeof syncronize; static_cache: any; + route_cache_compressing: Set; route_cache: Record; code_index_compressing: Set; code_index_cache: Record< string, Record >; - br: any + br: BrotliWasmType };