Serve static assets from versioned path

This commit is contained in:
Alan Grainger
2026-07-01 08:45:03 +12:00
parent 00da9018cf
commit abd297553c
3 changed files with 34 additions and 3 deletions
+8
View File
@@ -26,6 +26,7 @@ import { canDownload } from './share'
import { toString } from './utils/text' import { toString } from './utils/text'
import { decrypt, encrypt } from './encrypt' import { decrypt, encrypt } from './encrypt'
import { respondToInvalidRequest } from './invalidRequestHandler' import { respondToInvalidRequest } from './invalidRequestHandler'
import { ASSET_VERSION } from './version'
import { h } from 'preact' import { h } from 'preact'
import { renderPage } from './view/render' import { renderPage } from './view/render'
import { Home } from './view/home' import { Home } from './view/home'
@@ -52,6 +53,13 @@ app.use(cookieSession({
app.use(express.json()) app.use(express.json())
// For parsing the selective-download form POST (form-encoded body) // For parsing the selective-download form POST (form-encoded body)
app.use(express.urlencoded({ extended: false, limit: '1mb' })) 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 // Serve static assets from the 'public' folder as /share/static
app.use('/share/static', express.static('public', { setHeaders: addResponseHeaders })) app.use('/share/static', express.static('public', { setHeaders: addResponseHeaders }))
// Serve the same assets on /, to allow for /robots.txt and /favicon.ico // Serve the same assets on /, to allow for /robots.txt and /favicon.ico
+22
View File
@@ -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)
+4 -3
View File
@@ -1,6 +1,7 @@
import { AssetType } from '../types' import { AssetType } from '../types'
import { ThemeScript } from './theme' import { ThemeScript } from './theme'
import { GalleryItem, LightboxConfig, MetadataConfig } from '../shared/types' import { GalleryItem, LightboxConfig, MetadataConfig } from '../shared/types'
import { ASSET_VERSION } from '../version'
export type { GalleryItem, LightboxConfig, MetadataConfig } export type { GalleryItem, LightboxConfig, MetadataConfig }
@@ -57,9 +58,9 @@ export function Gallery (props: GalleryProps) {
<meta name="twitter:card" content="summary_large_image"/> <meta name="twitter:card" content="summary_large_image"/>
</>} </>}
<link rel="icon" href="/share/static/favicon.ico" type="image/x-icon"/> <link rel="icon" href="/share/static/favicon.ico" type="image/x-icon"/>
<link type="text/css" rel="stylesheet" href="/share/static/style.css"/> <link type="text/css" rel="stylesheet" href={`/share/static/${ASSET_VERSION}/style.css`}/>
<link type="text/css" rel="stylesheet" href="/share/static/photoswipe/photoswipe.css"/> <link type="text/css" rel="stylesheet" href="/share/static/photoswipe/photoswipe.css"/>
<link type="text/css" rel="stylesheet" href="/share/static/photoswipe-overrides.css"/> <link type="text/css" rel="stylesheet" href={`/share/static/${ASSET_VERSION}/photoswipe-overrides.css`}/>
</head> </head>
<body> <body>
{(props.showTitle || props.showDownload) && ( {(props.showTitle || props.showDownload) && (
@@ -112,7 +113,7 @@ export function Gallery (props: GalleryProps) {
id="ipp-init" id="ipp-init"
dangerouslySetInnerHTML={{ __html: initJson }} dangerouslySetInnerHTML={{ __html: initJson }}
/> />
<script type="module" src="/share/static/js/client/init.js"></script> <script type="module" src={`/share/static/${ASSET_VERSION}/js/client/init.js`}></script>
</body> </body>
</html> </html>
) )