From f9c54b517206f30b548387562dc45ce85833782f Mon Sep 17 00:00:00 2001 From: Egor Pozharov Date: Mon, 4 May 2026 16:08:37 +0600 Subject: [PATCH] add build version tracking with commit SHA, version number, and build timestamp to frontend --- .gitea/workflows/deploy.yml | 2 ++ frontend/Dockerfile | 3 +++ frontend/src/components/ui/UpdatePrompt.tsx | 16 ++++++++++++++-- frontend/src/vite-env.d.ts | 5 +++++ frontend/vite.config.ts | 16 +++++++++++++++- 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 frontend/src/vite-env.d.ts diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 15be4e7..a2f1abe 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -43,6 +43,8 @@ jobs: with: context: ./frontend push: true + build-args: | + GITEA_SHA=${{ gitea.sha }} tags: | gitea.gitea.chedius.ru/${{ gitea.repository_owner }}/delivery-tracker/frontend:latest gitea.gitea.chedius.ru/${{ gitea.repository_owner }}/delivery-tracker/frontend:${{ gitea.sha }} diff --git a/frontend/Dockerfile b/frontend/Dockerfile index dd00ae3..b659e29 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -5,6 +5,9 @@ FROM node:20-alpine AS builder WORKDIR /app +ARG GITEA_SHA +ARG COMMIT_SHA + # Copy package files COPY package.json yarn.lock ./ RUN yarn install --frozen-lockfile diff --git a/frontend/src/components/ui/UpdatePrompt.tsx b/frontend/src/components/ui/UpdatePrompt.tsx index 08211e8..daeb8c7 100644 --- a/frontend/src/components/ui/UpdatePrompt.tsx +++ b/frontend/src/components/ui/UpdatePrompt.tsx @@ -4,21 +4,31 @@ import { useRegisterSW } from 'virtual:pwa-register/react'; // Check for SW updates every hour and on tab focus/visibility change const UPDATE_INTERVAL_MS = 60 * 60 * 1000; +const buildLabel = `${__APP_VERSION__}-${__APP_COMMIT__}`; + export function UpdatePrompt() { const { needRefresh: [needRefresh], updateServiceWorker, } = useRegisterSW({ onRegisteredSW(_swUrl, registration) { + console.info('[App] version', { + version: __APP_VERSION__, + commit: __APP_COMMIT__, + builtAt: __APP_BUILT_AT__, + serviceWorker: registration ? 'registered' : 'unavailable', + }); + if (!registration) return; const checkForUpdate = async () => { if (registration.installing || !navigator) return; if ('connection' in navigator && !navigator.onLine) return; try { + console.debug('[PWA] checking for update', { version: buildLabel }); await registration.update(); } catch { - // network error — ignore, will retry + console.debug('[PWA] update check failed, will retry later', { version: buildLabel }); } }; @@ -34,7 +44,9 @@ export function UpdatePrompt() { useEffect(() => { if (needRefresh) { - console.log('[PWA] New version detected, auto-updating...'); + console.info('[PWA] new version detected, applying automatically', { + currentVersion: buildLabel, + }); updateServiceWorker(true); } }, [needRefresh, updateServiceWorker]); diff --git a/frontend/src/vite-env.d.ts b/frontend/src/vite-env.d.ts new file mode 100644 index 0000000..b387277 --- /dev/null +++ b/frontend/src/vite-env.d.ts @@ -0,0 +1,5 @@ +/// + +declare const __APP_VERSION__: string; +declare const __APP_COMMIT__: string; +declare const __APP_BUILT_AT__: string; diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 1b04995..96a8d02 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -3,8 +3,22 @@ import react from '@vitejs/plugin-react' import tailwindcss from '@tailwindcss/vite' import { VitePWA } from 'vite-plugin-pwa' +const appVersion = process.env.npm_package_version ?? '0.0.0' +const appCommit = ( + process.env.GITEA_SHA ?? + process.env.GITHUB_SHA ?? + process.env.COMMIT_SHA ?? + 'dev' +).slice(0, 12) +const appBuiltAt = new Date().toISOString() + // https://vite.dev/config/ export default defineConfig({ + define: { + __APP_VERSION__: JSON.stringify(appVersion), + __APP_COMMIT__: JSON.stringify(appCommit), + __APP_BUILT_AT__: JSON.stringify(appBuiltAt), + }, plugins: [ react(), tailwindcss(), @@ -35,4 +49,4 @@ export default defineConfig({ }, }, }, -}) \ No newline at end of file +})