-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Set cache-control headers to reduce server load (fixes #412) * add missing file * remove old middleware folder * use let --------- Co-authored-by: SleeplessOne1917 <abias1122@gmail.com>
- Loading branch information
1 parent
0e9faf7
commit 32063a5
Showing
3 changed files
with
44 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { NextFunction, Response } from "express"; | ||
import { UserService } from "../shared/services"; | ||
|
||
export function setDefaultCsp({ | ||
res, | ||
next, | ||
}: { | ||
res: Response; | ||
next: NextFunction; | ||
}) { | ||
res.setHeader( | ||
"Content-Security-Policy", | ||
`default-src 'self'; manifest-src *; connect-src *; img-src * data:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; form-action 'self'; base-uri 'self'; frame-src *; media-src *` | ||
); | ||
|
||
next(); | ||
} | ||
|
||
// Set cache-control headers. If user is logged in, set `private` to prevent storing data in | ||
// shared caches (eg nginx) and leaking of private data. If user is not logged in, allow caching | ||
// all responses for 60 seconds to reduce load on backend and database. The specific cache | ||
// 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 | ||
export function setCacheControl({ | ||
res, | ||
next, | ||
}: { | ||
res: Response; | ||
next: NextFunction; | ||
}) { | ||
const user = UserService.Instance; | ||
let caching; | ||
if (user.auth()) { | ||
caching = "private"; | ||
} else { | ||
caching = "public, max-age=60"; | ||
} | ||
res.setHeader("Cache-Control", caching); | ||
|
||
next(); | ||
} |
This file was deleted.
Oops, something went wrong.