Skip to content

Commit

Permalink
Avoid fsstat on every request, remove koa-static (outline#4387)
Browse files Browse the repository at this point in the history
* Avoid fsstat on every request, remove koa-static

* tsx

* Move compression middleware
  • Loading branch information
tommoor committed Nov 5, 2022
1 parent bac1c9d commit 9936f42
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 47 deletions.
2 changes: 1 addition & 1 deletion app/components/PageTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const PageTitle = ({ title, favicon }: Props) => {
<link
rel="shortcut icon"
type="image/png"
href={cdnPath("/favicon-32.png")}
href={cdnPath("/images/favicon-32.png")}
sizes="32x32"
/>
)}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@
"koa-router": "7.4.0",
"koa-send": "5.0.1",
"koa-sslify": "2.1.2",
"koa-static": "^4.0.1",
"koa-useragent": "^4.1.0",
"lodash": "^4.17.21",
"mammoth": "^1.4.19",
Expand Down Expand Up @@ -252,8 +251,8 @@
"@types/koa-logger": "^3.1.2",
"@types/koa-mount": "^4.0.1",
"@types/koa-router": "^7.4.4",
"@types/koa-send": "^4.1.3",
"@types/koa-sslify": "^2.1.0",
"@types/koa-static": "^4.0.2",
"@types/koa-useragent": "^2.1.2",
"@types/markdown-it": "^12.2.3",
"@types/markdown-it-container": "^2.0.5",
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
2 changes: 0 additions & 2 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
33 changes: 25 additions & 8 deletions server/routes/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -16,15 +16,30 @@ const isProduction = env.ENVIRONMENT === "production";
const koa = new Koa();
const router = new Router();

koa.use<BaseContext, UserAgentContext>(userAgent);

// 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,
})
);
router.use(["/images/*", "/email/*"], async (ctx, next) => {
let done;

koa.use<BaseContext, UserAgentContext>(userAgent);
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/*"],
Expand Down Expand Up @@ -66,6 +81,8 @@ if (isProduction) {
});
}

router.use(compress());

router.get("/locales/:lng.json", async (ctx) => {
const { lng } = ctx.params;

Expand Down
4 changes: 2 additions & 2 deletions server/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/utils/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = {
display: "standalone",
icons: [
{
src: path.resolve("public/icon-512.png"),
src: path.resolve("public/images/icon-512.png"),
// For Chrome, you must provide at least a 192x192 pixel icon, and a 512x512 pixel icon.
// If only those two icon sizes are provided, Chrome will automatically scale the icons
// to fit the device. If you'd prefer to scale your own icons, and adjust them for
Expand Down
34 changes: 4 additions & 30 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2952,7 +2952,7 @@
dependencies:
"@types/koa" "*"

"@types/koa-send@*":
"@types/koa-send@^4.1.3":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@types/koa-send/-/koa-send-4.1.3.tgz#17193c6472ae9e5d1b99ae8086949cc4fd69179d"
integrity sha512-daaTqPZlgjIJycSTNjKpHYuKhXYP30atFc1pBcy6HHqB9+vcymDgYTguPdx9tO4HMOqNyz6bz/zqpxt5eLR+VA==
Expand All @@ -2966,14 +2966,6 @@
dependencies:
"@types/koa" "*"

"@types/koa-static@^4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@types/koa-static/-/koa-static-4.0.2.tgz#a199d2d64d2930755eb3ea370aeaf2cb6f501d67"
integrity sha512-ns/zHg+K6XVPMuohjpOlpkR1WLa4VJ9czgUP9bxkCDn0JZBtUWbD/wKDZzPGDclkQK1bpAEScufCHOy8cbfL0w==
dependencies:
"@types/koa" "*"
"@types/koa-send" "*"

"@types/koa-useragent@^2.1.2":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@types/koa-useragent/-/koa-useragent-2.1.2.tgz#7c75fe55c742e559c4643d65b34c6ce5945f853f"
Expand Down Expand Up @@ -6193,7 +6185,7 @@ debug@4, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, d
dependencies:
ms "2.1.2"

debug@^2.2.0, debug@^2.3.3, debug@^2.6.1, debug@^2.6.3, debug@^2.6.8, debug@^2.6.9:
debug@^2.2.0, debug@^2.3.3, debug@^2.6.1, debug@^2.6.8, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
Expand Down Expand Up @@ -8512,7 +8504,7 @@ http-errors@2.0.0:
statuses "2.0.1"
toidentifier "1.0.1"

http-errors@^1.3.1, http-errors@^1.6.1, http-errors@^1.6.3, http-errors@^1.7.3, http-errors@^1.8.0:
http-errors@^1.3.1, http-errors@^1.6.3, http-errors@^1.7.3, http-errors@^1.8.0:
version "1.8.1"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.8.1.tgz#7c3f28577cbc8a207388455dbd62295ed07bd68c"
integrity sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==
Expand Down Expand Up @@ -10150,29 +10142,11 @@ koa-send@5.0.1, koa-send@^5.0.0:
http-errors "^1.7.3"
resolve-path "^1.4.0"

koa-send@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/koa-send/-/koa-send-4.1.3.tgz#0822207bbf5253a414c8f1765ebc29fa41353cb6"
integrity sha512-3UetMBdaXSiw24qM2Mx5mKmxLKw5ZTPRjACjfhK6Haca55RKm9hr/uHDrkrxhSl5/S1CKI/RivZVIopiatZuTA==
dependencies:
debug "^2.6.3"
http-errors "^1.6.1"
mz "^2.6.0"
resolve-path "^1.4.0"

koa-sslify@2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/koa-sslify/-/koa-sslify-2.1.2.tgz#8947fd53949d69d539607814097863c1ecf38f30"
integrity sha1-iUf9U5SdadU5YHgUCXhjwezzjzA=

koa-static@^4.0.1:
version "4.0.3"
resolved "https://registry.yarnpkg.com/koa-static/-/koa-static-4.0.3.tgz#5f93ad00fb1905db9ce46667c0e8bb7d22abfcd8"
integrity sha512-JGmxTuPWy4bH7bt6gD/OMWkhprawvRmzJSr8TWKmTL4N7+IMv3s0SedeQi5S4ilxM9Bo6ptkCyXj/7wf+VS5tg==
dependencies:
debug "^3.1.0"
koa-send "^4.1.3"

koa-static@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/koa-static/-/koa-static-5.0.0.tgz#5e92fc96b537ad5219f425319c95b64772776943"
Expand Down Expand Up @@ -11122,7 +11096,7 @@ msgpackr@^1.5.2:
optionalDependencies:
msgpackr-extract "^2.0.2"

mz@^2.4.0, mz@^2.6.0:
mz@^2.4.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32"
integrity sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==
Expand Down

0 comments on commit 9936f42

Please sign in to comment.