From aaf9bc970c5e34d7a5d6780f172fc2983a78f67e Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Wed, 28 Jun 2023 22:22:06 -0400 Subject: [PATCH 01/17] Use git hash to break cache --- .prettierignore | 1 + Dockerfile | 2 +- dev.dockerfile | 4 +++- src/server/index.tsx | 3 ++- src/server/utils/create-ssr-html.tsx | 5 +++-- src/shared/components/common/icon.tsx | 3 ++- src/shared/components/person/person-listing.tsx | 5 ++++- src/shared/config.ts | 6 ++++-- src/shared/version.ts | 1 + webpack.config.js | 8 +++++++- 10 files changed, 28 insertions(+), 10 deletions(-) diff --git a/.prettierignore b/.prettierignore index 004c815..e9ba8f6 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,3 +2,4 @@ src/shared/translations lemmy-translations src/assets/css/themes/*.css stats.json +dist \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2b36581..66a0add 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ COPY src src COPY .git .git # Set UI version -RUN echo "export const VERSION = '$(git describe --tag)';" > "src/shared/version.ts" +RUN echo "export const VERSION = '$(git describe --tag)'; export const COMMIT_HASH = '$(git rev-parse HEAD)';" > "src/shared/version.ts" RUN yarn --production --prefer-offline RUN yarn build:prod diff --git a/dev.dockerfile b/dev.dockerfile index 3bfc10d..e446ba8 100644 --- a/dev.dockerfile +++ b/dev.dockerfile @@ -18,11 +18,13 @@ COPY generate_translations.js \ .babelrc \ ./ + COPY lemmy-translations lemmy-translations COPY src src +COPY .git .git # Set UI version -RUN echo "export const VERSION = 'dev';" > "src/shared/version.ts" +RUN echo "export const VERSION = 'dev'; export const COMMIT_HASH = '$(git rev-parse HEAD)';" > "src/shared/version.ts" RUN yarn --prefer-offline RUN yarn build:dev diff --git a/src/server/index.tsx b/src/server/index.tsx index aed8bca..5dd8303 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -2,6 +2,7 @@ import { setupDateFns } from "@utils/app"; import express from "express"; import path from "path"; import process from "process"; +import { COMMIT_HASH } from "../shared/version"; import CatchAllHandler from "./handlers/catch-all-handler"; import ManifestHandler from "./handlers/manifest-handler"; import RobotsHandler from "./handlers/robots-handler"; @@ -18,7 +19,7 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"] server.use(express.json()); server.use(express.urlencoded({ extended: false })); -server.use("/static", express.static(path.resolve("./dist"))); +server.use(`/static-${COMMIT_HASH}`, express.static(path.resolve("./dist"))); server.use(setCacheControl); if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) { diff --git a/src/server/utils/create-ssr-html.tsx b/src/server/utils/create-ssr-html.tsx index 1377598..d77df26 100644 --- a/src/server/utils/create-ssr-html.tsx +++ b/src/server/utils/create-ssr-html.tsx @@ -4,6 +4,7 @@ import serialize from "serialize-javascript"; import sharp from "sharp"; import { favIconPngUrl, favIconUrl } from "../../shared/config"; import { ILemmyConfig, IsoDataOptionalSite } from "../../shared/interfaces"; +import { COMMIT_HASH } from "../../shared/version"; import { buildThemeList } from "./build-themes-list"; import { fetchIconPng } from "./fetch-icon-png"; @@ -87,7 +88,7 @@ export async function createSsrHtml( - + ${helmet.link.toString() || fallbackTheme} @@ -102,7 +103,7 @@ export async function createSsrHtml(
${root}
- + `; diff --git a/src/shared/components/common/icon.tsx b/src/shared/components/common/icon.tsx index 5b6ddf8..7060e9c 100644 --- a/src/shared/components/common/icon.tsx +++ b/src/shared/components/common/icon.tsx @@ -1,5 +1,6 @@ import classNames from "classnames"; import { Component } from "inferno"; +import { COMMIT_HASH } from "../../../shared/version"; import { I18NextService } from "../../services"; interface IconProps { @@ -23,7 +24,7 @@ export class Icon extends Component { })} >
{this.props.icon} diff --git a/src/shared/components/person/person-listing.tsx b/src/shared/components/person/person-listing.tsx index 6631a8e..a8fc0da 100644 --- a/src/shared/components/person/person-listing.tsx +++ b/src/shared/components/person/person-listing.tsx @@ -4,6 +4,7 @@ import classNames from "classnames"; import { Component } from "inferno"; import { Link } from "inferno-router"; import { Person } from "lemmy-js-client"; +import { COMMIT_HASH } from "../../../shared/version"; import { relTags } from "../../config"; import { PictrsImage } from "../common/pictrs-image"; import { CakeDay } from "./cake-day"; @@ -88,7 +89,9 @@ export class PersonListing extends Component { !this.props.person.banned && showAvatars() && ( )} diff --git a/src/shared/config.ts b/src/shared/config.ts index 97b28d2..6b0a190 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -1,5 +1,7 @@ -export const favIconUrl = "/static/assets/icons/favicon.svg"; -export const favIconPngUrl = "/static/assets/icons/apple-touch-icon.png"; +import { COMMIT_HASH } from "./version"; + +export const favIconUrl = `/static-${COMMIT_HASH}/assets/icons/favicon.svg`; +export const favIconPngUrl = `/static-${COMMIT_HASH}/assets/icons/apple-touch-icon.png`; export const repoUrl = "https://github.com/LemmyNet"; export const joinLemmyUrl = "https://join-lemmy.org"; diff --git a/src/shared/version.ts b/src/shared/version.ts index c1dba35..a799956 100644 --- a/src/shared/version.ts +++ b/src/shared/version.ts @@ -1 +1,2 @@ export const VERSION = "unknown version"; +export const COMMIT_HASH = "unknown hash"; diff --git a/webpack.config.js b/webpack.config.js index 4d95a80..268d9ec 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -14,6 +14,11 @@ const banner = ` @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0 `; +const commitHash = require("child_process") + .execSync("git rev-parse HEAD") + .toString() + .trim(); + const base = { output: { filename: "js/server.js", @@ -96,6 +101,7 @@ const createClientConfig = (_env, mode) => { entry: "./src/client/index.tsx", output: { filename: "js/client.js", + publicPath: `/static-${commitHash}`, }, plugins: [ ...base.plugins, @@ -103,7 +109,7 @@ const createClientConfig = (_env, mode) => { enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct workbox: { modifyURLPrefix: { - "/": "/static/", + "/": `/static-${commitHash}/`, }, cacheId: "lemmy", include: [/(assets|styles)\/.+\..+|client\.js$/g], From c7ea1b7c56df3b12ec18761b8932fe0f5ca9c21c Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Thu, 29 Jun 2023 06:18:31 -0400 Subject: [PATCH 02/17] Address PR feedback --- src/server/index.tsx | 2 +- src/server/utils/create-ssr-html.tsx | 4 ++-- src/shared/components/common/icon.tsx | 2 +- src/shared/components/person/person-listing.tsx | 2 +- src/shared/config.ts | 4 ++-- webpack.config.js | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/server/index.tsx b/src/server/index.tsx index 5dd8303..e9c907f 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -19,7 +19,7 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"] server.use(express.json()); server.use(express.urlencoded({ extended: false })); -server.use(`/static-${COMMIT_HASH}`, express.static(path.resolve("./dist"))); +server.use(`/static/${COMMIT_HASH}`, express.static(path.resolve("./dist"))); server.use(setCacheControl); if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) { diff --git a/src/server/utils/create-ssr-html.tsx b/src/server/utils/create-ssr-html.tsx index d77df26..0a4ec22 100644 --- a/src/server/utils/create-ssr-html.tsx +++ b/src/server/utils/create-ssr-html.tsx @@ -88,7 +88,7 @@ export async function createSsrHtml( - + ${helmet.link.toString() || fallbackTheme} @@ -103,7 +103,7 @@ export async function createSsrHtml(
${root}
- + `; diff --git a/src/shared/components/common/icon.tsx b/src/shared/components/common/icon.tsx index 7060e9c..10f59c7 100644 --- a/src/shared/components/common/icon.tsx +++ b/src/shared/components/common/icon.tsx @@ -24,7 +24,7 @@ export class Icon extends Component { })} >
{this.props.icon} diff --git a/src/shared/components/person/person-listing.tsx b/src/shared/components/person/person-listing.tsx index a8fc0da..169a93f 100644 --- a/src/shared/components/person/person-listing.tsx +++ b/src/shared/components/person/person-listing.tsx @@ -90,7 +90,7 @@ export class PersonListing extends Component { showAvatars() && ( diff --git a/src/shared/config.ts b/src/shared/config.ts index 6b0a190..cb0f8e1 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -1,7 +1,7 @@ import { COMMIT_HASH } from "./version"; -export const favIconUrl = `/static-${COMMIT_HASH}/assets/icons/favicon.svg`; -export const favIconPngUrl = `/static-${COMMIT_HASH}/assets/icons/apple-touch-icon.png`; +export const favIconUrl = `/static/${COMMIT_HASH}/assets/icons/favicon.svg`; +export const favIconPngUrl = `/static/${COMMIT_HASH}/assets/icons/apple-touch-icon.png`; export const repoUrl = "https://github.com/LemmyNet"; export const joinLemmyUrl = "https://join-lemmy.org"; diff --git a/webpack.config.js b/webpack.config.js index 268d9ec..0b03f6d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -101,7 +101,7 @@ const createClientConfig = (_env, mode) => { entry: "./src/client/index.tsx", output: { filename: "js/client.js", - publicPath: `/static-${commitHash}`, + publicPath: `/static/${commitHash}`, }, plugins: [ ...base.plugins, @@ -109,7 +109,7 @@ const createClientConfig = (_env, mode) => { enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct workbox: { modifyURLPrefix: { - "/": `/static-${commitHash}/`, + "/": `/static/${commitHash}/`, }, cacheId: "lemmy", include: [/(assets|styles)\/.+\..+|client\.js$/g], From 753953ad3eff19c5535689422adf3f2048d0499a Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Thu, 29 Jun 2023 09:25:08 -0400 Subject: [PATCH 03/17] Make hash docker agnostic --- Dockerfile | 2 +- dev.dockerfile | 3 +- package.json | 4 +- src/server/index.tsx | 7 +- src/server/utils/create-ssr-html.tsx | 9 +- src/shared/components/common/icon.tsx | 3 +- .../components/person/person-listing.tsx | 4 +- src/shared/config.ts | 6 +- src/shared/version.ts | 1 - webpack.config.js | 108 +++++++++--------- 10 files changed, 75 insertions(+), 72 deletions(-) diff --git a/Dockerfile b/Dockerfile index 66a0add..2b36581 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ COPY src src COPY .git .git # Set UI version -RUN echo "export const VERSION = '$(git describe --tag)'; export const COMMIT_HASH = '$(git rev-parse HEAD)';" > "src/shared/version.ts" +RUN echo "export const VERSION = '$(git describe --tag)';" > "src/shared/version.ts" RUN yarn --production --prefer-offline RUN yarn build:prod diff --git a/dev.dockerfile b/dev.dockerfile index e446ba8..881d9bc 100644 --- a/dev.dockerfile +++ b/dev.dockerfile @@ -18,13 +18,12 @@ COPY generate_translations.js \ .babelrc \ ./ - COPY lemmy-translations lemmy-translations COPY src src COPY .git .git # Set UI version -RUN echo "export const VERSION = 'dev'; export const COMMIT_HASH = '$(git rev-parse HEAD)';" > "src/shared/version.ts" +RUN echo "export const VERSION = 'dev';" > "src/shared/version.ts" RUN yarn --prefer-offline RUN yarn build:dev diff --git a/package.json b/package.json index e5c1fa7..fcac4e1 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,9 @@ "scripts": { "analyze": "webpack --mode=none", "prebuild:dev": "yarn clean && node generate_translations.js", - "build:dev": "webpack --mode=development", + "build:dev": "webpack --env COMMIT_HASH=$(git rev-parse --short HEAD) --mode=development", "prebuild:prod": "yarn clean && node generate_translations.js", - "build:prod": "webpack --mode=production", + "build:prod": "webpack --env COMMIT_HASH=$(git rev-parse --short HEAD) --mode=production", "clean": "yarn run rimraf dist", "dev": "yarn build:dev --watch", "lint": "yarn translations:generate && tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx \"src/**\" && prettier --check \"src/**/*.{ts,tsx,js,css,scss}\"", diff --git a/src/server/index.tsx b/src/server/index.tsx index e9c907f..c6af347 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -2,7 +2,6 @@ import { setupDateFns } from "@utils/app"; import express from "express"; import path from "path"; import process from "process"; -import { COMMIT_HASH } from "../shared/version"; import CatchAllHandler from "./handlers/catch-all-handler"; import ManifestHandler from "./handlers/manifest-handler"; import RobotsHandler from "./handlers/robots-handler"; @@ -19,7 +18,10 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"] server.use(express.json()); server.use(express.urlencoded({ extended: false })); -server.use(`/static/${COMMIT_HASH}`, express.static(path.resolve("./dist"))); +server.use( + `/static/${process.env.COMMIT_HASH}`, + express.static(path.resolve("./dist")) +); server.use(setCacheControl); if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) { @@ -34,6 +36,7 @@ server.get("/css/themelist", ThemesListHandler); server.get("/*", CatchAllHandler); server.listen(Number(port), hostname, () => { + console.log(`HASH: ${process.env.COMMIT_HASH}`); setupDateFns(); console.log(`http://${hostname}:${port}`); }); diff --git a/src/server/utils/create-ssr-html.tsx b/src/server/utils/create-ssr-html.tsx index 0a4ec22..014d325 100644 --- a/src/server/utils/create-ssr-html.tsx +++ b/src/server/utils/create-ssr-html.tsx @@ -4,7 +4,6 @@ import serialize from "serialize-javascript"; import sharp from "sharp"; import { favIconPngUrl, favIconUrl } from "../../shared/config"; import { ILemmyConfig, IsoDataOptionalSite } from "../../shared/interfaces"; -import { COMMIT_HASH } from "../../shared/version"; import { buildThemeList } from "./build-themes-list"; import { fetchIconPng } from "./fetch-icon-png"; @@ -88,7 +87,9 @@ export async function createSsrHtml( - + ${helmet.link.toString() || fallbackTheme} @@ -103,7 +104,9 @@ export async function createSsrHtml(
${root}
- + `; diff --git a/src/shared/components/common/icon.tsx b/src/shared/components/common/icon.tsx index 10f59c7..4102e7b 100644 --- a/src/shared/components/common/icon.tsx +++ b/src/shared/components/common/icon.tsx @@ -1,6 +1,5 @@ import classNames from "classnames"; import { Component } from "inferno"; -import { COMMIT_HASH } from "../../../shared/version"; import { I18NextService } from "../../services"; interface IconProps { @@ -24,7 +23,7 @@ export class Icon extends Component { })} >
{this.props.icon} diff --git a/src/shared/components/person/person-listing.tsx b/src/shared/components/person/person-listing.tsx index 169a93f..1d88e38 100644 --- a/src/shared/components/person/person-listing.tsx +++ b/src/shared/components/person/person-listing.tsx @@ -4,7 +4,6 @@ import classNames from "classnames"; import { Component } from "inferno"; import { Link } from "inferno-router"; import { Person } from "lemmy-js-client"; -import { COMMIT_HASH } from "../../../shared/version"; import { relTags } from "../../config"; import { PictrsImage } from "../common/pictrs-image"; import { CakeDay } from "./cake-day"; @@ -90,7 +89,8 @@ export class PersonListing extends Component { showAvatars() && ( diff --git a/src/shared/config.ts b/src/shared/config.ts index cb0f8e1..3687e60 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -1,7 +1,5 @@ -import { COMMIT_HASH } from "./version"; - -export const favIconUrl = `/static/${COMMIT_HASH}/assets/icons/favicon.svg`; -export const favIconPngUrl = `/static/${COMMIT_HASH}/assets/icons/apple-touch-icon.png`; +export const favIconUrl = `/static/${process.env.COMMIT_HASH}/assets/icons/favicon.svg`; +export const favIconPngUrl = `/static/${process.env.COMMIT_HASH}/assets/icons/apple-touch-icon.png`; export const repoUrl = "https://github.com/LemmyNet"; export const joinLemmyUrl = "https://join-lemmy.org"; diff --git a/src/shared/version.ts b/src/shared/version.ts index a799956..c1dba35 100644 --- a/src/shared/version.ts +++ b/src/shared/version.ts @@ -1,2 +1 @@ export const VERSION = "unknown version"; -export const COMMIT_HASH = "unknown hash"; diff --git a/webpack.config.js b/webpack.config.js index 0b03f6d..8acd71a 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -14,61 +14,62 @@ const banner = ` @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0 `; -const commitHash = require("child_process") - .execSync("git rev-parse HEAD") - .toString() - .trim(); - -const base = { - output: { - filename: "js/server.js", - publicPath: "/", - hashFunction: "xxhash64", - }, - resolve: { - extensions: [".js", ".jsx", ".ts", ".tsx"], - alias: { - "@": path.resolve(__dirname, "src/"), - "@utils": path.resolve(__dirname, "src/shared/utils/"), +function getBase(env) { + return { + output: { + filename: "js/server.js", + publicPath: "/", + hashFunction: "xxhash64", }, - }, - performance: { - hints: false, - }, - module: { - rules: [ - { - test: /\.(scss|css)$/i, - use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"], + resolve: { + extensions: [".js", ".jsx", ".ts", ".tsx"], + alias: { + "@": path.resolve(__dirname, "src/"), + "@utils": path.resolve(__dirname, "src/shared/utils/"), }, - { - test: /\.(js|jsx|tsx|ts)$/, // All ts and tsx files will be process by - exclude: /node_modules/, // ignore node_modules - loader: "babel-loader", - }, - // Due to some weird babel issue: https://github.com/webpack/webpack/issues/11467 - { - test: /\.m?js/, - resolve: { - fullySpecified: false, + }, + performance: { + hints: false, + }, + module: { + rules: [ + { + test: /\.(scss|css)$/i, + use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"], }, - }, + { + test: /\.(js|jsx|tsx|ts)$/, // All ts and tsx files will be process by + exclude: /node_modules/, // ignore node_modules + loader: "babel-loader", + }, + // Due to some weird babel issue: https://github.com/webpack/webpack/issues/11467 + { + test: /\.m?js/, + resolve: { + fullySpecified: false, + }, + }, + ], + }, + plugins: [ + new webpack.DefinePlugin({ + "process.env.COMMIT_HASH": `"${env.COMMIT_HASH}"`, + }), + new MiniCssExtractPlugin({ + filename: "styles/styles.css", + }), + new CopyPlugin({ + patterns: [{ from: "./src/assets", to: "./assets" }], + }), + new webpack.BannerPlugin({ + banner, + }), ], - }, - plugins: [ - new MiniCssExtractPlugin({ - filename: "styles/styles.css", - }), - new CopyPlugin({ - patterns: [{ from: "./src/assets", to: "./assets" }], - }), - new webpack.BannerPlugin({ - banner, - }), - ], -}; + }; +} -const createServerConfig = (_env, mode) => { +const createServerConfig = (env, mode) => { + const base = getBase(env); const config = merge({}, base, { mode, entry: "./src/server/index.tsx", @@ -95,13 +96,14 @@ const createServerConfig = (_env, mode) => { return config; }; -const createClientConfig = (_env, mode) => { +const createClientConfig = (env, mode) => { + const base = getBase(env); const config = merge({}, base, { mode, entry: "./src/client/index.tsx", output: { filename: "js/client.js", - publicPath: `/static/${commitHash}`, + publicPath: `/static/${env.COMMIT_HASH}`, }, plugins: [ ...base.plugins, @@ -109,7 +111,7 @@ const createClientConfig = (_env, mode) => { enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct workbox: { modifyURLPrefix: { - "/": `/static/${commitHash}/`, + "/": `/static/${env.COMMIT_HASH}/`, }, cacheId: "lemmy", include: [/(assets|styles)\/.+\..+|client\.js$/g], From 6d74f86bd069e55064703cfc91fa4ecd015a8365 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Thu, 29 Jun 2023 09:34:38 -0400 Subject: [PATCH 04/17] Add trailing slash --- webpack.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webpack.config.js b/webpack.config.js index cea29b1..9afdb52 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -103,7 +103,7 @@ const createClientConfig = (env, mode) => { entry: "./src/client/index.tsx", output: { filename: "js/client.js", - publicPath: `/static/${env.COMMIT_HASH}`, + publicPath: `/static/${env.COMMIT_HASH}/`, }, plugins: [ ...base.plugins, From c24417b9e9401be90e7c10d097c28faaa4889d2d Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Thu, 29 Jun 2023 09:35:23 -0400 Subject: [PATCH 05/17] Update .prettierignore Co-authored-by: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> --- .prettierignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.prettierignore b/.prettierignore index e9ba8f6..c6145fd 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,4 +2,4 @@ src/shared/translations lemmy-translations src/assets/css/themes/*.css stats.json -dist \ No newline at end of file +dist From 25f65ba6c88427ae901842e55035a68957b06cab Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Thu, 29 Jun 2023 09:39:38 -0400 Subject: [PATCH 06/17] Remove debugging log --- src/server/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/server/index.tsx b/src/server/index.tsx index d712132..cd4e39c 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -38,7 +38,6 @@ server.get("/css/themelist", ThemesListHandler); server.get("/*", CatchAllHandler); server.listen(Number(port), hostname, () => { - console.log(`HASH: ${process.env.COMMIT_HASH}`); setupDateFns(); console.log(`http://${hostname}:${port}`); }); From ce5743f17c774be5b8fcf8d5b84f981f62bdfaa8 Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Thu, 29 Jun 2023 10:08:12 -0400 Subject: [PATCH 07/17] implement getStaticDir util --- src/server/index.tsx | 6 ++---- src/server/utils/create-ssr-html.tsx | 9 +++------ src/shared/components/common/icon.tsx | 5 ++++- src/shared/components/person/person-listing.tsx | 6 ++---- src/shared/config.ts | 6 ++++-- src/shared/utils/env/get-static-dir.ts | 5 +++++ src/shared/utils/env/index.ts | 2 ++ 7 files changed, 22 insertions(+), 17 deletions(-) create mode 100644 src/shared/utils/env/get-static-dir.ts diff --git a/src/server/index.tsx b/src/server/index.tsx index cd4e39c..e1b36e2 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -1,4 +1,5 @@ import { setupDateFns } from "@utils/app"; +import { getStaticDir } from "@utils/env"; import express from "express"; import path from "path"; import process from "process"; @@ -19,10 +20,7 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"] server.use(express.json()); server.use(express.urlencoded({ extended: false })); -server.use( - `/static/${process.env.COMMIT_HASH}`, - express.static(path.resolve("./dist")) -); +server.use(getStaticDir(), express.static(path.resolve("./dist"))); server.use(setCacheControl); if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) { diff --git a/src/server/utils/create-ssr-html.tsx b/src/server/utils/create-ssr-html.tsx index 014d325..f6d46b0 100644 --- a/src/server/utils/create-ssr-html.tsx +++ b/src/server/utils/create-ssr-html.tsx @@ -1,3 +1,4 @@ +import { getStaticDir } from "@utils/env"; import { Helmet } from "inferno-helmet"; import { renderToString } from "inferno-server"; import serialize from "serialize-javascript"; @@ -87,9 +88,7 @@ export async function createSsrHtml( - + ${helmet.link.toString() || fallbackTheme} @@ -104,9 +103,7 @@ export async function createSsrHtml(
${root}
- + `; diff --git a/src/shared/components/common/icon.tsx b/src/shared/components/common/icon.tsx index 4102e7b..92a41a3 100644 --- a/src/shared/components/common/icon.tsx +++ b/src/shared/components/common/icon.tsx @@ -1,3 +1,4 @@ +import { getStaticDir } from "@utils/env"; import classNames from "classnames"; import { Component } from "inferno"; import { I18NextService } from "../../services"; @@ -23,7 +24,9 @@ export class Icon extends Component { })} >
{this.props.icon} diff --git a/src/shared/components/person/person-listing.tsx b/src/shared/components/person/person-listing.tsx index 1d88e38..dfc5d66 100644 --- a/src/shared/components/person/person-listing.tsx +++ b/src/shared/components/person/person-listing.tsx @@ -1,4 +1,5 @@ import { showAvatars } from "@utils/app"; +import { getStaticDir } from "@utils/env"; import { hostname, isCakeDay } from "@utils/helpers"; import classNames from "classnames"; import { Component } from "inferno"; @@ -88,10 +89,7 @@ export class PersonListing extends Component { !this.props.person.banned && showAvatars() && ( )} diff --git a/src/shared/config.ts b/src/shared/config.ts index 3687e60..58ecc08 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -1,5 +1,7 @@ -export const favIconUrl = `/static/${process.env.COMMIT_HASH}/assets/icons/favicon.svg`; -export const favIconPngUrl = `/static/${process.env.COMMIT_HASH}/assets/icons/apple-touch-icon.png`; +import { getStaticDir } from "@utils/env"; + +export const favIconUrl = `${getStaticDir()}/assets/icons/favicon.svg`; +export const favIconPngUrl = `${getStaticDir()}/assets/icons/apple-touch-icon.png`; export const repoUrl = "https://github.com/LemmyNet"; export const joinLemmyUrl = "https://join-lemmy.org"; diff --git a/src/shared/utils/env/get-static-dir.ts b/src/shared/utils/env/get-static-dir.ts new file mode 100644 index 0000000..1d19596 --- /dev/null +++ b/src/shared/utils/env/get-static-dir.ts @@ -0,0 +1,5 @@ +// Returns path to static directory, intended +// for cache-busting based on latest commit hash. +export default function getStaticDir() { + return `/static/${process.env.COMMIT_HASH}`; +} diff --git a/src/shared/utils/env/index.ts b/src/shared/utils/env/index.ts index e14c673..3a9a3fe 100644 --- a/src/shared/utils/env/index.ts +++ b/src/shared/utils/env/index.ts @@ -6,6 +6,7 @@ import getHttpBaseExternal from "./get-http-base-external"; import getHttpBaseInternal from "./get-http-base-internal"; import getInternalHost from "./get-internal-host"; import getSecure from "./get-secure"; +import getStaticDir from "./get-static-dir"; import httpExternalPath from "./http-external-path"; import isHttps from "./is-https"; @@ -18,6 +19,7 @@ export { getHttpBaseInternal, getInternalHost, getSecure, + getStaticDir, httpExternalPath, isHttps, }; From 751495702cc9f84271ff75ab876d5eb16a8a9b1e Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Thu, 29 Jun 2023 10:29:33 -0400 Subject: [PATCH 08/17] Use git hash to break cache (#1684) * Use git hash to break cache * Address PR feedback * Make hash docker agnostic * Add trailing slash * Update .prettierignore Co-authored-by: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> * Remove debugging log * implement getStaticDir util --------- Co-authored-by: Dessalines Co-authored-by: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> --- .prettierignore | 1 + dev.dockerfile | 1 + package.json | 4 +- src/server/index.tsx | 3 +- src/server/utils/create-ssr-html.tsx | 5 +- src/shared/components/common/icon.tsx | 5 +- .../components/person/person-listing.tsx | 3 +- src/shared/config.ts | 6 +- src/shared/utils/env/get-static-dir.ts | 5 + src/shared/utils/env/index.ts | 2 + webpack.config.js | 103 ++++++++++-------- 11 files changed, 81 insertions(+), 57 deletions(-) create mode 100644 src/shared/utils/env/get-static-dir.ts diff --git a/.prettierignore b/.prettierignore index 004c815..c6145fd 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,3 +2,4 @@ src/shared/translations lemmy-translations src/assets/css/themes/*.css stats.json +dist diff --git a/dev.dockerfile b/dev.dockerfile index 3bfc10d..881d9bc 100644 --- a/dev.dockerfile +++ b/dev.dockerfile @@ -20,6 +20,7 @@ COPY generate_translations.js \ COPY lemmy-translations lemmy-translations COPY src src +COPY .git .git # Set UI version RUN echo "export const VERSION = 'dev';" > "src/shared/version.ts" diff --git a/package.json b/package.json index 16d3aec..3bbc0b6 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,9 @@ "scripts": { "analyze": "webpack --mode=none", "prebuild:dev": "yarn clean && node generate_translations.js", - "build:dev": "webpack --mode=development", + "build:dev": "webpack --env COMMIT_HASH=$(git rev-parse --short HEAD) --mode=development", "prebuild:prod": "yarn clean && node generate_translations.js", - "build:prod": "webpack --mode=production", + "build:prod": "webpack --env COMMIT_HASH=$(git rev-parse --short HEAD) --mode=production", "clean": "yarn run rimraf dist", "dev": "yarn build:dev --watch", "lint": "yarn translations:generate && tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx \"src/**\" && prettier --check \"src/**/*.{ts,tsx,js,css,scss}\"", diff --git a/src/server/index.tsx b/src/server/index.tsx index 270f33c..e1b36e2 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -1,4 +1,5 @@ import { setupDateFns } from "@utils/app"; +import { getStaticDir } from "@utils/env"; import express from "express"; import path from "path"; import process from "process"; @@ -19,7 +20,7 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"] server.use(express.json()); server.use(express.urlencoded({ extended: false })); -server.use("/static", express.static(path.resolve("./dist"))); +server.use(getStaticDir(), express.static(path.resolve("./dist"))); server.use(setCacheControl); if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) { diff --git a/src/server/utils/create-ssr-html.tsx b/src/server/utils/create-ssr-html.tsx index 1377598..f6d46b0 100644 --- a/src/server/utils/create-ssr-html.tsx +++ b/src/server/utils/create-ssr-html.tsx @@ -1,3 +1,4 @@ +import { getStaticDir } from "@utils/env"; import { Helmet } from "inferno-helmet"; import { renderToString } from "inferno-server"; import serialize from "serialize-javascript"; @@ -87,7 +88,7 @@ export async function createSsrHtml( - + ${helmet.link.toString() || fallbackTheme} @@ -102,7 +103,7 @@ export async function createSsrHtml(
${root}
- + `; diff --git a/src/shared/components/common/icon.tsx b/src/shared/components/common/icon.tsx index 5b6ddf8..92a41a3 100644 --- a/src/shared/components/common/icon.tsx +++ b/src/shared/components/common/icon.tsx @@ -1,3 +1,4 @@ +import { getStaticDir } from "@utils/env"; import classNames from "classnames"; import { Component } from "inferno"; import { I18NextService } from "../../services"; @@ -23,7 +24,9 @@ export class Icon extends Component { })} >
{this.props.icon} diff --git a/src/shared/components/person/person-listing.tsx b/src/shared/components/person/person-listing.tsx index 6631a8e..dfc5d66 100644 --- a/src/shared/components/person/person-listing.tsx +++ b/src/shared/components/person/person-listing.tsx @@ -1,4 +1,5 @@ import { showAvatars } from "@utils/app"; +import { getStaticDir } from "@utils/env"; import { hostname, isCakeDay } from "@utils/helpers"; import classNames from "classnames"; import { Component } from "inferno"; @@ -88,7 +89,7 @@ export class PersonListing extends Component { !this.props.person.banned && showAvatars() && ( )} diff --git a/src/shared/config.ts b/src/shared/config.ts index 97b28d2..58ecc08 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -1,5 +1,7 @@ -export const favIconUrl = "/static/assets/icons/favicon.svg"; -export const favIconPngUrl = "/static/assets/icons/apple-touch-icon.png"; +import { getStaticDir } from "@utils/env"; + +export const favIconUrl = `${getStaticDir()}/assets/icons/favicon.svg`; +export const favIconPngUrl = `${getStaticDir()}/assets/icons/apple-touch-icon.png`; export const repoUrl = "https://github.com/LemmyNet"; export const joinLemmyUrl = "https://join-lemmy.org"; diff --git a/src/shared/utils/env/get-static-dir.ts b/src/shared/utils/env/get-static-dir.ts new file mode 100644 index 0000000..1d19596 --- /dev/null +++ b/src/shared/utils/env/get-static-dir.ts @@ -0,0 +1,5 @@ +// Returns path to static directory, intended +// for cache-busting based on latest commit hash. +export default function getStaticDir() { + return `/static/${process.env.COMMIT_HASH}`; +} diff --git a/src/shared/utils/env/index.ts b/src/shared/utils/env/index.ts index e14c673..3a9a3fe 100644 --- a/src/shared/utils/env/index.ts +++ b/src/shared/utils/env/index.ts @@ -6,6 +6,7 @@ import getHttpBaseExternal from "./get-http-base-external"; import getHttpBaseInternal from "./get-http-base-internal"; import getInternalHost from "./get-internal-host"; import getSecure from "./get-secure"; +import getStaticDir from "./get-static-dir"; import httpExternalPath from "./http-external-path"; import isHttps from "./is-https"; @@ -18,6 +19,7 @@ export { getHttpBaseInternal, getInternalHost, getSecure, + getStaticDir, httpExternalPath, isHttps, }; diff --git a/webpack.config.js b/webpack.config.js index a2b31d0..9afdb52 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -14,56 +14,62 @@ const banner = ` @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0 `; -const base = { - output: { - filename: "js/server.js", - publicPath: "/", - hashFunction: "xxhash64", - }, - resolve: { - extensions: [".js", ".jsx", ".ts", ".tsx"], - alias: { - "@": path.resolve(__dirname, "src/"), - "@utils": path.resolve(__dirname, "src/shared/utils/"), +function getBase(env) { + return { + output: { + filename: "js/server.js", + publicPath: "/", + hashFunction: "xxhash64", }, - }, - performance: { - hints: false, - }, - module: { - rules: [ - { - test: /\.(scss|css)$/i, - use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"], + resolve: { + extensions: [".js", ".jsx", ".ts", ".tsx"], + alias: { + "@": path.resolve(__dirname, "src/"), + "@utils": path.resolve(__dirname, "src/shared/utils/"), }, - { - test: /\.(js|jsx|tsx|ts)$/, // All ts and tsx files will be process by - exclude: /node_modules/, // ignore node_modules - loader: "babel-loader", - }, - // Due to some weird babel issue: https://github.com/webpack/webpack/issues/11467 - { - test: /\.m?js/, - resolve: { - fullySpecified: false, + }, + performance: { + hints: false, + }, + module: { + rules: [ + { + test: /\.(scss|css)$/i, + use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"], }, - }, + { + test: /\.(js|jsx|tsx|ts)$/, // All ts and tsx files will be process by + exclude: /node_modules/, // ignore node_modules + loader: "babel-loader", + }, + // Due to some weird babel issue: https://github.com/webpack/webpack/issues/11467 + { + test: /\.m?js/, + resolve: { + fullySpecified: false, + }, + }, + ], + }, + plugins: [ + new webpack.DefinePlugin({ + "process.env.COMMIT_HASH": `"${env.COMMIT_HASH}"`, + }), + new MiniCssExtractPlugin({ + filename: "styles/styles.css", + }), + new CopyPlugin({ + patterns: [{ from: "./src/assets", to: "./assets" }], + }), + new webpack.BannerPlugin({ + banner, + }), ], - }, - plugins: [ - new MiniCssExtractPlugin({ - filename: "styles/styles.css", - }), - new CopyPlugin({ - patterns: [{ from: "./src/assets", to: "./assets" }], - }), - new webpack.BannerPlugin({ - banner, - }), - ], -}; + }; +} -const createServerConfig = (_env, mode) => { +const createServerConfig = (env, mode) => { + const base = getBase(env); const config = merge({}, base, { mode, entry: "./src/server/index.tsx", @@ -90,13 +96,14 @@ const createServerConfig = (_env, mode) => { return config; }; -const createClientConfig = (_env, mode) => { +const createClientConfig = (env, mode) => { + const base = getBase(env); const config = merge({}, base, { mode, entry: "./src/client/index.tsx", output: { filename: "js/client.js", - publicPath: "/static/", + publicPath: `/static/${env.COMMIT_HASH}/`, }, plugins: [ ...base.plugins, @@ -104,7 +111,7 @@ const createClientConfig = (_env, mode) => { enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct workbox: { modifyURLPrefix: { - "/": "/static/", + "/": `/static/${env.COMMIT_HASH}/`, }, cacheId: "lemmy", include: [/(assets|styles|js)\/.+\..+$/g], From 80d9aac1cae64753117862143e256e4741b68d4d Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Thu, 29 Jun 2023 10:38:35 -0400 Subject: [PATCH 09/17] Fix taglines on `Home` (#1701) * fix taglines on home * fix error on admin panel --- src/shared/components/home/home.tsx | 6 ++++-- src/shared/components/home/tagline-form.tsx | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 5ef1a87..5e73367 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -279,13 +279,15 @@ export class Home extends Component { trendingCommunitiesRes, commentsRes, postsRes, - tagline: getRandomFromList(this.state?.siteRes?.taglines ?? []) - ?.content, isIsomorphic: true, }; HomeCacheService.postsRes = postsRes; } + + this.state.tagline = getRandomFromList( + this.state?.siteRes?.taglines ?? [] + )?.content; } componentWillUnmount() { diff --git a/src/shared/components/home/tagline-form.tsx b/src/shared/components/home/tagline-form.tsx index bdbe1e6..f7cf99a 100644 --- a/src/shared/components/home/tagline-form.tsx +++ b/src/shared/components/home/tagline-form.tsx @@ -141,7 +141,7 @@ export class TaglineForm extends Component { handleEditTaglineClick(d: { i: TaglineForm; index: number }, event: any) { event.preventDefault(); - if (this.state.editingRow == d.index) { + if (d.i.state.editingRow == d.index) { d.i.setState({ editingRow: undefined }); } else { d.i.setState({ editingRow: d.index }); From b73cb808e4e5424c95daf815f02cc36077a9533e Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 29 Jun 2023 10:40:19 -0400 Subject: [PATCH 10/17] v0.18.1-rc.4 --- package.json | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 3bbc0b6..cf2d78f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lemmy-ui", - "version": "0.18.1-rc.3", + "version": "0.18.1-rc.4", "description": "An isomorphic UI for lemmy", "repository": "https://github.com/LemmyNet/lemmy-ui", "license": "AGPL-3.0", @@ -22,9 +22,16 @@ "translations:update": "git submodule update --remote --recursive" }, "lint-staged": { - "*.{ts,tsx,js}": ["prettier --write", "eslint --fix"], - "*.{css, scss}": ["prettier --write"], - "package.json": ["sortpack"] + "*.{ts,tsx,js}": [ + "prettier --write", + "eslint --fix" + ], + "*.{css, scss}": [ + "prettier --write" + ], + "package.json": [ + "sortpack" + ] }, "dependencies": { "@babel/plugin-proposal-decorators": "^7.21.0", From 08370d4c4e240b68fcc49a08af02b38fbd68eafe Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 29 Jun 2023 11:12:12 -0400 Subject: [PATCH 11/17] Try increasing node memory. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 2b36581..92b3f7e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,7 +27,7 @@ COPY .git .git RUN echo "export const VERSION = '$(git describe --tag)';" > "src/shared/version.ts" RUN yarn --production --prefer-offline -RUN yarn build:prod +RUN NODE_OPTIONS="--max-old-space-size=8192" yarn build:prod # Prune the image RUN node-prune /usr/src/app/node_modules From 339cefa2b0ee206f5ca36cf2896c3473a130dc79 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Thu, 29 Jun 2023 13:14:48 -0400 Subject: [PATCH 12/17] Cache static data for a day --- src/server/index.tsx | 8 +++++++- src/server/middleware.ts | 28 +++++++++++++++++----------- src/server/utils/create-ssr-html.tsx | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/server/index.tsx b/src/server/index.tsx index e1b36e2..458d7f0 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -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"]) { diff --git a/src/server/middleware.ts b/src/server/middleware.ts index 9815e71..753fce9 100644 --- a/src/server/middleware.ts +++ b/src/server/middleware.ts @@ -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({ @@ -22,19 +22,25 @@ export function setDefaultCsp({ // 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 ( + 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=60"; + } } res.setHeader("Cache-Control", caching); diff --git a/src/server/utils/create-ssr-html.tsx b/src/server/utils/create-ssr-html.tsx index f6d46b0..ba85228 100644 --- a/src/server/utils/create-ssr-html.tsx +++ b/src/server/utils/create-ssr-html.tsx @@ -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) From fead020bdcd2e63cb9291b60ad8d5ff1e701fee9 Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Thu, 29 Jun 2023 13:28:55 -0400 Subject: [PATCH 13/17] Fix `PostListing` mobile margin layout issue (#1706) --- src/shared/components/post/post-listing.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index 586403b..5c562a4 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -403,8 +403,9 @@ export class PostListing extends Component { createdLine() { const post_view = this.postView; + return ( -
+
From d8ee0ec78aed4c40b921d2b863db6afb7630587f Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Thu, 29 Jun 2023 13:33:30 -0400 Subject: [PATCH 14/17] change max-age to 5 for non-authed responses --- src/server/middleware.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/server/middleware.ts b/src/server/middleware.ts index 753fce9..84c568d 100644 --- a/src/server/middleware.ts +++ b/src/server/middleware.ts @@ -18,7 +18,7 @@ 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 @@ -29,6 +29,7 @@ export function setCacheControl( ) { const user = UserService.Instance; let caching: string; + if ( req.path.match(/\.(js|css|txt|manifest\.webmanifest)\/?$/) || req.path.includes("/css/themelist") @@ -39,9 +40,10 @@ export function setCacheControl( if (user.auth()) { caching = "private"; } else { - caching = "public, max-age=60"; + caching = "public, max-age=5"; } } + res.setHeader("Cache-Control", caching); next(); From 9e7fec772d873e7bc46a51403d9569f70619c9b8 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 29 Jun 2023 16:20:38 -0400 Subject: [PATCH 15/17] v0.18.1-rc.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cf2d78f..9acaba4 100644 --- a/package.json +++ b/package.json @@ -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", From 2d88e42cabac606d2b7f201fcce6985f9da538f4 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Thu, 29 Jun 2023 16:33:08 -0400 Subject: [PATCH 16/17] Fix dev caching issue --- src/server/middleware.ts | 7 +++++-- webpack.config.js | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/server/middleware.ts b/src/server/middleware.ts index 84c568d..f64b781 100644 --- a/src/server/middleware.ts +++ b/src/server/middleware.ts @@ -29,10 +29,13 @@ export function setCacheControl( ) { const user = UserService.Instance; let caching: string; + console.log("In middleware"); + console.log(process.env.NODE_ENV); if ( - req.path.match(/\.(js|css|txt|manifest\.webmanifest)\/?$/) || - req.path.includes("/css/themelist") + 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"; diff --git a/webpack.config.js b/webpack.config.js index 9afdb52..0c9806d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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", From cc184a86c82943ba78588736cc19c9f4a04e22b4 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Thu, 29 Jun 2023 18:12:22 -0400 Subject: [PATCH 17/17] Fix authorized route false flag --- src/server/middleware.ts | 2 -- src/shared/utils/app/is-auth-path.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/server/middleware.ts b/src/server/middleware.ts index f64b781..235f072 100644 --- a/src/server/middleware.ts +++ b/src/server/middleware.ts @@ -29,8 +29,6 @@ export function setCacheControl( ) { const user = UserService.Instance; let caching: string; - console.log("In middleware"); - console.log(process.env.NODE_ENV); if ( process.env.NODE_ENV === "production" && diff --git a/src/shared/utils/app/is-auth-path.ts b/src/shared/utils/app/is-auth-path.ts index 0ec963a..5a201ac 100644 --- a/src/shared/utils/app/is-auth-path.ts +++ b/src/shared/utils/app/is-auth-path.ts @@ -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 ); }