mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-01 10:09: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 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";
|
||||||
|
|
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({
|
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,
|
||||||
|
|
Loading…
Reference in a new issue