From: SleeplessOne1917 Date: Thu, 29 Jun 2023 17:14:48 +0000 (-0400) Subject: Cache static data for a day X-Git-Url: http://these/git/%22https:/www.exeterceilidhs.net/%22%7B%7D/%7Bthis.captchaPngSrc%28%29%7D?a=commitdiff_plain;h=339cefa2b0ee206f5ca36cf2896c3473a130dc79;p=lemmy-ui.git Cache static data for a day --- diff --git a/src/server/index.tsx b/src/server/index.tsx index e1b36e2..458d7f0 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -20,7 +20,13 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"] server.use(express.json()); server.use(express.urlencoded({ extended: false })); -server.use(getStaticDir(), express.static(path.resolve("./dist"))); +server.use( + getStaticDir(), + express.static(path.resolve("./dist"), { + maxAge: 24 * 60 * 60 * 1000, // 1 day + immutable: true, + }) +); server.use(setCacheControl); if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) { diff --git a/src/server/middleware.ts b/src/server/middleware.ts index 9815e71..753fce9 100644 --- a/src/server/middleware.ts +++ b/src/server/middleware.ts @@ -1,4 +1,4 @@ -import type { NextFunction, Response } from "express"; +import type { NextFunction, Request, Response } from "express"; import { UserService } from "../shared/services"; export function setDefaultCsp({ @@ -22,19 +22,25 @@ export function setDefaultCsp({ // interval is rather arbitrary and could be set higher (less server load) or lower (fresher data). // // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control -export function setCacheControl({ - res, - next, -}: { - res: Response; - next: NextFunction; -}) { +export function setCacheControl( + req: Request, + res: Response, + next: NextFunction +) { const user = UserService.Instance; let caching: string; - if (user.auth()) { - caching = "private"; + if ( + req.path.match(/\.(js|css|txt|manifest\.webmanifest)\/?$/) || + req.path.includes("/css/themelist") + ) { + // Static content gets cached publicly for a day + caching = "public, max-age=86400"; } else { - caching = "public, max-age=60"; + if (user.auth()) { + caching = "private"; + } else { + caching = "public, max-age=60"; + } } res.setHeader("Cache-Control", caching); diff --git a/src/server/utils/create-ssr-html.tsx b/src/server/utils/create-ssr-html.tsx index f6d46b0..ba85228 100644 --- a/src/server/utils/create-ssr-html.tsx +++ b/src/server/utils/create-ssr-html.tsx @@ -24,7 +24,7 @@ export async function createSsrHtml( if (!appleTouchIcon) { appleTouchIcon = site?.site_view.site.icon - ? `data:image/png;base64,${sharp( + ? `data:image/png;base64,${await sharp( await fetchIconPng(site.site_view.site.icon) ) .resize(180, 180)