Merge pull request #419 from rishimohan/main

fix: generate share page og image on edge
This commit is contained in:
Marko Kraemer 2025-05-20 19:41:52 +02:00 committed by GitHub
commit f9e9c05db1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 44 additions and 25 deletions

View File

@ -1,9 +1,19 @@
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
// Add route segment config for caching
export const runtime = 'edge'; // Use edge runtime for better performance
export const revalidate = 3600; // Cache for 1 hour
export async function GET(request) { export async function GET(request) {
const { searchParams } = new URL(request.url); const { searchParams } = new URL(request.url);
const title = searchParams.get('title'); const title = searchParams.get('title');
// Add error handling
if (!title) {
return new NextResponse('Missing title parameter', { status: 400 });
}
try {
const response = await fetch(`https://api.orshot.com/v1/studio/render`, { const response = await fetch(`https://api.orshot.com/v1/studio/render`, {
method: 'POST', method: 'POST',
headers: { headers: {
@ -21,6 +31,10 @@ export async function GET(request) {
}), }),
}); });
if (!response.ok) {
throw new Error(`Orshot API error: ${response.status}`);
}
const blob = await response.blob(); const blob = await response.blob();
const arrayBuffer = await blob.arrayBuffer(); const arrayBuffer = await blob.arrayBuffer();
const image = Buffer.from(arrayBuffer); const image = Buffer.from(arrayBuffer);
@ -29,6 +43,11 @@ export async function GET(request) {
status: 200, status: 200,
headers: { headers: {
'Content-Type': 'image/png', 'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
}, },
}); });
} catch (error) {
console.error('OG Image generation error:', error);
return new NextResponse('Error generating image', { status: 500 });
}
} }