Skip to content

Commit 6d93d37

Browse files
committed
web: Implement SSR for formatted dates
1 parent bacf47e commit 6d93d37

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

web/composables/date-fns.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, ref, MaybeRef, unref, watch, onMounted } from 'vue'
1+
import { computed, ref, MaybeRef, unref, watch } from 'vue'
22
import { useI18n } from 'vue-i18n'
33
import { LocaleIso } from '~/locales'
44
import { Locale, format, formatDistanceToNow } from 'date-fns'
@@ -59,8 +59,9 @@ export const useFormattedDistanceToNow = (timestamp: MaybeRef<Date|undefined>, {
5959
})
6060
}
6161
}
62-
watch(() => unref(timestamp), updateFormatted)
63-
onMounted(updateFormatted)
62+
watch(() => [timestamp, locale], updateFormatted, {
63+
immediate: true,
64+
})
6465

6566
return formatted
6667
}
@@ -77,8 +78,9 @@ export const useFormattedDate = (timestamp: MaybeRef<Date>, formatStr: MaybeRef<
7778
locale: locale.value,
7879
})
7980
}
80-
watch(() => unref(timestamp), updateFormatted)
81-
onMounted(updateFormatted)
81+
watch(() => [timestamp, locale], updateFormatted, {
82+
immediate: true,
83+
})
8284

8385
return formatted
8486
}

0 commit comments

Comments
 (0)