Skip to content

Commit

Permalink
fix slow serving of static files by precompressing them
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlstrm committed Jan 21, 2024
1 parent 52f1267 commit ce55211
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
16 changes: 8 additions & 8 deletions packages/ilmomasiina-backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,10 @@ export default async function initApp(): Promise<FastifyInstance> {
}
}

// 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, {
Expand All @@ -102,6 +95,13 @@ export default async function initApp(): Promise<FastifyInstance> {
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);
Expand Down

0 comments on commit ce55211

Please sign in to comment.