1- import { computed , ref , MaybeRef , unref , watch } from 'vue'
1+ import { computed , MaybeRef , unref , watch , useId } from 'vue'
22import { useI18n } from 'vue-i18n'
33import { LocaleIso } from '~/locales'
44import { Locale , format , formatDistanceToNow } from 'date-fns'
@@ -46,8 +46,9 @@ export const useDateFnLocale = () => {
4646/** SSR-friendly relative date formatting */
4747export const useFormattedDistanceToNow = ( timestamp : MaybeRef < Date | undefined > , { addSuffix } : { addSuffix : MaybeRef < boolean > } ) => {
4848 const { locale } = useDateFnLocale ( )
49+ const id = useId ( )
4950
50- const formatted = ssrRef ( '…' , computed ( ( ) => `timediff-${ unref ( timestamp ) } ` ) )
51+ const formatted = ssrRef ( '…' , `timediff-${ id } ` )
5152
5253 const updateFormatted = ( ) => {
5354 const ts = unref ( timestamp )
@@ -60,7 +61,7 @@ export const useFormattedDistanceToNow = (timestamp: MaybeRef<Date|undefined>, {
6061 } )
6162 }
6263 }
63- watch ( ( ) => [ timestamp , locale ] , updateFormatted , {
64+ watch ( ( ) => [ unref ( timestamp ) , unref ( locale ) ] , updateFormatted , {
6465 immediate : true ,
6566 } )
6667
@@ -71,15 +72,16 @@ export const useFormattedDistanceToNow = (timestamp: MaybeRef<Date|undefined>, {
7172/** SSR-friendly date formatting */
7273export const useFormattedDate = ( timestamp : MaybeRef < Date > , formatStr : MaybeRef < string > ) => {
7374 const { locale } = useDateFnLocale ( )
75+ const id = useId ( )
7476
75- const formatted = ssrRef ( '…' , computed ( ( ) => `timestamp-${ unref ( timestamp ) } - ${ unref ( formatStr ) } ` ) )
77+ const formatted = ssrRef ( '…' , `timestamp-${ id } ` )
7678
7779 const updateFormatted = ( ) => {
7880 formatted . value = format ( unref ( timestamp ) , unref ( formatStr ) , {
7981 locale : locale . value ,
8082 } )
8183 }
82- watch ( ( ) => [ timestamp , formatStr , locale ] , updateFormatted , {
84+ watch ( ( ) => [ unref ( timestamp ) , unref ( formatStr ) , unref ( locale ) ] , updateFormatted , {
8385 immediate : true ,
8486 } )
8587
0 commit comments