fix(perf): persist Prisma singleton in production & add timing breakdown

This commit is contained in:
Tien Tran committed 2026-09-28 23:21:39 -07:00
1 parent 8af1952bbf
commit bb135dbc9e
2 files changed
+14 -2

No files matched your search

+13 -1
View File
@@ -6,8 +6,10 @@ import { generateUniqueShortCode } from "@/lib/short-code";
import { generateQRCodeDataUrl } from "@/lib/qr";
export async function POST(req: Request) {
const t0 = performance.now();
try {
const user = await getCurrentUser();
const tAuth = performance.now();
const body = await req.json();
const { url, custom_code, password, tags, expires_at, display_on_portal } = body;
@@ -62,6 +64,7 @@ export async function POST(req: Request) {
// Auto-generate using configured default_url_length
shortCode = await generateUniqueShortCode();
}
const tCodeGen = performance.now();
// Hash password if set
let hashedPassword: string | null = null;
@@ -111,13 +114,15 @@ export async function POST(req: Request) {
tags: true,
},
});
const tDbCreate = performance.now();
// Build absolute short URL
const appUrl = (process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000").replace(/\/$/, "");
const fullShortUrl = `${appUrl}/${shortCode}`;
// Generate QR Code
const qrCodeDataUrl = await generateQRCodeDataUrl(fullShortUrl, { size: 280 });
const qrCodeDataUrl = await generateQRCodeDataUrl(fullShortUrl, { size: 240 });
const tQr = performance.now();
return NextResponse.json({
success: true,
@@ -133,6 +138,13 @@ export async function POST(req: Request) {
display_on_portal: createdUrl.displayOnPortal,
tags: createdUrl.tags.map((t) => t.name),
},
_timings: {
auth_ms: Math.round(tAuth - t0),
code_gen_ms: Math.round(tCodeGen - tAuth),
db_create_ms: Math.round(tDbCreate - tCodeGen),
qr_ms: Math.round(tQr - tDbCreate),
total_server_ms: Math.round(tQr - t0),
},
});
} catch (error) {
console.error("Shorten API error:", error);
+1 -1
View File
@@ -10,4 +10,4 @@ export const prisma =
log: process.env.NODE_ENV === "development" ? ["warn", "error"] : ["error"],
});
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
globalForPrisma.prisma = prisma;