perf: precompress and cache assets (#7757)

* perf: precompress and cache assets

* fix cache header

* use startswith

---------

Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com>
This commit is contained in:
Ben McCann 2024-03-08 18:15:38 -08:00 committed by GitHub
parent 4fdb0835c9
commit ba55e867e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 77 additions and 7 deletions

View file

@ -5,6 +5,7 @@ import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { json } from 'body-parser';
import cookieParser from 'cookie-parser';
import sirv from 'sirv';
import { AppModule } from './app.module';
import { AppService } from './app.service';
import { useSwagger } from './app.utils';
@ -28,7 +29,20 @@ export async function bootstrap() {
const excludePaths = ['/.well-known/immich', '/custom.css'];
app.setGlobalPrefix('api', { exclude: excludePaths });
app.useStaticAssets('www');
// copied from https://github.com/sveltejs/kit/blob/679b5989fe62e3964b9a73b712d7b41831aa1f07/packages/adapter-node/src/handler.js#L46
// provides serving of precompressed assets and caching of immutable assets
app.use(
sirv('www', {
etag: true,
gzip: true,
brotli: true,
setHeaders: (res, pathname) => {
if (pathname.startsWith(`/_app/immutable`) && res.statusCode === 200) {
res.setHeader('cache-control', 'public,max-age=31536000,immutable');
}
},
}),
);
app.use(app.get(AppService).ssr(excludePaths));
const server = await app.listen(port);