Remix is a full-stack React framework where data mutations happen in server-side action functions and data fetching happens in loader functions. Because Remix actions run on the server, UUID v7 generation happens naturally in Node.js (or Deno/Bun) — avoiding the hydration mismatch that affects client-side UUID generation in frameworks like Next.js. See UUID in React and Next.js for the comparison.
Setup
npx create-remix@latest
npm install uuid
npm install --save-dev @types/uuid
Server action with UUID v7
// app/routes/orders.new.tsx
import { json, redirect } from '@remix-run/node';
import type { ActionFunctionArgs } from '@remix-run/node';
import { v7 as uuidv7 } from 'uuid';
import { db } from '~/db.server';
export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData();
const customerId = formData.get('customerId') as string;
const total = Number(formData.get('total'));
if (!customerId || isNaN(total)) {
return json({ error: 'Invalid input' }, { status: 400 });
}
// UUID v7 generated server-side — no hydration mismatch risk
const order = await db.order.create({
data: {
id: uuidv7(),
customerId,
total,
},
});
return redirect(`/orders/${order.id}`);
}
No hydration mismatch because the UUID is generated in the server action, never in a React component. See UUID in Prisma for the schema setup.
Loader with UUID v7 cursor pagination
// app/routes/orders.tsx
import { json } from '@remix-run/node';
import type { LoaderFunctionArgs } from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import { db } from '~/db.server';
export async function loader({ request }: LoaderFunctionArgs) {
const url = new URL(request.url);
const cursor = url.searchParams.get('cursor') ?? undefined;
const limit = 20;
const orders = await db.order.findMany({
where: cursor ? { id: { gt: cursor } } : undefined,
orderBy: { id: 'asc' },
take: limit,
});
const nextCursor = orders.length === limit ? orders[orders.length - 1].id : null;
return json({ orders, nextCursor });
}
export default function OrdersPage() {
const { orders, nextCursor } = useLoaderData<typeof loader>();
return (
<div>
{orders.map(order => (
<div key={order.id}>
<a href={`/orders/${order.id}`}>{order.id}</a>
</div>
))}
{nextCursor && (
<a href={`/orders?cursor=${nextCursor}`}>Load more</a>
)}
</div>
);
}
UUID v7 cursors are stable across page reloads — the cursor encodes creation time and is safe to include in URLs. See UUID v7 for Cursor Pagination.
Optimistic UI with client-side UUID
// app/routes/todos.tsx
import { useFetcher } from '@remix-run/react';
import { v7 as uuidv7 } from 'uuid';
export default function TodosPage() {
const fetcher = useFetcher();
// Generate UUID v7 client-side for optimistic ID
// Server action may use this ID or generate its own
function handleAdd(text: string) {
const optimisticId = uuidv7();
fetcher.submit(
{ id: optimisticId, text },
{ method: 'post', action: '/todos' }
);
}
return (
<button onClick={() => handleAdd('New todo')}>Add todo</button>
);
}
// app/routes/todos.tsx action
export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData();
// Accept client ID or generate server-side
const id = (formData.get('id') as string) || uuidv7();
const text = formData.get('text') as string;
await db.todo.upsert({
where: { id },
create: { id, text },
update: { text },
});
return json({ id });
}
Drizzle ORM with Remix
// app/db.server.ts
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
export const db = drizzle(postgres(process.env.DATABASE_URL!));
// app/schema.ts
import { pgTable, uuid, text, numeric } from 'drizzle-orm/pg-core';
import { sql } from 'drizzle-orm';
export const orders = pgTable('orders', {
id: uuid('id').primaryKey().default(sql`uuidv7()`),
customer: text('customer').notNull(),
total: numeric('total').notNull(),
});
See UUID in Drizzle ORM for the full migration setup.
Further reading
- UUID in React and Next.js — hydration patterns
- UUID in Prisma — v7 Setup and Migration
- UUID in Drizzle ORM — v7 Setup
- UUID v7 for Cursor Pagination
- UUID in JavaScript — full guide
External references
Frequently asked questions
Should I generate UUID v7 in a Remix action or loader?
Generate UUID v7 in actions (mutations), not loaders (reads). Actions run on the server, so v7() from the uuid npm package runs in the Node.js/Deno/Bun server environment where crypto.getRandomValues() is available. Never generate IDs in loaders — loaders are for fetching data.
Can I use UUID v7 for optimistic UI in Remix?
Yes. The client can generate a UUID v7 using crypto.randomUUID() (UUID v4 only in browsers) or import the uuid package. For optimistic UI, generate the ID client-side in the form submission, use it for the optimistic entry, and submit it as a hidden field. The server can use the submitted ID or override it with a server-generated UUID v7. See UUID in React and Next.js for the hydration pitfalls.
How do I use UUID v7 with Remix and Prisma?
In your Prisma schema set id String @id @default(dbgenerated("uuidv7()")) @db.Uuid and enable the pg_uuidv7 extension. Alternatively, generate UUID v7 in the Remix action with v7() and pass it to prisma.model.create({ data: { id: uuidv7(), ... } }). See UUID in Prisma.