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:
Tom Moor
2022-11-05 06:50:46 -07:00
committed by GitHub
parent bac1c9dc14
commit 9936f42882
14 changed files with 36 additions and 48 deletions

View File

@@ -6,7 +6,6 @@ import "./logging/tracing"; // must come before importing any instrumented modul
import http from "http";
import https from "https";
import Koa from "koa";
import compress from "koa-compress";
import helmet from "koa-helmet";
import logger from "koa-logger";
import onerror from "koa-onerror";
@@ -81,7 +80,6 @@ async function start(id: number, disconnect: () => void) {
app.use(logger((str) => Logger.info("http", str)));
}
app.use(compress());
app.use(helmet());
// catch errors in one place, automatically set status and response headers

View File

@@ -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;

View File

@@ -17,13 +17,13 @@
<link
rel="shortcut icon"
type="image/png"
href="/favicon-32.png"
href="/images/favicon-32.png"
sizes="32x32"
/>
<link
rel="apple-touch-icon"
type="image/png"
href="/apple-touch-icon.png"
href="/images/apple-touch-icon.png"
sizes="192x192"
/>
<link

View File

@@ -4,7 +4,7 @@ export const opensearchResponse = (baseUrl: string): string => {
<ShortName>Outline</ShortName>
<Description>Search Outline</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16" type="image/x-icon">${baseUrl}/favicon.ico</Image>
<Image width="16" height="16" type="image/x-icon">${baseUrl}/images/favicon-16.png</Image>
<Url type="text/html" method="get" template="${baseUrl}/search/{searchTerms}?ref=opensearch"/>
<moz:SearchForm>${baseUrl}/search</moz:SearchForm>
</OpenSearchDescription>