Compare commits

...
Sign in to create a new pull request.

9 commits

Author SHA1 Message Date
SleeplessOne1917
b6415f828e
Merge pull request #1711 from LemmyNet/cache-dev
Fix some issues
2023-06-29 19:26:40 -04:00
SleeplessOne1917
cc184a86c8 Fix authorized route false flag 2023-06-29 18:12:22 -04:00
SleeplessOne1917
2d88e42cab Fix dev caching issue 2023-06-29 16:33:08 -04:00
Dessalines
9e7fec772d v0.18.1-rc.5 2023-06-29 16:20:38 -04:00
SleeplessOne1917
df39e0fe5d
Merge pull request #1708 from LemmyNet/cache-control
Cache static data for a day
2023-06-29 14:19:07 -04:00
SleeplessOne1917
fa41117320
Merge branch 'main' into cache-control 2023-06-29 13:35:44 -04:00
Alec Armbruster
d8ee0ec78a
change max-age to 5 for non-authed responses 2023-06-29 13:33:30 -04:00
Alec Armbruster
fead020bdc
Fix PostListing mobile margin layout issue (#1706) 2023-06-29 17:28:55 +00:00
SleeplessOne1917
339cefa2b0 Cache static data for a day 2023-06-29 13:14:48 -04:00
7 changed files with 37 additions and 20 deletions

View file

@ -1,6 +1,6 @@
{
"name": "lemmy-ui",
"version": "0.18.1-rc.4",
"version": "0.18.1-rc.5",
"description": "An isomorphic UI for lemmy",
"repository": "https://github.com/LemmyNet/lemmy-ui",
"license": "AGPL-3.0",

View file

@ -20,7 +20,13 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"]
server.use(express.json());
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);
if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) {

View file

@ -1,4 +1,4 @@
import type { NextFunction, Response } from "express";
import type { NextFunction, Request, Response } from "express";
import { UserService } from "../shared/services";
export function setDefaultCsp({
@ -18,24 +18,33 @@ export function setDefaultCsp({
// 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
// all responses for 5 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;
}) {
export function setCacheControl(
req: Request,
res: Response,
next: NextFunction
) {
const user = UserService.Instance;
let caching: string;
if (user.auth()) {
caching = "private";
if (
process.env.NODE_ENV === "production" &&
(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 {
caching = "public, max-age=60";
if (user.auth()) {
caching = "private";
} else {
caching = "public, max-age=5";
}
}
res.setHeader("Cache-Control", caching);
next();

View file

@ -24,7 +24,7 @@ export async function createSsrHtml(
if (!appleTouchIcon) {
appleTouchIcon = site?.site_view.site.icon
? `data:image/png;base64,${sharp(
? `data:image/png;base64,${await sharp(
await fetchIconPng(site.site_view.site.icon)
)
.resize(180, 180)

View file

@ -403,8 +403,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
createdLine() {
const post_view = this.postView;
return (
<div className="small">
<div className="small mb-1 mb-md-0">
<span className="me-1">
<PersonListing person={post_view.creator} />
</span>

View file

@ -1,5 +1,5 @@
export default function isAuthPath(pathname: string) {
return /create_.*|inbox|settings|admin|reports|registration_applications/g.test(
return /^\/create_.*|inbox|settings|admin|reports|registration_applications/g.test(
pathname
);
}

View file

@ -14,7 +14,7 @@ const banner = `
@license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0
`;
function getBase(env) {
function getBase(env, mode) {
return {
output: {
filename: "js/server.js",
@ -54,6 +54,7 @@ function getBase(env) {
plugins: [
new webpack.DefinePlugin({
"process.env.COMMIT_HASH": `"${env.COMMIT_HASH}"`,
"process.env.NODE_ENV": `"${mode}"`,
}),
new MiniCssExtractPlugin({
filename: "styles/styles.css",
@ -69,7 +70,7 @@ function getBase(env) {
}
const createServerConfig = (env, mode) => {
const base = getBase(env);
const base = getBase(env, mode);
const config = merge({}, base, {
mode,
entry: "./src/server/index.tsx",
@ -97,7 +98,7 @@ const createServerConfig = (env, mode) => {
};
const createClientConfig = (env, mode) => {
const base = getBase(env);
const base = getBase(env, mode);
const config = merge({}, base, {
mode,
entry: "./src/client/index.tsx",