Skip to content

Instantly share code, notes, and snippets.

@anuragts
Created June 25, 2024 21:09
Show Gist options
  • Save anuragts/fa2b23737c577ce91210528c7844049f to your computer and use it in GitHub Desktop.
Save anuragts/fa2b23737c577ce91210528c7844049f to your computer and use it in GitHub Desktop.
ogimage
import { ImageResponse } from 'next/og';
import { Parameters } from './page';
import { getJobById } from '@/lib/posts/getPostById';
import { createClient } from '@/lib/supabase/getClientClient';
import { PriceService } from '@/services/priceService';
import { redis } from '@/lib/redis';
import { Job } from '@/lib/types/post.types';
import { getJobBySlug } from '@/lib/posts/getJobBySlug';
export const runtime = 'edge';
export const revalidate = 600;
export const size = {
width: 1200,
height: 630,
};
export default async function Image({ params }: { params: Parameters }) {
const fontData = await fetch(
new URL('../../../public/inter-bold.ttf', import.meta.url)
).then((res) => res.arrayBuffer());
const { slug } = params;
const jobCacheKey = `job:${slug}`;
let job: Job | null = null;
const cachedJob = await redis.get(jobCacheKey);
job = typeof cachedJob === 'string' ? JSON.parse(cachedJob) : cachedJob;
if (!job) {
const supabaseClient = createClient();
job = await getJobBySlug(supabaseClient, slug);
if (job) {
await redis.set(jobCacheKey, JSON.stringify(job), { ex: 600 });
}
}
if (!job) {
return new ImageResponse(
(
<div
style={{
background: 'linear-gradient(to bottom, #484BE1, #1E1C54)',
fontFamily: '"Inter"',
}}
tw='flex flex-col items-center justify-center w-full h-full text-white'
>
<div tw='flex flex-col items-center'>
<p tw='text-[40px] font-bold mt-0 mb-4 opacity-70'>Now Hiring</p>
<p tw='text-[60px] font-bold m-0'>๐Ÿ‘‰ Click To Apply ๐Ÿ‘ˆ</p>
</div>
</div>
),
{
...size,
emoji: 'twemoji',
fonts: [
{
name: 'Inter',
data: fontData,
weight: 700,
style: 'normal',
},
],
}
);
}
return new ImageResponse(
(
<div
style={{
background: 'linear-gradient(to bottom, #484BE1, #1E1C54)',
fontFamily: '"Inter"',
}}
tw='flex flex-col items-center justify-center w-full h-full text-white'
>
<div tw='flex flex-col items-center mb-0'>
<p tw='text-[40px] font-bold mt-0 mb-4 opacity-70'>Now Hiring</p>
<p tw='text-[60px] mt-0 mb-4 text-center'>
{job.title}
{job.company ? ` @ ${job.company}` : ''}
</p>
<p tw='text-[60px] font-bold mt-0 mb-4'>{`${PriceService.getPriceShort(
{
payoutUSD: job.max_payout_usd,
payoutType: job.payout_type,
jobType: job.job_type,
}
)} ๐Ÿ’ต`}</p>
<p tw='text-[60px] font-bold mt-0 mb-16'>Remote Based</p>
<p tw='text-[60px] font-bold m-0'>๐Ÿ‘‰ Click To Apply ๐Ÿ‘ˆ</p>
</div>
</div>
),
{
...size,
emoji: 'twemoji',
fonts: [
{
name: 'Inter',
data: fontData,
weight: 700,
style: 'normal',
},
],
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment