Avoid fsstat on every request, remove koa-static (#4387)
* Avoid fsstat on every request, remove koa-static * tsx * Move compression middleware
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import path from "path";
|
||||
import Koa, { BaseContext } from "koa";
|
||||
import compress from "koa-compress";
|
||||
import Router from "koa-router";
|
||||
import send from "koa-send";
|
||||
import serve from "koa-static";
|
||||
import userAgent, { UserAgentContext } from "koa-useragent";
|
||||
import { languages } from "@shared/i18n";
|
||||
import env from "@server/env";
|
||||
@@ -16,16 +16,31 @@ const isProduction = env.ENVIRONMENT === "production";
|
||||
const koa = new Koa();
|
||||
const router = new Router();
|
||||
|
||||
// serve public assets
|
||||
koa.use(
|
||||
serve(path.resolve(__dirname, "../../../public"), {
|
||||
// 1 month expiry, these assets are mostly static but do not contain a hash
|
||||
maxAge: 30 * 24 * 60 * 60 * 1000,
|
||||
})
|
||||
);
|
||||
|
||||
koa.use<BaseContext, UserAgentContext>(userAgent);
|
||||
|
||||
// serve public assets
|
||||
router.use(["/images/*", "/email/*"], async (ctx, next) => {
|
||||
let done;
|
||||
|
||||
if (ctx.method === "HEAD" || ctx.method === "GET") {
|
||||
try {
|
||||
done = await send(ctx, ctx.path, {
|
||||
root: path.resolve(__dirname, "../../../public"),
|
||||
// 7 day expiry, these assets are mostly static but do not contain a hash
|
||||
maxAge: 7 * 24 * 60 * 60 * 1000,
|
||||
});
|
||||
} catch (err) {
|
||||
if (err.status !== 404) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!done) {
|
||||
await next();
|
||||
}
|
||||
});
|
||||
|
||||
router.use(
|
||||
["/share/:shareId", "/share/:shareId/doc/:documentSlug", "/share/:shareId/*"],
|
||||
(ctx) => {
|
||||
@@ -66,6 +81,8 @@ if (isProduction) {
|
||||
});
|
||||
}
|
||||
|
||||
router.use(compress());
|
||||
|
||||
router.get("/locales/:lng.json", async (ctx) => {
|
||||
const { lng } = ctx.params;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user