Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken user.auth() method on middleware.ts #1718

Merged
merged 7 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/server/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextFunction, Request, Response } from "express";
import { UserService } from "../shared/services";
import { hasJwtCookie } from "./utils/has-jwt-cookie";

export function setDefaultCsp({
res,
Expand Down Expand Up @@ -27,18 +27,20 @@ export function setCacheControl(
res: Response,
next: NextFunction
) {
const user = UserService.Instance;
if (process.env.NODE_ENV !== "production") {
return next();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not cache anything on dev.


let caching: string;

if (
process.env.NODE_ENV === "production" &&
(req.path.match(/\.(js|css|txt|manifest\.webmanifest)\/?$/) ||
req.path.includes("/css/themelist"))
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 {
if (user.auth()) {
if (hasJwtCookie(req)) {
caching = "private";
} else {
caching = "public, max-age=5";
Expand Down
6 changes: 6 additions & 0 deletions src/server/utils/has-jwt-cookie.ts
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 0 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ const createClientConfig = (env, mode) => {
new ServiceWorkerPlugin({
enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct
workbox: {
modifyURLPrefix: {
"/": `/static/${env.COMMIT_HASH}/`,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes a bad path with our service worker on production.

cacheId: "lemmy",
include: [/(assets|styles|js)\/.+\..+$/g],
inlineWorkboxRuntime: true,
Expand Down