Skip to content

Commit 6be1b91

Browse files
FindHaofacebook-github-bot
authored andcommitted
Show build date (localized) and Git short SHA in footer (#97)
Summary: ### Motivation - Improve traceability of website builds by displaying: - the app version (from `package.json`), - the build timestamp (localized at runtime via `toLocaleString()`), and - the Git commit short SHA used for the build. ### Summary of Changes - Inject build metadata at build time via Vite `define`: - `import.meta.env.PACKAGE_VERSION` (existing) → from `package.json`. - `import.meta.env.PACKAGE_BUILD_DATE` → ISO string from `BUILD_DATE` env or `new Date().toISOString()`. - `import.meta.env.GIT_COMMIT_SHA_SHORT` → from `GIT_COMMIT_SHA_SHORT` env or `git rev-parse --short HEAD`, with fallback to `"unknown"`. - Extend TypeScript env typings (`src/vite-env.d.ts`) with the two new fields. - Render in footer (localized date): - Example format: `Version 0.1.1 · Built 9/10/2025, 4:21:45 PM · git:abc1234`. ### Files Touched - `website/vite.config.ts` - Injected `PACKAGE_BUILD_DATE` and `GIT_COMMIT_SHA_SHORT` into `import.meta.env` using Vite `define`. - Safe fallbacks: if not in a Git repo/CI without Git, value becomes `"unknown"`. - `website/src/vite-env.d.ts` - Added typings for `PACKAGE_BUILD_DATE` and `GIT_COMMIT_SHA_SHORT`. - `website/src/App.tsx` - Footer now shows: version, localized build date (`toLocaleString()`), and short SHA. ### Implementation Details - Build-time values are set once when Vite starts (dev) or during `vite build` (prod): - Dev server must be restarted to refresh the build date/SHA. - `toLocaleString()` uses the user's browser locale/timezone for display. - CI environments can optionally pass explicit values to ensure reproducible metadata: - `BUILD_DATE=2025-09-10T08:00:00Z` - `GIT_COMMIT_SHA_SHORT=abc123` - If `git` is unavailable or the working directory is not a Git repo, SHA falls back to `"unknown"`. ### How to Test 1) Development ```bash cd website npm run dev # Open the app. Footer should display: # Version <pkgVersion> · Built <localizedDateTime> · <shortSHA or unknown> ``` 2) Production build ```bash cd website npm run build npm run preview # Verify footer shows version, localized build date, and short SHA ``` 3) Override via environment variables (optional) ```bash # Dev BUILD_DATE=2025-09-10T08:00:00Z GIT_COMMIT_SHA_SHORT=abc123 npm run dev # Build BUILD_DATE=2025-09-10T08:00:00Z GIT_COMMIT_SHA_SHORT=abc123 npm run build ``` ### Risk/Impact - Low. Purely presentational. No runtime network calls introduced. - Backward-compatible: existing `PACKAGE_VERSION` use remains unchanged; new values have safe fallbacks. ### Notes for CI - Ensure the repo is a Git checkout if you want automatic SHA detection, or provide `GIT_COMMIT_SHA_SHORT` explicitly. - The build date defaults to the build machine time unless `BUILD_DATE` is provided. Pull Request resolved: #97 Reviewed By: sfzhu93 Differential Revision: D82164288 Pulled By: FindHao fbshipit-source-id: 1b24f0d33357a9273f4b04d8b5409b2cb6d05c7f
1 parent 3ff7ac6 commit 6be1b91

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

website/src/App.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,14 @@ function App() {
534534
<a href="https://opensource.fb.com/legal/privacy" className="hover:underline">Privacy Policy</a>
535535
</div>
536536
<div className="text-gray-500">
537-
Version {import.meta.env.PACKAGE_VERSION}
537+
{(() => {
538+
const buildDate = new Date(import.meta.env.PACKAGE_BUILD_DATE)
539+
const localized = isNaN(buildDate.getTime())
540+
? import.meta.env.PACKAGE_BUILD_DATE
541+
: buildDate.toLocaleString()
542+
const sha = import.meta.env.GIT_COMMIT_SHA_SHORT
543+
return `Version ${import.meta.env.PACKAGE_VERSION} · Built ${localized} · ${sha}`
544+
})()}
538545
</div>
539546
</div>
540547
</footer>

website/src/vite-env.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
interface ImportMetaEnv {
44
readonly PACKAGE_VERSION: string;
5+
readonly PACKAGE_BUILD_DATE: string;
6+
readonly GIT_COMMIT_SHA_SHORT: string;
57
}
68

79
interface ImportMeta {

website/vite.config.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,51 @@ import { defineConfig } from 'vite'
22
import react from '@vitejs/plugin-react'
33
import { readFileSync } from 'fs'
44
import { resolve } from 'path'
5+
import { execSync } from 'child_process'
6+
7+
// Execute a shell command and return trimmed stdout, or null on failure.
8+
function safeExecTrim(command: string): string | null {
9+
try {
10+
return execSync(command).toString().trim() || null
11+
} catch {
12+
return null
13+
}
14+
}
515

616
const packageJson = JSON.parse(
717
readFileSync(resolve(__dirname, 'package.json'), 'utf-8')
818
)
919

20+
// Build-time metadata
21+
const buildDate = process.env.BUILD_DATE || new Date().toISOString()
22+
23+
// Resolve a short commit SHA for build metadata.
24+
// Precedence:
25+
// 1) GIT_COMMIT_SHA_SHORT environment variable (CI can set this explicitly)
26+
// 2) git rev-parse --short HEAD -> returns "git:<sha>"
27+
// 3) hg id -i (Mercurial) -> returns "hg:<sha>"
28+
// 4) 'unknown' (neither VCS available)
29+
function resolveCommitSha(): string {
30+
const envSha = process.env.GIT_COMMIT_SHA_SHORT
31+
if (envSha) return envSha
32+
33+
// Try Git first, then Mercurial.
34+
const candidates: Array<{ cmd: string; prefix: string }> = [
35+
{ cmd: 'git rev-parse --short HEAD', prefix: 'git:' },
36+
{ cmd: 'hg id -i', prefix: 'hg:' }
37+
]
38+
39+
for (const { cmd, prefix } of candidates) {
40+
const out = safeExecTrim(cmd)
41+
if (out) return `${prefix}${out}`
42+
}
43+
// Final fallback when no VCS is present or accessible.
44+
return 'unknown'
45+
}
46+
47+
// Compute once at config load; injected below via define.
48+
const gitSha = resolveCommitSha()
49+
1050
export default defineConfig({
1151
plugins: [
1252
react(),
@@ -36,6 +76,8 @@ export default defineConfig({
3676
}
3777
},
3878
define: {
39-
'import.meta.env.PACKAGE_VERSION': JSON.stringify(packageJson.version)
79+
'import.meta.env.PACKAGE_VERSION': JSON.stringify(packageJson.version),
80+
'import.meta.env.PACKAGE_BUILD_DATE': JSON.stringify(buildDate),
81+
'import.meta.env.GIT_COMMIT_SHA_SHORT': JSON.stringify(gitSha)
4082
}
4183
})

0 commit comments

Comments
 (0)