feat: non-blocking background URL title fetching via Next.js after()
This commit is contained in:
1 parent
f826efdf00
commit
8a9aae2050
1 file changed
+18
-1
@@ -1,9 +1,10 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { NextResponse, after } from "next/server";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { generateRandomCode, getDefaultUrlLength } from "@/lib/short-code";
|
||||
import { generateQRCodeDataUrl } from "@/lib/qr";
|
||||
import { fetchUrlTitle } from "@/lib/url-metadata";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
@@ -140,6 +141,22 @@ export async function POST(req: Request) {
|
||||
// Generate QR Code
|
||||
const qrCodeDataUrl = await generateQRCodeDataUrl(fullShortUrl, { size: 240 });
|
||||
|
||||
// Non-blocking background task via Next.js after()
|
||||
// Response is returned immediately to the client; title is fetched & updated afterward.
|
||||
after(async () => {
|
||||
try {
|
||||
const pageTitle = await fetchUrlTitle(url.trim());
|
||||
if (pageTitle) {
|
||||
await prisma.shortUrl.update({
|
||||
where: { id: createdUrl.id },
|
||||
data: { title: pageTitle },
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Silently ignore background scraping errors
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
|
||||
Reference in new issue
Block a user