mirror of
				https://github.com/immich-app/immich.git
				synced 2025-11-04 03:27:09 -05:00 
			
		
		
		
	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 <jrasm91@gmail.com>
This commit is contained in:
		
							parent
							
								
									6c8fad4cac
								
							
						
					
					
						commit
						078da36f20
					
				@ -82,8 +82,7 @@ const { version } = JSON.parse(readFileSync('./package.json', 'utf8'));
 | 
				
			|||||||
export const serverVersion = Version.fromString(version);
 | 
					export const serverVersion = Version.fromString(version);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const APP_MEDIA_LOCATION = process.env.IMMICH_MEDIA_LOCATION || './upload';
 | 
					export const APP_MEDIA_LOCATION = process.env.IMMICH_MEDIA_LOCATION || './upload';
 | 
				
			||||||
 | 
					export const WEB_ROOT = process.env.IMMICH_WEB_ROOT || '/usr/src/app/www';
 | 
				
			||||||
export const WEB_ROOT_PATH = join(process.env.IMMICH_WEB_ROOT || '/usr/src/app/www', 'index.html');
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
const GEODATA_ROOT_PATH = process.env.IMMICH_REVERSE_GEOCODING_ROOT || '/usr/src/resources';
 | 
					const GEODATA_ROOT_PATH = process.env.IMMICH_REVERSE_GEOCODING_ROOT || '/usr/src/resources';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -8,13 +8,14 @@ import {
 | 
				
			|||||||
  SharedLinkService,
 | 
					  SharedLinkService,
 | 
				
			||||||
  StorageService,
 | 
					  StorageService,
 | 
				
			||||||
  SystemConfigService,
 | 
					  SystemConfigService,
 | 
				
			||||||
  WEB_ROOT_PATH,
 | 
					  WEB_ROOT,
 | 
				
			||||||
} from '@app/domain';
 | 
					} from '@app/domain';
 | 
				
			||||||
import { ImmichLogger } from '@app/infra/logger';
 | 
					import { ImmichLogger } from '@app/infra/logger';
 | 
				
			||||||
import { Injectable } from '@nestjs/common';
 | 
					import { Injectable } from '@nestjs/common';
 | 
				
			||||||
import { Cron, CronExpression, Interval } from '@nestjs/schedule';
 | 
					import { Cron, CronExpression, Interval } from '@nestjs/schedule';
 | 
				
			||||||
import { NextFunction, Request, Response } from 'express';
 | 
					import { NextFunction, Request, Response } from 'express';
 | 
				
			||||||
import { readFileSync } from 'node:fs';
 | 
					import { readFileSync } from 'node:fs';
 | 
				
			||||||
 | 
					import { join } from 'node:path';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const render = (index: string, meta: OpenGraphTags) => {
 | 
					const render = (index: string, meta: OpenGraphTags) => {
 | 
				
			||||||
  const tags = `
 | 
					  const tags = `
 | 
				
			||||||
@ -71,7 +72,7 @@ export class AppService {
 | 
				
			|||||||
  ssr(excludePaths: string[]) {
 | 
					  ssr(excludePaths: string[]) {
 | 
				
			||||||
    let index = '';
 | 
					    let index = '';
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      index = readFileSync(WEB_ROOT_PATH).toString();
 | 
					      index = readFileSync(join(WEB_ROOT, 'index.html')).toString();
 | 
				
			||||||
    } catch {
 | 
					    } catch {
 | 
				
			||||||
      this.logger.warn('Unable to open `www/index.html, skipping SSR.');
 | 
					      this.logger.warn('Unable to open `www/index.html, skipping SSR.');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -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 { WebSocketAdapter } from '@app/infra';
 | 
				
			||||||
import { ImmichLogger } from '@app/infra/logger';
 | 
					import { ImmichLogger } from '@app/infra/logger';
 | 
				
			||||||
import { NestFactory } from '@nestjs/core';
 | 
					import { NestFactory } from '@nestjs/core';
 | 
				
			||||||
import { NestExpressApplication } from '@nestjs/platform-express';
 | 
					import { NestExpressApplication } from '@nestjs/platform-express';
 | 
				
			||||||
import { json } from 'body-parser';
 | 
					import { json } from 'body-parser';
 | 
				
			||||||
import cookieParser from 'cookie-parser';
 | 
					import cookieParser from 'cookie-parser';
 | 
				
			||||||
 | 
					import { existsSync } from 'node:fs';
 | 
				
			||||||
import sirv from 'sirv';
 | 
					import sirv from 'sirv';
 | 
				
			||||||
import { AppModule } from './app.module';
 | 
					import { AppModule } from './app.module';
 | 
				
			||||||
import { AppService } from './app.service';
 | 
					import { AppService } from './app.service';
 | 
				
			||||||
@ -29,20 +30,22 @@ export async function bootstrap() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  const excludePaths = ['/.well-known/immich', '/custom.css'];
 | 
					  const excludePaths = ['/.well-known/immich', '/custom.css'];
 | 
				
			||||||
  app.setGlobalPrefix('api', { exclude: excludePaths });
 | 
					  app.setGlobalPrefix('api', { exclude: excludePaths });
 | 
				
			||||||
  // copied from https://github.com/sveltejs/kit/blob/679b5989fe62e3964b9a73b712d7b41831aa1f07/packages/adapter-node/src/handler.js#L46
 | 
					  if (existsSync(WEB_ROOT)) {
 | 
				
			||||||
  // provides serving of precompressed assets and caching of immutable assets
 | 
					    // copied from https://github.com/sveltejs/kit/blob/679b5989fe62e3964b9a73b712d7b41831aa1f07/packages/adapter-node/src/handler.js#L46
 | 
				
			||||||
  app.use(
 | 
					    // provides serving of precompressed assets and caching of immutable assets
 | 
				
			||||||
    sirv('www', {
 | 
					    app.use(
 | 
				
			||||||
      etag: true,
 | 
					      sirv(WEB_ROOT, {
 | 
				
			||||||
      gzip: true,
 | 
					        etag: true,
 | 
				
			||||||
      brotli: true,
 | 
					        gzip: true,
 | 
				
			||||||
      setHeaders: (res, pathname) => {
 | 
					        brotli: true,
 | 
				
			||||||
        if (pathname.startsWith(`/_app/immutable`) && res.statusCode === 200) {
 | 
					        setHeaders: (res, pathname) => {
 | 
				
			||||||
          res.setHeader('cache-control', 'public,max-age=31536000,immutable');
 | 
					          if (pathname.startsWith(`/_app/immutable`) && res.statusCode === 200) {
 | 
				
			||||||
        }
 | 
					            res.setHeader('cache-control', 'public,max-age=31536000,immutable');
 | 
				
			||||||
      },
 | 
					          }
 | 
				
			||||||
    }),
 | 
					        },
 | 
				
			||||||
  );
 | 
					      }),
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  app.use(app.get(AppService).ssr(excludePaths));
 | 
					  app.use(app.get(AppService).ssr(excludePaths));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const server = await app.listen(port);
 | 
					  const server = await app.listen(port);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user