diff --git a/src/server/middleware.ts b/src/server/middleware.ts index 235f0729..0420e47e 100644 --- a/src/server/middleware.ts +++ b/src/server/middleware.ts @@ -1,5 +1,5 @@ import type { NextFunction, Request, Response } from "express"; -import { UserService } from "../shared/services"; +import { hasJwtCookie } from "./utils/has-jwt-cookie"; export function setDefaultCsp({ res, @@ -27,18 +27,20 @@ export function setCacheControl( res: Response, next: NextFunction ) { - const user = UserService.Instance; + if (process.env.NODE_ENV !== "production") { + return next(); + } + let caching: string; if ( - process.env.NODE_ENV === "production" && - (req.path.match(/\.(js|css|txt|manifest\.webmanifest)\/?$/) || - req.path.includes("/css/themelist")) + 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 { - if (user.auth()) { + if (hasJwtCookie(req)) { caching = "private"; } else { caching = "public, max-age=5"; diff --git a/src/server/utils/has-jwt-cookie.ts b/src/server/utils/has-jwt-cookie.ts new file mode 100644 index 00000000..ea558ffa --- /dev/null +++ b/src/server/utils/has-jwt-cookie.ts @@ -0,0 +1,6 @@ +import * as cookie from "cookie"; +import type { Request } from "express"; + +export function hasJwtCookie(req: Request): boolean { + return Boolean(cookie.parse(req.headers.cookie ?? "").jwt?.length); +} diff --git a/webpack.config.js b/webpack.config.js index 0c9806dd..a67ed2ec 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -111,9 +111,6 @@ const createClientConfig = (env, mode) => { new ServiceWorkerPlugin({ enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct workbox: { - modifyURLPrefix: { - "/": `/static/${env.COMMIT_HASH}/`, - }, cacheId: "lemmy", include: [/(assets|styles|js)\/.+\..+$/g], inlineWorkboxRuntime: true,