diff --git a/app/src/index.ts b/app/src/index.ts index a0f0c9e..118cb8a 100644 --- a/app/src/index.ts +++ b/app/src/index.ts @@ -26,6 +26,7 @@ import { canDownload } from './share' import { toString } from './utils/text' import { decrypt, encrypt } from './encrypt' import { respondToInvalidRequest } from './invalidRequestHandler' +import { ASSET_VERSION } from './version' import { h } from 'preact' import { renderPage } from './view/render' import { Home } from './view/home' @@ -52,6 +53,13 @@ app.use(cookieSession({ app.use(express.json()) // For parsing the selective-download form POST (form-encoded body) app.use(express.urlencoded({ extended: false, limit: '1mb' })) +// Cache-busted, immutable static assets under a per-release version segment. +const inProduction = process.env.NODE_ENV === 'production' +app.use('/share/static/' + ASSET_VERSION, express.static('public', { + immutable: inProduction, + maxAge: inProduction ? '365d' : 0, + setHeaders: addResponseHeaders +})) // Serve static assets from the 'public' folder as /share/static app.use('/share/static', express.static('public', { setHeaders: addResponseHeaders })) // Serve the same assets on /, to allow for /robots.txt and /favicon.ico diff --git a/app/src/version.ts b/app/src/version.ts new file mode 100644 index 0000000..17405af --- /dev/null +++ b/app/src/version.ts @@ -0,0 +1,22 @@ +import { readFileSync } from 'fs' +import { join } from 'path' + +/** + * Resolve the running application version. Prefers the APP_VERSION env var + * baked in at Docker build time (see Dockerfile); falls back to package.json + * for local dev (`npm run dev`), and finally to 'dev' if neither is readable. + */ +function resolveVersion (): string { + if (process.env.APP_VERSION) return process.env.APP_VERSION + try { + const pkg = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'), 'utf-8')) + return pkg.version || 'dev' + } catch { + return 'dev' + } +} + +export const APP_VERSION = resolveVersion() + +/** URL-safe cache-busting segment for static asset paths. */ +export const ASSET_VERSION = encodeURIComponent(APP_VERSION) diff --git a/app/src/view/gallery.tsx b/app/src/view/gallery.tsx index aa740e4..68eeb8d 100644 --- a/app/src/view/gallery.tsx +++ b/app/src/view/gallery.tsx @@ -1,6 +1,7 @@ import { AssetType } from '../types' import { ThemeScript } from './theme' import { GalleryItem, LightboxConfig, MetadataConfig } from '../shared/types' +import { ASSET_VERSION } from '../version' export type { GalleryItem, LightboxConfig, MetadataConfig } @@ -57,9 +58,9 @@ export function Gallery (props: GalleryProps) { } - + - + {(props.showTitle || props.showDownload) && ( @@ -112,7 +113,7 @@ export function Gallery (props: GalleryProps) { id="ipp-init" dangerouslySetInnerHTML={{ __html: initJson }} /> - + )