fix: deduplicate share target triggers across client and server

This commit is contained in:
chrisf committed 2026-10-22 21:02:43 -06:00
1 parent a99e05c53c
commit efb3ecf1ae
3 files changed
+74

No files matched your search

+28
View File
@@ -164,6 +164,34 @@ export async function POST(req: NextRequest) {
}
}
// Deduplication check: if a note with the exact same sourceUrl was created within the last 5 minutes, reuse it!
if (url) {
const recentDup = await prisma.note.findFirst({
where: {
userId,
sourceUrl: url,
createdAt: {
gte: new Date(Date.now() - 5 * 60 * 1000), // last 5 minutes
},
},
orderBy: { createdAt: 'desc' },
});
if (recentDup) {
const wantsJson =
contentType.includes('application/json') ||
Boolean(req.headers.get('accept')?.includes('application/json'));
if (isShareTarget && !wantsJson) {
return NextResponse.redirect(getRedirectUrl(req, recentDup.id), 303);
}
return NextResponse.json(
{ ok: true, noteId: recentDup.id, title: recentDup.title, mode: 'existing-recent' },
{ status: 200, headers: corsHeaders }
);
}
}
const notebookId = notebookName.trim()
? await getOrCreateNotebookByName(notebookName.trim(), userId)
: (await ensureDefaultNotebook(userId)).id;
+32
View File
@@ -86,6 +86,22 @@ export async function GET(req: NextRequest) {
}
}
if (url) {
const recentDup = await prisma.note.findFirst({
where: {
userId,
sourceUrl: url,
createdAt: {
gte: new Date(Date.now() - 5 * 60 * 1000),
},
},
orderBy: { createdAt: 'desc' },
});
if (recentDup) {
return NextResponse.redirect(getRedirectUrl(req, recentDup.id), 303);
}
}
let noteId: string;
if (url && !text) {
// Direct web clip
@@ -205,6 +221,22 @@ export async function POST(req: NextRequest) {
}
}
if (url) {
const recentDup = await prisma.note.findFirst({
where: {
userId,
sourceUrl: url,
createdAt: {
gte: new Date(Date.now() - 5 * 60 * 1000),
},
},
orderBy: { createdAt: 'desc' },
});
if (recentDup) {
return NextResponse.redirect(getRedirectUrl(req, recentDup.id), 303);
}
}
const wantsJson =
contentType.includes('application/json') ||
Boolean(req.headers.get('accept')?.includes('application/json'));