]> Untitled Git - lemmy-ui.git/commitdiff
Cache static data for a day
authorSleeplessOne1917 <abias1122@gmail.com>
Thu, 29 Jun 2023 17:14:48 +0000 (13:14 -0400)
committerSleeplessOne1917 <abias1122@gmail.com>
Thu, 29 Jun 2023 17:14:48 +0000 (13:14 -0400)
src/server/index.tsx
src/server/middleware.ts
src/server/utils/create-ssr-html.tsx

index e1b36e2a6bc1f81fa3134b02f84b7b609aac3298..458d7f03e388aee6ccdc07edad21f99ad9a31d61 100644 (file)
@@ -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"]) {
index 9815e71e1f520171c4f7fe23387675b39e40b6c8..753fce9442fe6e0eded254d5f942f0ab0da4b026 100644 (file)
@@ -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);
 
index f6d46b02058b055adae1d7e1367c52c0c88f87ad..ba85228f2f9c1394241fe40acf63e3fd95880331 100644 (file)
@@ -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)