Skip to content

Commit 5f21036

Browse files
committed
fix: og
1 parent 09f2927 commit 5f21036

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

apps/web/app/api/og/route.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import { OpenGraph } from '@/registry/ui/open-graph'
66

77
export const revalidate = false
88

9-
export async function GET(req: Request) {
9+
export async function GET(req: Request, _: RouteContext<'/api/og'>) {
1010
const url = new URL(req.url)
1111

12-
const { searchParams } = url
13-
const title = searchParams.get('title') ?? 'Default Title'
14-
const description = searchParams.get('description') ?? 'Default Description'
15-
const image = searchParams.get('image') ?? undefined
12+
const title = url.searchParams.get('title') ?? ''
13+
const description = url.searchParams.get('description') ?? ''
14+
let image = url.searchParams.get('image') ?? ''
15+
if (image && !image.startsWith('http'))
16+
image = new URL(image, req.url).toString()
1617

1718
const fontData = await loadGoogleFont('Geist')
1819
const logoUrl = new URL('/icon-512.png', req.url).toString()
@@ -23,7 +24,6 @@ export async function GET(req: Request) {
2324
title={title}
2425
description={description}
2526
image={image}
26-
// oxlint-disable-next-line jsx-a11y/alt-text, next/no-img-element
2727
logo={<img src={logoUrl} width={56} height={56} />}
2828
caption={url.hostname}
2929
/>,

apps/web/content/docs/components/open-graph.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ Copy and paste the following code into your project.
3131
```tsx title="app/api/og/route.tsx"
3232
import { ImageResponse } from 'next/og'
3333

34+
import { OpenGraph } from '@/components/ui/open-graph'
3435
import { appName } from '@/lib/shared'
35-
import { OpenGraph } from '@/registry/ui/open-graph'
3636

3737
export const revalidate = false
3838

39-
export async function GET(req: Request) {
39+
export async function GET(req: Request, _: RouteContext<'/api/og'>) {
4040
const url = new URL(req.url)
4141

42-
const { searchParams } = url
43-
const title = searchParams.get('title') ?? 'Default Title'
44-
const description = searchParams.get('description') ?? 'Default Description'
45-
const image = searchParams.get('image') ?? undefined
42+
const title = url.searchParams.get('title') ?? ''
43+
const description = url.searchParams.get('description') ?? ''
44+
let image = url.searchParams.get('image') ?? ''
45+
if (image && !image.startsWith('http'))
46+
image = new URL(image, req.url).toString()
4647

4748
const logoUrl = new URL('/icon-512.png', req.url).toString()
4849

@@ -52,7 +53,6 @@ export async function GET(req: Request) {
5253
title={title}
5354
description={description}
5455
image={image}
55-
// oxlint-disable-next-line jsx-a11y/alt-text, next/no-img-element
5656
logo={<img src={logoUrl} width={56} height={56} />}
5757
caption={url.hostname}
5858
/>,

apps/web/registry/ui/open-graph.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ function OpenGraph({
2222
const mutedColor = '#a4a4a4'
2323
const isImageOnly = !props.title && !props.description
2424

25+
const MAX_TITLE_LENGTH = props.image ? 35 : 60
26+
const MAX_DESCRIPTION_LENGTH = props.image ? 160 : 300
27+
2528
const truncatedTitle = props.title
26-
? props.title.length > 35
27-
? props.title.slice(0, 32) + '...'
29+
? props.title.length > MAX_TITLE_LENGTH
30+
? props.title.slice(0, MAX_TITLE_LENGTH - 3) + '...'
2831
: props.title
2932
: ''
3033
const truncatedDescription = props.description
31-
? props.description.length > 160
32-
? props.description.slice(0, 157) + '...'
34+
? props.description.length > MAX_DESCRIPTION_LENGTH
35+
? props.description.slice(0, MAX_DESCRIPTION_LENGTH - 3) + '...'
3336
: props.description
3437
: ''
3538

0 commit comments

Comments
 (0)