-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
172 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as cheerio from 'cheerio'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
interface MetaData { | ||
title: string; | ||
description: string; | ||
image: string; | ||
favicon: string; | ||
domain: string; | ||
} | ||
|
||
async function getMetadata(url: string): Promise<MetaData> { | ||
try { | ||
const response = await fetch(url); | ||
const html = await response.text(); | ||
const $ = cheerio.load(html); | ||
const domain = new URL(url).hostname; | ||
|
||
return { | ||
title: $('meta[property="og:title"]').attr('content') || $('title').text() || domain, | ||
description: | ||
$('meta[property="og:description"]').attr('content') || | ||
$('meta[name="description"]').attr('content') || | ||
'', | ||
image: $('meta[property="og:image"]').attr('content') || '', | ||
favicon: | ||
$('link[rel="icon"]').attr('href') || | ||
$('link[rel="shortcut icon"]').attr('href') || | ||
`https://${domain}/favicon.ico`, | ||
domain, | ||
}; | ||
} catch (error) { | ||
return { | ||
title: url, | ||
description: '', | ||
image: '', | ||
favicon: '', | ||
domain: new URL(url).hostname, | ||
}; | ||
} | ||
} | ||
|
||
export async function GET(request: Request) { | ||
const { searchParams } = new URL(request.url); | ||
const url = searchParams.get('url'); | ||
|
||
if (!url) { | ||
return NextResponse.json({ error: 'URL is required' }, { status: 400 }); | ||
} | ||
|
||
const metadata = await getMetadata(url); | ||
return NextResponse.json(metadata); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters