SSR-safe time element for Nuxt 3
- ✨ SSR/SSG-safe rendering of any date/time
- 💪 Prevents hydration mismatch on client
- 🏁 Respects browser locale
- ✅ Renders semantic
<time>element
Install and add nuxt-time to your nuxt.config.
# Whichever matches your package manager
pnpm add -D nuxt-time
npm install -D nuxt-time
yarn add -D nuxt-timeexport default defineNuxtConfig({
modules: ['nuxt-time'],
})To use, you can use the <NuxtTime> component anywhere in your app. It will render a spec-compliant <time> element.
It accepts datetime and locale (optional) properties, along with any property accepted by Intl.DateTimeFormat (see MDN reference). Note: you can provide a Date or number (e.g. Date.now()) via datetime and it will be rendered correctly as an ISO-formatted date.
<template>
<!--
Date.now() will safely respect the time on the server, not on the
client. What's more, there will be no flash between server
and client locale formatting.
-->
<NuxtTime :datetime="Date.now()" second="numeric" month="long" day="numeric" />
</template>- Clone this repository
- Enable Corepack using
corepack enable(usenpm i -g corepackfor Node.js < 16.10) - Install dependencies using
pnpm install - Stub module with
pnpm dev:prepare - Run
pnpm devto start playground in development mode
Made with ❤️
Published under the MIT License.