Serve static assets from versioned path
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
@@ -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) {
|
||||
<meta name="twitter:card" content="summary_large_image"/>
|
||||
</>}
|
||||
<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-overrides.css"/>
|
||||
<link type="text/css" rel="stylesheet" href={`/share/static/${ASSET_VERSION}/photoswipe-overrides.css`}/>
|
||||
</head>
|
||||
<body>
|
||||
{(props.showTitle || props.showDownload) && (
|
||||
@@ -112,7 +113,7 @@ export function Gallery (props: GalleryProps) {
|
||||
id="ipp-init"
|
||||
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>
|
||||
</html>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user