mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-01 01:59:56 +00:00
Merge pull request #1718 from LemmyNet/fix-cache-auth
Fix broken `user.auth()` method on `middleware.ts`
This commit is contained in:
commit
c8e3256c88
3 changed files with 14 additions and 9 deletions
|
@ -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";
|
||||
|
|
6
src/server/utils/has-jwt-cookie.ts
Normal file
6
src/server/utils/has-jwt-cookie.ts
Normal 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);
|
||||
}
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue