Skip to content

Commit

Permalink
perf: precompress and cache assets (immich-app#7757)
Browse files Browse the repository at this point in the history
* perf: precompress and cache assets

* fix cache header

* use startswith

---------

Co-authored-by: mertalev <[email protected]>
  • Loading branch information
benmccann and mertalev authored Mar 9, 2024
1 parent 4fdb083 commit ba55e86
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
60 changes: 60 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"rxjs": "^7.8.1",
"sanitize-filename": "^1.6.3",
"sharp": "^0.33.0",
"sirv": "^2.0.4",
"thumbhash": "^0.1.1",
"typeorm": "^0.3.17",
"ua-parser-js": "^1.0.35"
Expand Down
16 changes: 15 additions & 1 deletion server/src/immich/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions web/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ const config = {
preprocess: vitePreprocess(),
kit: {
adapter: adapter({
// default options are shown. On some platforms
// these options are set automatically — see below
pages: 'build',
assets: 'build',
fallback: 'index.html',
precompress: false,
strict: true,
precompress: true,
}),
alias: {
$lib: 'src/lib',
Expand Down

0 comments on commit ba55e86

Please sign in to comment.