From 078da36f2055b2cb8f0b38588b15c37a542d9941 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:06:04 -0700 Subject: [PATCH] fix(server): serve static directory only if it exists (#7857) * fix(server): serve static directory only if it exists * update * refactor: web root --------- Co-authored-by: Jason Rasmussen --- server/src/domain/domain.constant.ts | 3 +-- server/src/immich/app.service.ts | 5 +++-- server/src/immich/main.ts | 33 +++++++++++++++------------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/server/src/domain/domain.constant.ts b/server/src/domain/domain.constant.ts index 0dc9c54140..b723474dd3 100644 --- a/server/src/domain/domain.constant.ts +++ b/server/src/domain/domain.constant.ts @@ -82,8 +82,7 @@ const { version } = JSON.parse(readFileSync('./package.json', 'utf8')); export const serverVersion = Version.fromString(version); export const APP_MEDIA_LOCATION = process.env.IMMICH_MEDIA_LOCATION || './upload'; - -export const WEB_ROOT_PATH = join(process.env.IMMICH_WEB_ROOT || '/usr/src/app/www', 'index.html'); +export const WEB_ROOT = process.env.IMMICH_WEB_ROOT || '/usr/src/app/www'; const GEODATA_ROOT_PATH = process.env.IMMICH_REVERSE_GEOCODING_ROOT || '/usr/src/resources'; diff --git a/server/src/immich/app.service.ts b/server/src/immich/app.service.ts index f3369b1210..adfb9d8780 100644 --- a/server/src/immich/app.service.ts +++ b/server/src/immich/app.service.ts @@ -8,13 +8,14 @@ import { SharedLinkService, StorageService, SystemConfigService, - WEB_ROOT_PATH, + WEB_ROOT, } from '@app/domain'; import { ImmichLogger } from '@app/infra/logger'; import { Injectable } from '@nestjs/common'; import { Cron, CronExpression, Interval } from '@nestjs/schedule'; import { NextFunction, Request, Response } from 'express'; import { readFileSync } from 'node:fs'; +import { join } from 'node:path'; const render = (index: string, meta: OpenGraphTags) => { const tags = ` @@ -71,7 +72,7 @@ export class AppService { ssr(excludePaths: string[]) { let index = ''; try { - index = readFileSync(WEB_ROOT_PATH).toString(); + index = readFileSync(join(WEB_ROOT, 'index.html')).toString(); } catch { this.logger.warn('Unable to open `www/index.html, skipping SSR.'); } diff --git a/server/src/immich/main.ts b/server/src/immich/main.ts index 0039019b62..d2fe44f1f3 100644 --- a/server/src/immich/main.ts +++ b/server/src/immich/main.ts @@ -1,10 +1,11 @@ -import { envName, isDev, serverVersion } from '@app/domain'; +import { WEB_ROOT, envName, isDev, serverVersion } from '@app/domain'; import { WebSocketAdapter } from '@app/infra'; import { ImmichLogger } from '@app/infra/logger'; import { NestFactory } from '@nestjs/core'; import { NestExpressApplication } from '@nestjs/platform-express'; import { json } from 'body-parser'; import cookieParser from 'cookie-parser'; +import { existsSync } from 'node:fs'; import sirv from 'sirv'; import { AppModule } from './app.module'; import { AppService } from './app.service'; @@ -29,20 +30,22 @@ export async function bootstrap() { const excludePaths = ['/.well-known/immich', '/custom.css']; app.setGlobalPrefix('api', { exclude: excludePaths }); - // 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'); - } - }, - }), - ); + if (existsSync(WEB_ROOT)) { + // 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(WEB_ROOT, { + 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);