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,34 +1,53 @@
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');
const response = await fetch(`https://api.orshot.com/v1/studio/render`, { // Add error handling
method: 'POST', if (!title) {
headers: { return new NextResponse('Missing title parameter', { status: 400 });
'Content-Type': 'application/json', }
Authorization: `Bearer ${process.env.ORSHOT_API_KEY}`,
},
body: JSON.stringify({
templateId: 10,
modifications: {
title,
},
response: {
type: 'binary',
},
}),
});
const blob = await response.blob(); try {
const arrayBuffer = await blob.arrayBuffer(); const response = await fetch(`https://api.orshot.com/v1/studio/render`, {
const image = Buffer.from(arrayBuffer); method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.ORSHOT_API_KEY}`,
},
body: JSON.stringify({
templateId: 10,
modifications: {
title,
},
response: {
type: 'binary',
},
}),
});
return new NextResponse(image, { if (!response.ok) {
status: 200, throw new Error(`Orshot API error: ${response.status}`);
headers: { }
'Content-Type': 'image/png',
}, const blob = await response.blob();
}); const arrayBuffer = await blob.arrayBuffer();
const image = Buffer.from(arrayBuffer);
return new NextResponse(image, {
status: 200,
headers: {
'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 });
}
} }