diff --git a/src/app/api/shorten/route.ts b/src/app/api/shorten/route.ts index e16aa8b..358600f 100644 --- a/src/app/api/shorten/route.ts +++ b/src/app/api/shorten/route.ts @@ -4,7 +4,6 @@ import { prisma } from "@/lib/prisma"; import { getCurrentUser } from "@/lib/auth"; import { generateUniqueShortCode } from "@/lib/short-code"; import { generateQRCodeDataUrl } from "@/lib/qr"; -import { fetchUrlTitle } from "@/lib/url-metadata"; export async function POST(req: Request) { try { @@ -113,22 +112,6 @@ export async function POST(req: Request) { }, }); - // Truly non-blocking title scrape in background (does not block HTTP response) - fetchUrlTitle(url.trim()) - .then(async (fetchedTitle) => { - if (fetchedTitle) { - try { - await prisma.shortUrl.update({ - where: { id: createdUrl.id }, - data: { title: fetchedTitle }, - }); - } catch { - // Background task: ignore error - } - } - }) - .catch(() => {}); - // Build absolute short URL const appUrl = (process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000").replace(/\/$/, ""); const fullShortUrl = `${appUrl}/${shortCode}`; diff --git a/src/lib/qr.ts b/src/lib/qr.ts index cadcfd7..54b84e8 100644 --- a/src/lib/qr.ts +++ b/src/lib/qr.ts @@ -22,7 +22,7 @@ export async function generateQRCodeDataUrl(text: string, options: QROptions = { }; return await QRCode.toDataURL(text, { - errorCorrectionLevel: "H", + errorCorrectionLevel: "M", margin, width: size, color: qrColor, diff --git a/src/lib/short-code.ts b/src/lib/short-code.ts index 1a7ebdf..0ce2b02 100644 --- a/src/lib/short-code.ts +++ b/src/lib/short-code.ts @@ -2,11 +2,18 @@ import { prisma } from "./prisma"; const CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; +let cachedLength: { value: number; timestamp: number } | null = null; + /** * Get the configured default short code length from admin settings. * Defaults to 5 if not configured. */ export async function getDefaultUrlLength(): Promise { + const now = Date.now(); + if (cachedLength && now - cachedLength.timestamp < 60000) { + return cachedLength.value; + } + try { const setting = await prisma.setting.findUnique({ where: { key: "default_url_length" }, @@ -14,12 +21,15 @@ export async function getDefaultUrlLength(): Promise { if (setting && setting.value) { const len = parseInt(setting.value, 10); if (!isNaN(len) && len >= 3 && len <= 16) { + cachedLength = { value: len, timestamp: now }; return len; } } } catch (error) { console.error("Error reading default_url_length:", error); } + + cachedLength = { value: 5, timestamp: now }; return 5; }