From ce55211aec91d832ef6b9669ce9b0d7240efd9fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahlstr=C3=B6m=20Kalle?= Date: Sun, 21 Jan 2024 02:49:40 +0200 Subject: [PATCH] fix slow serving of static files by precompressing them --- Dockerfile | 8 ++++++-- packages/ilmomasiina-backend/src/app.ts | 16 ++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 08cd9f04..0502efd1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ # Build stage: -FROM node:14-alpine as builder - +FROM node:16-alpine as builder +RUN apk add --no-cache brotli # Build-time env variables ARG SENTRY_DSN ARG PATH_PREFIX @@ -26,6 +26,10 @@ ENV NODE_ENV=production # Build all packages RUN npm run build +# precompress static files for frontend +RUN find packages/ilmomasiina-frontend/build -type f\ + -regex ".*\.\(js\|json\|html\|map\|css\|svg\|ico\|txt\)" -exec gzip -k "{}" \; -exec brotli "{}" \; + # Main stage: FROM node:16-alpine diff --git a/packages/ilmomasiina-backend/src/app.ts b/packages/ilmomasiina-backend/src/app.ts index 97b3d1ae..aa51de6d 100644 --- a/packages/ilmomasiina-backend/src/app.ts +++ b/packages/ilmomasiina-backend/src/app.ts @@ -80,17 +80,10 @@ export default async function initApp(): Promise { } } - // Add on-the-fly compression - server.register(fastifyCompress, { inflateIfDeflated: true }); - - server.register(setupRoutes, { - prefix: '/api', - adminSession: new AdminAuthSession(config.feathersAuthSecret), - }); - // Serving frontend files if frontendFilesPath is not null. // Ideally these files should be served by a web server and not the app server, // but this helps run a low-effort server. + // frontend files should not be gzipped on the fly, rather done on the build step. if (config.frontendFilesPath) { console.info(`Serving frontend files from '${config.frontendFilesPath}'`); server.register(fastifyStatic, { @@ -102,6 +95,13 @@ export default async function initApp(): Promise { reply.sendFile('index.html'); }); } + // Add on-the-fly compression + server.register(fastifyCompress, { inflateIfDeflated: true }); + + server.register(setupRoutes, { + prefix: '/api', + adminSession: new AdminAuthSession(config.feathersAuthSecret), + }); // Every minute, remove signups that haven't been confirmed fast enough cron.schedule('* * * * *', deleteUnconfirmedSignups);