
해당 질문에 대한 레퍼런스 링크 URL이 DB에 있다.
컴포넌트가 프롭스로 받은 URL을 통해 API Route에 OG 데이터를 추출요청을 보내고 해당 OG데이터를 통해 북마크를 생성한다.
노션의 북마크 기능을 생각하면 될 듯
src / app / api / og / route.ts
import { NextRequest, NextResponse } from "next/server";
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const url = searchParams.get("url");
if (!url) return NextResponse.json({ error: "URL is required" }, { status: 400 });
try {
const response = await fetch(url);
const html = await response.text();
// OG 데이터를 정규 표현식으로 추출
const ogTitle = html.match(/<meta property="og:title" content="(.*?)"/)?.[1] || "";
const ogImage = html.match(/<meta property="og:image" content="(.*?)"/)?.[1] || "";
const ogDescription = html.match(/<meta property="og:description" content="(.*?)"/)?.[1] || "";
return NextResponse.json({ title: ogTitle, image: ogImage, description: ogDescription });
} catch (error) {
console.error(error);
return NextResponse.json({ error: "Failed to fetch OG data" }, { status: 500 });
}
}
api 라우트를 생성한다.
해당 라우트는 파라미터로 url을 받고 해당 url의 OG 데이터를 반환한다.
const getOgData = async (url: string) => {
const response = await fetch(`/api/og?url=${encodeURIComponent(url)}`);
const data = await response.json();
return data;
};
async function printOgDataTest(url : string) {
const ogData = await getOgData(url);
console.log(ogData);
}
printOgDataTest("https://example.com")
사용 방법은 위와 같다
'프론트엔드 > Next.js' 카테고리의 다른 글
[Next.js] Next15 앱라우터 환경에서 구글 애널리틱스(GA | gtag.js) 적용 방법 (0) | 2025.03.13 |
---|---|
[Next.js] Next15 서버 컴포넌트에서 동적 세그먼트 파라미터 가져오기 | 동적 메타데이터 설정하기 | Next15 파라미터 관련 배포 오류 (0) | 2025.03.05 |
[Next.js] Typescript + Next15 + styled-components + MUI 프로젝트 세팅 A to Z (0) | 2025.02.26 |
[Next.js] Next 15 l SEO 완벽 가이드 A to Z (0) | 2025.02.13 |
[Next.js] Next15 버전 SEO 설정 (0) | 2025.02.13 |

해당 질문에 대한 레퍼런스 링크 URL이 DB에 있다.
컴포넌트가 프롭스로 받은 URL을 통해 API Route에 OG 데이터를 추출요청을 보내고 해당 OG데이터를 통해 북마크를 생성한다.
노션의 북마크 기능을 생각하면 될 듯
src / app / api / og / route.ts
import { NextRequest, NextResponse } from "next/server";
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const url = searchParams.get("url");
if (!url) return NextResponse.json({ error: "URL is required" }, { status: 400 });
try {
const response = await fetch(url);
const html = await response.text();
// OG 데이터를 정규 표현식으로 추출
const ogTitle = html.match(/<meta property="og:title" content="(.*?)"/)?.[1] || "";
const ogImage = html.match(/<meta property="og:image" content="(.*?)"/)?.[1] || "";
const ogDescription = html.match(/<meta property="og:description" content="(.*?)"/)?.[1] || "";
return NextResponse.json({ title: ogTitle, image: ogImage, description: ogDescription });
} catch (error) {
console.error(error);
return NextResponse.json({ error: "Failed to fetch OG data" }, { status: 500 });
}
}
api 라우트를 생성한다.
해당 라우트는 파라미터로 url을 받고 해당 url의 OG 데이터를 반환한다.
const getOgData = async (url: string) => {
const response = await fetch(`/api/og?url=${encodeURIComponent(url)}`);
const data = await response.json();
return data;
};
async function printOgDataTest(url : string) {
const ogData = await getOgData(url);
console.log(ogData);
}
printOgDataTest("https://example.com")
사용 방법은 위와 같다
'프론트엔드 > Next.js' 카테고리의 다른 글
[Next.js] Next15 앱라우터 환경에서 구글 애널리틱스(GA | gtag.js) 적용 방법 (0) | 2025.03.13 |
---|---|
[Next.js] Next15 서버 컴포넌트에서 동적 세그먼트 파라미터 가져오기 | 동적 메타데이터 설정하기 | Next15 파라미터 관련 배포 오류 (0) | 2025.03.05 |
[Next.js] Typescript + Next15 + styled-components + MUI 프로젝트 세팅 A to Z (0) | 2025.02.26 |
[Next.js] Next 15 l SEO 완벽 가이드 A to Z (0) | 2025.02.13 |
[Next.js] Next15 버전 SEO 설정 (0) | 2025.02.13 |