Files
tehran_tala/apps/user-app/src/composables/useAppUpdate.ts
T
s.esmaeiliandClaude Opus 5.5 dbead8a6ba feat(apps): version on every build, a «what changed» modal, and a clean update
- The build number is the commit count, so every build raises it with no
  file to bump. scripts/app-version.mjs bakes it into each front-end
  (__APP_BUILD__) and writes dist/version.json with that app's notes.
- Notes come from commit messages: only Persian lines that start with
  one of the four app tags are shown, each to its own app
  (docs/RELEASE-NOTES.md).
- Both customer apps and the admin panel poll version.json (every 5
  minutes and on return to the tab, bypassing caches). A newer build opens
  a modal that cannot be dismissed. «به‌روزرسانی» unregisters the service
  worker, empties Cache Storage and reloads; localStorage (sign-in, cart,
  settings) is kept.
- Replaces legacy's version dialog, which compared against a
  package.json version that was never bumped and so never opened, and
  useRefresh, which swapped in a new service worker silently.
- nginx (all three apps and nginx-spa.conf): index.html, sw.js and
  version.json are never cached. sw.js used to fall under the 6-month rule,
  which could pin a browser to an old release.

Checked: served with the real nginx config, a newer version.json opens
the modal with its notes; the same build does not.

اپ: اجرت هر کالا حالا همان است که در فاکتور حساب می‌شود؛ درصد خود شما و تخفیف‌ها هم در آن آمده است.
اپ: علاقه‌مندی‌های شما ذخیره می‌شوند و در هر دستگاهی که وارد شوید دیده می‌شوند.
اپ: از این به بعد هر نسخه‌ی تازه با فهرست تغییراتش اعلام می‌شود.
پنل: بخش تازه‌ی «اجرت»: فرمول اجرت را با یک نمونه امتحان و بعد تأیید کنید.
پنل: شهرها مثل قبل به‌صورت درخت‌اند (کشور، استان، شهر، محله)؛ شهری که درصد ندارد درصد شهر بالاتر را می‌گیرد.
پنل: «تاریخ تولد» به فرم مشتری عمده و تکی برگشت.
پنل: در گزارش تجمیعی سفارش‌ها، دکمه‌ی «دریافت فایل اکسل» اضافه شد.
پنل: رد احراز هویت مشتری حالا دلیل هم می‌گیرد.
پنل: با تأیید یا رد سند مالی، به مشتری پیامک می‌رود؛ در پیامک گروهی، %name با نام مشتری عوض می‌شود.
پنل: از این به بعد هر نسخه‌ی تازه‌ی پنل با فهرست تغییراتش اعلام می‌شود.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 12:45:52 +03:30

85 lines
2.9 KiB
TypeScript

// The update gate (scripts/app-version.mjs builds the other half).
//
// The bundle knows its own build number (__APP_BUILD__). Every few minutes,
// and whenever the app comes back to the foreground, it reads version.json
// from the server, bypassing every cache. A newer build there means a release
// went out: the modal lists what changed since the build this tab is running,
// and «به‌روزرسانی» drops the service worker and every Cache Storage entry,
// then reloads — so the next load is the new release, not a precached copy.
// localStorage is left alone: sign-in, cart and settings survive the update.
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
declare const __APP_BUILD__: number
declare const __APP_VERSION__: string
export interface VersionInfo {
build: number
version: string
history: { build: number; date: string; notes: string[] }[]
}
const CHECK_EVERY_MS = 5 * 60 * 1000
export const runningBuild: number = typeof __APP_BUILD__ === 'number' ? __APP_BUILD__ : 0
export const runningVersion: string = typeof __APP_VERSION__ === 'string' ? __APP_VERSION__ : ''
export function useAppUpdate() {
const available = ref<VersionInfo | null>(null)
const updating = ref(false)
/** What changed between the running build and the server's, newest first. */
const changes = computed(() => {
const info = available.value
if (!info) return []
return (info.history ?? []).filter((h) => h.build > runningBuild && h.build <= info.build)
})
async function check() {
if (import.meta.env.DEV || !runningBuild || available.value) return
try {
const res = await fetch(`${import.meta.env.BASE_URL}version.json?t=${Date.now()}`, { cache: 'no-store' })
if (!res.ok) return
const info = (await res.json()) as VersionInfo
if (typeof info.build === 'number' && info.build > runningBuild) available.value = info
} catch {
// Offline or a deploy in progress: try again on the next tick.
}
}
async function applyUpdate() {
updating.value = true
try {
const regs = await navigator.serviceWorker?.getRegistrations?.()
if (regs) await Promise.all(regs.map((r) => r.unregister()))
} catch {
/* no service worker */
}
try {
if ('caches' in window) {
const keys = await caches.keys()
await Promise.all(keys.map((k) => caches.delete(k)))
}
} catch {
/* Cache Storage unavailable */
}
window.location.reload()
}
let timer: ReturnType<typeof setInterval> | undefined
const onVisible = () => {
if (document.visibilityState === 'visible') check()
}
onMounted(() => {
check()
timer = setInterval(check, CHECK_EVERY_MS)
document.addEventListener('visibilitychange', onVisible)
})
onBeforeUnmount(() => {
if (timer) clearInterval(timer)
document.removeEventListener('visibilitychange', onVisible)
})
return { available, changes, updating, applyUpdate, check }
}