Skip to content

Commit

Permalink
cleaner font fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane98c committed Jan 7, 2025
1 parent 5de9674 commit 36b15b1
Showing 1 changed file with 24 additions and 45 deletions.
69 changes: 24 additions & 45 deletions pages/api/og.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,33 @@ import { postMetadata } from '../../utils/post-metadata'

export const runtime = 'edge'

const fetchFont = async (fontPath, fontType) => {
const headers = new Headers({ Referer: 'https://carbonplan.org/' })
const res = await fetch(`https://fonts.carbonplan.org/relative/${fontPath}`, {
cache: 'force-cache',
headers,
})

if (!res.ok) throw new Error(`Failed to load ${fontType} font: ${res.status}`)
return res.arrayBuffer()
}

const getFonts = async () => {
try {
const headers = new Headers({ Referer: 'https://carbonplan.org/' })
const [relativeMedium, faux, mono] = await Promise.all([
fetch('https://fonts.carbonplan.org/relative/relative-medium-pro.woff', {
cache: 'force-cache',
headers,
}).then(async (res) => {
if (!res.ok)
throw new Error(`Failed to load medium font: ${res.status}`)
return res.arrayBuffer()
}),
fetch(
'https://fonts.carbonplan.org/relative/relative-faux-book-pro.woff',
{
cache: 'force-cache',
headers,
}
).then(async (res) => {
if (!res.ok) throw new Error(`Failed to load faux font: ${res.status}`)
return res.arrayBuffer()
}),
fetch(
'https://fonts.carbonplan.org/relative/relative-mono-11-pitch-pro.woff',
{
cache: 'force-cache',
headers,
}
).then(async (res) => {
if (!res.ok) throw new Error(`Failed to load mono font: ${res.status}`)
return res.arrayBuffer()
}),
])

return [
{
name: 'heading',
data: relativeMedium,
},
{
name: 'faux',
data: faux,
},
{
name: 'mono',
data: mono,
},
const fonts = [
{ path: 'relative-medium-pro.woff', type: 'medium', name: 'heading' },
{ path: 'relative-faux-book-pro.woff', type: 'faux', name: 'faux' },
{ path: 'relative-mono-11-pitch-pro.woff', type: 'mono', name: 'mono' },
]

const fontData = await Promise.all(
fonts.map(({ path, type }) => fetchFont(path, type))
)

return fonts.map(({ name }, index) => ({
name,
data: fontData[index],
}))
} catch (error) {
console.error('Error loading fonts:', error)
throw error
Expand Down

0 comments on commit 36b15b1

Please sign in to comment.