-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@vercel/og cover image generation for blog posts and home
- Loading branch information
1 parent
8947a5b
commit e6d15c9
Showing
8 changed files
with
294 additions
and
2 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,28 @@ | ||
let baseUrl = 'http:/localhost:8000'; | ||
if (process.env.NEXT_PUBLIC_VERCEL_ENV === 'production') { | ||
baseUrl = 'https://keystonejs.com'; | ||
} else if (process.env.NEXT_PUBLIC_VERCEL_URL) { | ||
baseUrl = `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`; | ||
} | ||
|
||
export const getOgAbsoluteUrl = ({ | ||
title, | ||
description, | ||
type, | ||
}: { | ||
title: string; | ||
description?: string; | ||
type?: string; | ||
}) => { | ||
const ogUrl = new URL(`${baseUrl}/api/hero-image`); | ||
|
||
ogUrl.searchParams.append('title', title); | ||
if (typeof description === 'string') { | ||
ogUrl.searchParams.append('description', description); | ||
} | ||
if (typeof type === 'string') { | ||
ogUrl.searchParams.append('type', type); | ||
} | ||
|
||
return ogUrl.href; | ||
}; |
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,149 @@ | ||
import React from 'react'; | ||
import { ImageResponse } from '@vercel/og'; | ||
import type { NextRequest } from 'next/server'; | ||
|
||
export const config = { | ||
runtime: 'experimental-edge', | ||
}; | ||
|
||
const HeroImage = ({ | ||
title, | ||
description, | ||
type, | ||
}: { | ||
title: string; | ||
description?: string; | ||
type?: string; | ||
}) => { | ||
let titleFontSize = 96; | ||
if (title.length > 20) { | ||
titleFontSize = 80; | ||
} else if (title.length > 30) { | ||
titleFontSize = 72; | ||
} else if (title.length > 100) { | ||
titleFontSize = 60; | ||
} | ||
|
||
const shortenedDescription = | ||
typeof description === 'string' && description?.length > 110 | ||
? description?.substring(0, 110) + '...' | ||
: description || ''; | ||
return ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
backgroundColor: 'white', | ||
backgroundImage: 'linear-gradient(135deg, #1476FF, #00ABDA)', | ||
height: '100%', | ||
width: '100%', | ||
}} | ||
> | ||
<div | ||
style={{ | ||
position: 'relative', | ||
padding: '40px 80px', | ||
height: '100%', | ||
width: '100%', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
flexDirection: 'column', | ||
}} | ||
> | ||
<div | ||
style={{ | ||
letterSpacing: -2, | ||
color: '#FFF', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
flexDirection: 'column', | ||
}} | ||
> | ||
{type ? ( | ||
<div | ||
style={{ | ||
fontSize: 32, | ||
paddingBottom: 8, | ||
alignSelf: 'flex-start', | ||
}} | ||
> | ||
{type} | ||
</div> | ||
) : null} | ||
<div | ||
style={{ | ||
fontSize: titleFontSize, | ||
fontWeight: 700, | ||
paddingTop: 16, | ||
paddingBottom: 64, | ||
borderTop: type ? '1px solid white' : '1px solid transparent', | ||
}} | ||
> | ||
{title} | ||
</div> | ||
{shortenedDescription ? ( | ||
<div style={{ fontSize: 40, fontWeight: 400, lineHeight: 1.4 }}> | ||
{shortenedDescription} | ||
</div> | ||
) : null} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
// Make sure the font exists in the specified path: | ||
const interBold = fetch(new URL('../../public/font/Inter-Bold.ttf', import.meta.url)).then(res => | ||
res.arrayBuffer() | ||
); | ||
|
||
const interRegular = fetch(new URL('../../public/font/Inter-Regular.ttf', import.meta.url)).then( | ||
res => res.arrayBuffer() | ||
); | ||
|
||
// vercel API route that generates the OG image | ||
export default async function handler(req: NextRequest) { | ||
const interBoldData = await interBold; | ||
const interRegularData = await interRegular; | ||
|
||
try { | ||
const { searchParams } = new URL(req.url); | ||
const title = searchParams.has('title') ? searchParams.get('title') || '' : ''; | ||
const description = searchParams.get('description') || undefined; | ||
const type = searchParams.get('type') || undefined; | ||
|
||
if (title?.length > 100 || (typeof description === 'string' && description?.length > 300)) { | ||
return new Response( | ||
JSON.stringify({ | ||
code: 'INVALID_PARAMS', | ||
message: 'Param title/description too long', | ||
}), | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
return new ImageResponse(<HeroImage title={title} description={description} type={type} />, { | ||
width: 1200, | ||
height: 630, | ||
emoji: 'twemoji', | ||
fonts: [ | ||
{ | ||
name: 'Inter', | ||
data: interBoldData, | ||
style: 'normal', | ||
weight: 700, | ||
}, | ||
{ | ||
name: 'Inter', | ||
data: interRegularData, | ||
style: 'normal', | ||
weight: 400, | ||
}, | ||
], | ||
}); | ||
} catch (e: any) { | ||
return new Response(`Failed to generate hero image`, { | ||
status: 500, | ||
}); | ||
} | ||
} |
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
Binary file not shown.
Binary file not shown.
Oops, something went wrong.