Vue 3 and Nuxt 3 have the same hydration concern as React and Next.js: UUID generation in component setup() runs on both the server and client, producing different values for the same element. The solution is to generate UUIDs server-side and pass them to the component as props or via useState(). The uuid npm package works in both Node.js (Nuxt server) and the browser without bundler configuration. See UUID in JavaScript for the full runtime context.
Vue 3 composable
// composables/useUuid.ts
import { v7 as uuidv7 } from 'uuid';
export function useUuid() {
return {
/** Time-sortable UUID v7 — use for new entity IDs */
generate: (): string => uuidv7(),
/** UUID v4 via browser CSPRNG — no dependency needed */
generateV4: (): string => crypto.randomUUID(),
};
}
<!-- components/OrderForm.vue -->
<script setup lang="ts">
import { useUuid } from '~/composables/useUuid';
const { generate } = useUuid();
// Only generate client-side ephemeral IDs here
// Server-persisted IDs must come from the server response
const localDraftId = generate();
</script>
See TypeScript UUID Types for a branded type version of the composable.
Nuxt server route — safe UUID generation
// server/api/orders.post.ts
import { v7 as uuidv7 } from 'uuid';
import { defineEventHandler, readBody } from 'h3';
export default defineEventHandler(async (event) => {
const body = await readBody(event);
const id = uuidv7(); // server-side only — no hydration risk
await db.query(
'INSERT INTO orders (id, status) VALUES ($1, $2)',
[id, body.status]
);
return { id };
});
Nuxt server routes run in Node.js — uuidv7() is called once per request, server-side only. The returned id is safe to render in the subsequent page without mismatch.
Pinia store with UUID v7
// stores/orders.ts
import { defineStore } from 'pinia';
import { v7 as uuidv7 } from 'uuid';
interface Order { id: string; status: string; pending?: boolean; }
export const useOrderStore = defineStore('orders', {
state: () => ({ items: [] as Order[] }),
actions: {
addOptimistic(status: string): string {
const tempId = uuidv7();
this.items.push({ id: tempId, status, pending: true });
return tempId;
},
confirm(tempId: string, serverId: string) {
const idx = this.items.findIndex(o => o.id === tempId);
if (idx !== -1) this.items[idx] = { ...this.items[idx], id: serverId, pending: false };
},
},
});
UUID v7 is preferred over v4 for tempId because the timestamp prefix means optimistic items sort in insertion order inside the Pinia store — [...store.items].sort((a, b) => a.id.localeCompare(b.id)) returns chronological order. See UUID v4 vs v7.
Nuxt useState — hydration-safe server UUID
// pages/orders/[id].vue
<script setup lang="ts">
const route = useRoute();
// useState initialises from server-rendered HTML on the client — no mismatch
const orderId = useState('orderId', () => route.params.id as string);
</script>
useState() serialises the initial value into the SSR HTML payload; the client rehydrates from that value rather than re-running the initialiser. This pattern is safe for any UUID value passed as a route param.
Further reading
- UUID in JavaScript — full guide
- UUID in React and Next.js
- UUID in Svelte and SvelteKit
- TypeScript UUID Types — branded types
- crypto.randomUUID() browser support
- UUID Testing Strategies
External references
Frequently asked questions
How do I generate a UUID in a Vue 3 composable?
Create a composable that wraps the UUID factory: export function useUuid() { return { generate: () => uuidv7(), generateV4: () => crypto.randomUUID() }; }. Using a composable rather than calling uuidv7() directly in components makes the factory injectable and testable via vi.mock. See UUID Testing Strategies.
Does Nuxt 3 have a hydration mismatch problem with UUIDs?
Yes — if you call uuidv7() in a component's setup() during SSR, the server and client generate different UUIDs for the same rendered element, causing a hydration mismatch warning. Generate UUIDs server-side in Nuxt server routes (server/api/) or use useState() with a server-provided initial value so the same UUID is serialised into the HTML and reused on the client. See UUID in React and Next.js for the equivalent Next.js pattern.