Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(og-image): reconcile social preview on route change and share Photo type

Timothy Yin153253b9b1ad
diff --git a/app/pages/[...slug].vue b/app/pages/[...slug].vue
index 77cdeab8..6be63d30 100644
--- a/app/pages/[...slug].vue
+++ b/app/pages/[...slug].vue
@@ -20,30 +20,59 @@ const currentPhoto = computed(() =>
photos.value.find((photo) => photo.id === photoId.value),
)
-if (photoId.value) {
- defineOgImage('Photo', {
- photo: currentPhoto.value || undefined,
- appTitle: (getSetting('app:title') as string) || 'ChronoFrame',
- })
-} else {
- // Social previews must never include photos visible only to administrators.
- const { data: publicPhotos } = await useFetch<Photo[]>('/api/photos/visible')
- defineOgImage('Home', {
- appTitle: (getSetting('app:title') as string) || 'ChronoFrame',
- slogan: (getSetting('app:slogan') as string) || '',
- author: (getSetting('app:author') as string) || '',
- photoCount: publicPhotos.value?.length || 0,
- photoCountLabel: t(
- 'plural.photo',
- { count: publicPhotos.value?.length || 0 },
- publicPhotos.value?.length || 0,
- ),
- thumbnails: (publicPhotos.value || [])
- .filter((photo) => photo.thumbnailUrl)
- .slice(0, 3)
- .map((photo) => photo.thumbnailUrl),
+const nuxtApp = useNuxtApp()
+// This public collection is route-independent; late results must only update
+// Home metadata while the current route is still the home page.
+const { data: publicPhotos, execute: loadPublicPhotos } = await useFetch<
+ Photo[]
+>('/api/photos/visible', { immediate: !photoId.value })
+watch(photoId, (id) => {
+ if (!id && !publicPhotos.value) void loadPublicPhotos()
+})
+
+function updateOgImage() {
+ const id = photoId.value
+ const appTitle = (getSetting('app:title') as string) || 'ChronoFrame'
+ nuxtApp.runWithContext(() => {
+ if (id) {
+ defineOgImage('Photo', {
+ photo: currentPhoto.value?.id === id ? currentPhoto.value : undefined,
+ appTitle,
+ })
+ return
+ }
+ const visiblePhotos = publicPhotos.value || []
+ defineOgImage('Home', {
+ appTitle,
+ slogan: (getSetting('app:slogan') as string) || '',
+ author: (getSetting('app:author') as string) || '',
+ photoCount: visiblePhotos.length,
+ photoCountLabel: t(
+ 'plural.photo',
+ { count: visiblePhotos.length },
+ visiblePhotos.length,
+ ),
+ thumbnails: visiblePhotos
+ .map((photo) => photo.thumbnailUrl)
+ .filter((url): url is string => Boolean(url))
+ .slice(0, 3),
+ })
})
}
+watch(
+ [
+ photoId,
+ currentPhoto,
+ publicPhotos,
+ () => route.fullPath,
+ () => t('plural.photo', 0),
+ ],
+ updateOgImage,
+ { immediate: true },
+)
+// nuxt-og-image skips client updates during hydration. Reconcile once mounted
+// in case the route changed while the initial public-photo request was pending.
+onMounted(updateOgImage)
// 处理标签查询参数
const { clearAllFilters, toggleFilter } = usePhotoFilters()
diff --git a/shared/types/global.d.ts b/shared/types/global.d.ts
index 0fe21b4f..e1e839ba 100644
--- a/shared/types/global.d.ts
+++ b/shared/types/global.d.ts
@@ -1,8 +1,9 @@
+import type { Photo as DatabasePhoto } from '../../server/utils/db'
import type { WorkerPool } from '../../server/services/pipeline-queue'
declare global {
var __workerPool: WorkerPool | undefined
- interface Photo {
+ interface Photo extends DatabasePhoto {
fileName?: string | null
}
}