mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-29 07:41:13 +00:00
fix cache auth method
This commit is contained in:
parent
b6415f828e
commit
da45ffb46b
2 changed files with 27 additions and 11 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 { isRequestAuthenticated } from "./utils/is-request-authenticated";
|
||||||
|
|
||||||
export function setDefaultCsp({
|
export function setDefaultCsp({
|
||||||
res,
|
res,
|
||||||
|
@ -22,23 +22,30 @@ export function setDefaultCsp({
|
||||||
// interval is rather arbitrary and could be set higher (less server load) or lower (fresher data).
|
// 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
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
||||||
export function setCacheControl(
|
export function setCacheControl({
|
||||||
req: Request,
|
res,
|
||||||
res: Response,
|
req,
|
||||||
next: NextFunction
|
next,
|
||||||
) {
|
}: {
|
||||||
const user = UserService.Instance;
|
res: Response;
|
||||||
|
req: Request;
|
||||||
|
next: NextFunction;
|
||||||
|
}) {
|
||||||
let caching: string;
|
let caching: string;
|
||||||
|
|
||||||
|
// Avoid any sort of caching in development
|
||||||
|
if (process.env.NODE_ENV !== "production") {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
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 (isRequestAuthenticated(req)) {
|
||||||
caching = "private";
|
caching = "private";
|
||||||
} else {
|
} else {
|
||||||
caching = "public, max-age=5";
|
caching = "public, max-age=5";
|
||||||
|
|
9
src/server/utils/is-request-authenticated.ts
Normal file
9
src/server/utils/is-request-authenticated.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import type { Request } from "express";
|
||||||
|
|
||||||
|
export function isRequestAuthenticated(req: Request): boolean {
|
||||||
|
if (!req.headers.cookie) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return req.headers.cookie?.split("; ").some(c => c.startsWith("jwt"));
|
||||||
|
}
|
Loading…
Reference in a new issue