perf: completely remove blocking fetchUrlTitle & cache settings for instant shorten (<100ms)
This commit is contained in:
1 parent
41786f2f51
commit
8af1952bbf
3 files changed
+11
-18
No files matched your search
@@ -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}`;
|
||||
|
||||
+1
-1
@@ -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,
|
||||
|
||||
@@ -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<number> {
|
||||
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<number> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user