mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-01 01:59:56 +00:00
Cache static data for a day
This commit is contained in:
parent
08370d4c4e
commit
339cefa2b0
3 changed files with 25 additions and 13 deletions
|
@ -20,7 +20,13 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"]
|
||||||
|
|
||||||
server.use(express.json());
|
server.use(express.json());
|
||||||
server.use(express.urlencoded({ extended: false }));
|
server.use(express.urlencoded({ extended: false }));
|
||||||
server.use(getStaticDir(), express.static(path.resolve("./dist")));
|
server.use(
|
||||||
|
getStaticDir(),
|
||||||
|
express.static(path.resolve("./dist"), {
|
||||||
|
maxAge: 24 * 60 * 60 * 1000, // 1 day
|
||||||
|
immutable: true,
|
||||||
|
})
|
||||||
|
);
|
||||||
server.use(setCacheControl);
|
server.use(setCacheControl);
|
||||||
|
|
||||||
if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) {
|
if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import type { NextFunction, Response } from "express";
|
import type { NextFunction, Request, Response } from "express";
|
||||||
import { UserService } from "../shared/services";
|
import { UserService } from "../shared/services";
|
||||||
|
|
||||||
export function setDefaultCsp({
|
export function setDefaultCsp({
|
||||||
|
@ -22,19 +22,25 @@ 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(
|
||||||
res,
|
req: Request,
|
||||||
next,
|
res: Response,
|
||||||
}: {
|
next: NextFunction
|
||||||
res: Response;
|
) {
|
||||||
next: NextFunction;
|
|
||||||
}) {
|
|
||||||
const user = UserService.Instance;
|
const user = UserService.Instance;
|
||||||
let caching: string;
|
let caching: string;
|
||||||
if (user.auth()) {
|
if (
|
||||||
caching = "private";
|
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 {
|
} else {
|
||||||
caching = "public, max-age=60";
|
if (user.auth()) {
|
||||||
|
caching = "private";
|
||||||
|
} else {
|
||||||
|
caching = "public, max-age=60";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
res.setHeader("Cache-Control", caching);
|
res.setHeader("Cache-Control", caching);
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ export async function createSsrHtml(
|
||||||
|
|
||||||
if (!appleTouchIcon) {
|
if (!appleTouchIcon) {
|
||||||
appleTouchIcon = site?.site_view.site.icon
|
appleTouchIcon = site?.site_view.site.icon
|
||||||
? `data:image/png;base64,${sharp(
|
? `data:image/png;base64,${await sharp(
|
||||||
await fetchIconPng(site.site_view.site.icon)
|
await fetchIconPng(site.site_view.site.icon)
|
||||||
)
|
)
|
||||||
.resize(180, 180)
|
.resize(180, 180)
|
||||||
|
|
Loading…
Reference in a new issue