perf: finalize ultra-fast shorten API with optimistic insert and clean response
This commit is contained in:
1 parent
fec54aab1f
commit
f826efdf00
1 file changed
-11
@@ -6,10 +6,8 @@ import { generateRandomCode, getDefaultUrlLength } from "@/lib/short-code";
|
|||||||
import { generateQRCodeDataUrl } from "@/lib/qr";
|
import { generateQRCodeDataUrl } from "@/lib/qr";
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
const t0 = performance.now();
|
|
||||||
try {
|
try {
|
||||||
const user = await getCurrentUser();
|
const user = await getCurrentUser();
|
||||||
const tAuth = performance.now();
|
|
||||||
const body = await req.json();
|
const body = await req.json();
|
||||||
const { url, custom_code, password, tags, expires_at, display_on_portal } = body;
|
const { url, custom_code, password, tags, expires_at, display_on_portal } = body;
|
||||||
|
|
||||||
@@ -135,15 +133,12 @@ export async function POST(req: Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const tDb = performance.now();
|
|
||||||
|
|
||||||
// Build absolute short URL
|
// Build absolute short URL
|
||||||
const appUrl = (process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000").replace(/\/$/, "");
|
const appUrl = (process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000").replace(/\/$/, "");
|
||||||
const fullShortUrl = `${appUrl}/${finalShortCode}`;
|
const fullShortUrl = `${appUrl}/${finalShortCode}`;
|
||||||
|
|
||||||
// Generate QR Code
|
// Generate QR Code
|
||||||
const qrCodeDataUrl = await generateQRCodeDataUrl(fullShortUrl, { size: 240 });
|
const qrCodeDataUrl = await generateQRCodeDataUrl(fullShortUrl, { size: 240 });
|
||||||
const tQr = performance.now();
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -159,12 +154,6 @@ export async function POST(req: Request) {
|
|||||||
display_on_portal: createdUrl.displayOnPortal,
|
display_on_portal: createdUrl.displayOnPortal,
|
||||||
tags: createdUrl.tags ? createdUrl.tags.map((t: any) => t.name) : [],
|
tags: createdUrl.tags ? createdUrl.tags.map((t: any) => t.name) : [],
|
||||||
},
|
},
|
||||||
_timings: {
|
|
||||||
auth_ms: Math.round(tAuth - t0),
|
|
||||||
db_insert_ms: Math.round(tDb - tAuth),
|
|
||||||
qr_ms: Math.round(tQr - tDb),
|
|
||||||
total_server_ms: Math.round(tQr - t0),
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Shorten API error:", error);
|
console.error("Shorten API error:", error);
|
||||||
|
|||||||
Reference in new issue
Block a user