Skip to content

Commit c11bc74

Browse files
committed
Add getSiteUrl() for automatic Vercel OG image URL detection
OG images now work automatically on Vercel without manual URL configuration. Priority: NEXT_PUBLIC_SITE_URL > VERCEL_URL > siteConfig.url
1 parent 09f22cf commit c11bc74

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

packages/create-unmint/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-unmint",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "Create a new Unmint documentation project",
55
"type": "module",
66
"bin": {

packages/template/app/docs/[[...slug]]/page.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getMDXComponents } from '../../components/docs/mdx'
66
import { findNeighbour } from 'fumadocs-core/page-tree'
77
import type { Metadata } from 'next'
88
import type { Root, Node } from 'fumadocs-core/page-tree'
9-
import { siteConfig, themeConfig } from '@/lib/theme-config'
9+
import { getSiteUrl } from '@/lib/theme-config'
1010

1111
interface PageProps {
1212
params: Promise<{ slug?: string[] }>
@@ -108,7 +108,9 @@ export async function generateMetadata({ params }: PageProps): Promise<Metadata>
108108
const description = page.data.description
109109

110110
// Build OG image URL with query params
111-
const ogImageUrl = new URL(`${siteConfig.url}/api/og`)
111+
// getSiteUrl() auto-detects Vercel deployments
112+
const baseUrl = getSiteUrl()
113+
const ogImageUrl = new URL(`${baseUrl}/api/og`)
112114
ogImageUrl.searchParams.set('title', title)
113115
ogImageUrl.searchParams.set('section', section)
114116

@@ -119,7 +121,7 @@ export async function generateMetadata({ params }: PageProps): Promise<Metadata>
119121
title,
120122
description,
121123
type: 'article',
122-
url: `${siteConfig.url}${page.url}`,
124+
url: `${baseUrl}${page.url}`,
123125
images: [
124126
{
125127
url: ogImageUrl.toString(),

packages/template/lib/theme-config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,18 @@ export function getCSSVariables(mode: 'light' | 'dark') {
8787
'--accent-muted': colors.accentMuted,
8888
}
8989
}
90+
91+
/**
92+
* Get the site URL dynamically
93+
* Priority: NEXT_PUBLIC_SITE_URL > VERCEL_URL > siteConfig.url
94+
* This allows OG images to work automatically on Vercel without configuration
95+
*/
96+
export function getSiteUrl(): string {
97+
if (process.env.NEXT_PUBLIC_SITE_URL) {
98+
return process.env.NEXT_PUBLIC_SITE_URL
99+
}
100+
if (process.env.VERCEL_URL) {
101+
return `https://${process.env.VERCEL_URL}`
102+
}
103+
return siteConfig.url
104+
}

0 commit comments

Comments
 (0)