-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: @vercel/og 설치 * chore: default og image 추가 * feat: default og image 적용 * feat: 공유 페이지에서 동적인 og 이미지 생성 * ci/cd: 빌드 시 추가된 환경변수 적용
- Loading branch information
Showing
8 changed files
with
308 additions
and
4 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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
NEXT_PUBLIC_DOMAIN='localhost를 실행할 도메인' | ||
NEXT_PUBLIC_API='API 주소 요청 주소' | ||
NEXT_PUBLIC_REDIRECT_URL=로그인 이후, 리다이렉트할 URL | ||
NEXT_PUBLIC_MIXPANEL_TOKEN=믹스패널에 사용하는 토큰 | ||
NEXT_PUBLIC_MIXPANEL_TOKEN=믹스패널에 사용하는 토큰 | ||
NEXT_PUBLIC_IMAGE_URL=OG IMAGE URL을 가져오는 주소. |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* eslint-disable jsx-a11y/alt-text */ | ||
import { NextRequest } from 'next/server'; | ||
import { ImageResponse } from '@vercel/og'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
const styles = { | ||
1: { width: '1200px', height: '630px' }, | ||
2: { width: '600px', height: '630px' }, | ||
4: { width: '600px', height: '315px' }, | ||
8: { width: '300px', height: '315px' }, | ||
16: { width: '300px', height: '157.5px' }, | ||
}; | ||
|
||
const getImageStyle = (index: number) => { | ||
return styles[index as keyof typeof styles] || styles[16]; | ||
}; | ||
|
||
export async function GET(req: NextRequest) { | ||
const { searchParams } = new URL(req.url); | ||
const imageUrls: string[] = JSON.parse(searchParams.get('imageUrls') || '[]'); | ||
|
||
const width = 1200; | ||
const height = 630; | ||
|
||
// 이미지 개수를 1, 2, 4, 8, 16 중 가장 가까운 수로 조정 | ||
const adjustedCount = [1, 2, 4, 8, 16].reduce((prev, curr) => | ||
Math.abs(curr - imageUrls.length) < Math.abs(prev - imageUrls.length) | ||
? curr | ||
: prev | ||
); | ||
|
||
const images = await Promise.all( | ||
imageUrls.slice(0, adjustedCount).map(async (url: string) => { | ||
try { | ||
const res = await fetch(url); | ||
if (!res.ok) throw new Error('Failed to fetch image'); | ||
return url; | ||
} catch { | ||
return '/image/og_image.png'; | ||
} | ||
}) | ||
); | ||
const imageCount = images.length; | ||
|
||
return new ImageResponse( | ||
( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexWrap: 'wrap', | ||
width: '1200px', | ||
height: '630px', | ||
}} | ||
> | ||
{images.map((url: string, index: number) => ( | ||
<img | ||
key={index} | ||
src={url} | ||
style={{ | ||
...getImageStyle(imageCount), | ||
objectFit: 'cover', | ||
}} | ||
/> | ||
))} | ||
</div> | ||
), | ||
{ width, height } | ||
); | ||
} |
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
Oops, something went wrong.