Deno Fresh is a full-stack framework for Deno that renders pages on the edge with minimal JavaScript sent to the browser. UUID v7 integrates in server-side handlers using Deno’s native crypto support — no polyfills needed on the server. See UUID v7 in Deno for the Deno runtime context.
Install uuid package
// Using Deno's npm compatibility (deno.json or import map)
import { v7 as uuidv7 } from "npm:uuid";
// Or with jsr (Deno's native registry)
import * as uuid from "jsr:@std/uuid";
Fresh API route — POST handler
// routes/api/items.ts
import { Handlers } from "$fresh/server.ts";
import { v7 as uuidv7 } from "npm:uuid";
interface Item {
id: string;
name: string;
owner: string;
}
export const handler: Handlers = {
async POST(req, _ctx) {
const body = await req.json();
// Server-generated UUID v7 — encodes creation time
const id: string = body.id ?? uuidv7();
const item: Item = { id, name: body.name, owner: body.owner };
// Persist to Deno KV
const kv = await Deno.openKv();
await kv.set(["items", id], item);
return new Response(JSON.stringify(item), {
status: 201,
headers: { "Content-Type": "application/json" },
});
},
async GET(req, _ctx) {
const url = new URL(req.url);
const cursor = url.searchParams.get("cursor") ?? undefined;
const limit = parseInt(url.searchParams.get("limit") ?? "20");
const kv = await Deno.openKv();
const items: Item[] = [];
// UUID v7 key order = chronological — cursor pagination
const startKey = cursor ? ["items", cursor] : ["items"];
const iter = kv.list<Item>({ prefix: ["items"], start: startKey }, { limit: limit + 1 });
for await (const entry of iter) {
if (cursor && entry.key[1] === cursor) continue; // skip cursor itself
items.push(entry.value);
}
const hasMore = items.length > limit;
const data = hasMore ? items.slice(0, limit) : items;
const nextCursor = hasMore ? data.at(-1)!.id : null;
return Response.json({ data, nextCursor });
},
};
Fresh page route with UUID v7 data
// routes/items/[id].tsx
import { Handlers, PageProps } from "$fresh/server.ts";
interface ItemData {
item: { id: string; name: string } | null;
createdAt: string;
}
export const handler: Handlers<ItemData> = {
async GET(_req, ctx) {
const id = ctx.params.id;
const kv = await Deno.openKv();
const entry = await kv.get<{ id: string; name: string }>(["items", id]);
// Extract creation time from UUID v7 — no DB timestamp column needed
const hex = id.replace(/-/g, "").slice(0, 12);
const createdMs = parseInt(hex, 16);
const createdAt = new Date(createdMs).toISOString();
return ctx.render({ item: entry.value, createdAt });
},
};
export default function ItemPage({ data }: PageProps<ItemData>) {
if (!data.item) return <div>Not found</div>;
return (
<div>
<h1>{data.item.name}</h1>
<p>Created: {data.createdAt}</p>
<p>ID: <code>{data.item.id}</code></p>
</div>
);
}
Island with client-side UUID v7 (optimistic)
// islands/AddItem.tsx
import { useState } from "preact/hooks";
import { v7 as uuidv7 } from "npm:uuid";
export default function AddItem() {
const [items, setItems] = useState<{ id: string; name: string }[]>([]);
async function addItem(name: string) {
const id = uuidv7(); // client-generated UUID v7 for optimistic UI
// Optimistic: add to local state immediately
setItems((prev) => [...prev, { id, name }]);
// Confirm with server
await fetch("/api/items", {
method: "POST",
body: JSON.stringify({ id, name }),
});
}
return (
<div>
<button onClick={() => addItem("New Item")}>Add</button>
<ul>
{/* UUID v7 :key — time-ordered, stable */}
{items.map((item) => <li key={item.id}>{item.name}</li>)}
</ul>
</div>
);
}
Further reading
- UUID v7 in Deno — Standard Library and Fresh
- UUID v7 in Deno KV
- UUID v7 in SvelteKit
- UUID v7 for Cursor Pagination
- UUID in JavaScript
External references
Frequently asked questions
How do I generate UUID v7 in Deno Fresh?
The Deno standard library provides crypto.randomUUID() for UUID v4. For UUID v7, use the jsr:@std/uuid package: import { v7 } from "jsr:@std/uuid"; const id = v7.generate();. Alternatively, install the npm uuid package via Deno's npm compatibility: import { v7 as uuidv7 } from "npm:uuid";. See UUID v7 in Deno.
How does UUID v7 work with Deno KV in Fresh?
Use UUID v7 as the Deno KV key segment: kv.set(["items", uuidv7()], item). Deno KV keys are lexicographically ordered, so listing with kv.list({ prefix: ["items"], start: ["items", cursor] }) returns items in chronological order — UUID v7 timestamp prefix ensures this without a separate sort key. See UUID v7 in Deno KV.
Should I generate UUID v7 in Fresh islands or route handlers?
Generate UUID v7 in server-side route handlers (routes/api/) or in the handler exported from page routes. Islands run in the browser where crypto.randomUUID() is available (UUID v4 only). For UUID v7 in islands, use import { v7 as uuidv7 } from "npm:uuid" — the uuid npm package works in Deno's browser compatibility mode. See UUID v7 in SvelteKit for a comparable edge-first framework pattern.