Merge pull request #1718 from LemmyNet/fix-cache-auth

Fix broken `user.auth()` method on `middleware.ts`
This commit is contained in:
SleeplessOne1917 2023-06-30 10:17:14 -04:00 committed by GitHub
commit c8e3256c88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 9 deletions

View file

@ -1,5 +1,5 @@
import type { NextFunction, Request, Response } from "express"; import type { NextFunction, Request, Response } from "express";
import { UserService } from "../shared/services"; import { hasJwtCookie } from "./utils/has-jwt-cookie";
export function setDefaultCsp({ export function setDefaultCsp({
res, res,
@ -27,18 +27,20 @@ export function setCacheControl(
res: Response, res: Response,
next: NextFunction next: NextFunction
) { ) {
const user = UserService.Instance; if (process.env.NODE_ENV !== "production") {
return next();
}
let caching: string; let caching: string;
if ( if (
process.env.NODE_ENV === "production" && req.path.match(/\.(js|css|txt|manifest\.webmanifest)\/?$/) ||
(req.path.match(/\.(js|css|txt|manifest\.webmanifest)\/?$/) || req.path.includes("/css/themelist")
req.path.includes("/css/themelist"))
) { ) {
// Static content gets cached publicly for a day // Static content gets cached publicly for a day
caching = "public, max-age=86400"; caching = "public, max-age=86400";
} else { } else {
if (user.auth()) { if (hasJwtCookie(req)) {
caching = "private"; caching = "private";
} else { } else {
caching = "public, max-age=5"; caching = "public, max-age=5";

View file

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

View file

@ -111,9 +111,6 @@ const createClientConfig = (env, mode) => {
new ServiceWorkerPlugin({ new ServiceWorkerPlugin({
enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct
workbox: { workbox: {
modifyURLPrefix: {
"/": `/static/${env.COMMIT_HASH}/`,
},
cacheId: "lemmy", cacheId: "lemmy",
include: [/(assets|styles|js)\/.+\..+$/g], include: [/(assets|styles|js)\/.+\..+$/g],
inlineWorkboxRuntime: true, inlineWorkboxRuntime: true,