feat(seo): add compression, cache-control headers, configurable frontend dist path
This commit is contained in:
+14
-2
@@ -1,6 +1,7 @@
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import express from 'express';
|
||||
import compression from 'compression';
|
||||
import multer from 'multer';
|
||||
import { rateLimit } from 'express-rate-limit';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
@@ -65,6 +66,7 @@ export function createApp(config, prisma) {
|
||||
|
||||
const app = express();
|
||||
app.set('trust proxy', '1');
|
||||
app.use(compression());
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: (req, file, cb) => cb(null, path.join(config.storageDir, 'uploads')),
|
||||
@@ -248,8 +250,18 @@ export function createApp(config, prisma) {
|
||||
fs.createReadStream(filePath).pipe(res);
|
||||
});
|
||||
|
||||
const frontendDist = path.join(import.meta.dirname, '..', 'frontend', 'dist');
|
||||
app.use(express.static(frontendDist));
|
||||
const frontendDist = config.frontendDist;
|
||||
app.use(
|
||||
express.static(frontendDist, {
|
||||
setHeaders: (res, filePath) => {
|
||||
if (filePath.endsWith('.html')) {
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
} else if (filePath.includes(`${path.sep}assets${path.sep}`)) {
|
||||
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
|
||||
}
|
||||
},
|
||||
})
|
||||
);
|
||||
app.get(/^\/(?!api\/).*/, (req, res) => {
|
||||
res.sendFile(path.join(frontendDist, 'index.html'));
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dotenv/config';
|
||||
import path from 'node:path';
|
||||
|
||||
const REQUIRED_VARS = ['STORAGE_DIR', 'DB_HOST', 'DB_USER', 'DB_PASSWORD', 'DB_NAME'];
|
||||
|
||||
@@ -11,6 +12,7 @@ export function loadConfig() {
|
||||
return {
|
||||
port: Number(process.env.PORT ?? 3000),
|
||||
storageDir: process.env.STORAGE_DIR,
|
||||
frontendDist: path.join(import.meta.dirname, '..', 'frontend', 'dist'),
|
||||
db: {
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
|
||||
Reference in New Issue
Block a user