Skip to content

Commit 64fb8f0

Browse files
authored
fix(og): read OG fonts from the repo instead of fetching Google Fonts at build (#6471)
* fix(og): read OG fonts from the repo instead of fetching Google Fonts at build The release build died prerendering an integration OG card with "No fonts are loaded. At least one font is required to calculate the layout." loadGoogleFont swallowed every failure and returned null, so a throttled fetch produced an empty fonts array, and Satori requires at least one. Six routes build OG images -- integrations/[slug] alone is 237 pages -- and each render fetched two weights subsetted by &text=, a per-page URL no cache can reuse. Several hundred uncacheable requests to one host from one CI egress IP across parallel build workers, so a page losing that race was expected, not unlucky. Mintlify was just whichever page drew the short straw; its description is 56 chars, unremarkable next to slack's 141. Geist 400/500 now ship in public/brand/fonts and are read once at module scope, per Next's ImageResponse guidance. .ttf because Satori accepts only ttf/otf/woff -- the .woff2 already served to browsers cannot be reused. public/ needs no outputFileTracingIncludes entry: the Dockerfile copies it into the runner, which the force-dynamic share-token card needs since it renders per request. Output is unchanged: rendering the same card with the full font and with the old subset produces a byte-identical PNG (0 of 3,024,000 subpixels differ). Render drops from ~74ms plus ~425ms of font fetching to ~74ms, and the share card no longer makes two Google round trips per request. * docs(og): record why process.cwd() is the app dir in the container Review flagged the font path as invalid in the standalone image, reasoning that the container starts at the monorepo root. It does -- but Next's generated standalone server.js opens with process.chdir(__dirname), and that file ships beside public/. Same reason content/ is read this way at runtime.
1 parent 38f9d57 commit 64fb8f0

5 files changed

Lines changed: 155 additions & 47 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import { describe, expect, it } from 'vitest'
5+
import { createLandingOgImage } from '@/app/(landing)/og-utils'
6+
7+
/**
8+
* Renders a real PNG. The bundled fonts are the point: Satori throws
9+
* "No fonts are loaded" if it receives an empty `fonts` array, which is how a
10+
* failed Google Fonts fetch used to take the whole build down.
11+
*/
12+
describe('landing OG image', () => {
13+
it('renders a PNG using the bundled Geist fonts', async () => {
14+
const response = await createLandingOgImage({
15+
eyebrow: 'Sim integration',
16+
title: 'Mintlify Integration',
17+
subtitle: 'Deploy, edit, search, and measure Mintlify documentation',
18+
pills: ['12 tools', 'API key auth', 'Free to start'],
19+
domainLabel: 'sim.ai/integrations/mintlify',
20+
})
21+
22+
expect(response.status).toBe(200)
23+
const bytes = new Uint8Array(await response.arrayBuffer())
24+
expect(bytes.byteLength).toBeGreaterThan(1000)
25+
// PNG magic number — proves Satori laid the text out and resvg rasterized it.
26+
expect(Array.from(bytes.slice(0, 8))).toEqual([137, 80, 78, 71, 13, 10, 26, 10])
27+
}, 30_000)
28+
})

apps/sim/app/(landing)/og-utils.tsx

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { readFile } from 'node:fs/promises'
2+
import { join } from 'node:path'
13
import { ImageResponse } from 'next/og'
24
import { SimLogoFull } from '@/app/(landing)/components/og-sim-logo'
35

@@ -18,28 +20,37 @@ function getTitleFontSize(title: string): number {
1820
return TITLE_FONT_SIZE.large
1921
}
2022

21-
async function loadGoogleFont(
22-
font: string,
23-
weights: string,
24-
text: string
25-
): Promise<ArrayBuffer | null> {
26-
try {
27-
const url = `https://fonts.googleapis.com/css2?family=${font}:wght@${weights}&text=${encodeURIComponent(text)}`
28-
const css = await (await fetch(url)).text()
29-
const resource = css.match(/src: url\(([^)]+)\) format\('(opentype|truetype|woff2?)'\)/)
23+
/**
24+
* Geist, read from the repo rather than fetched from Google Fonts.
25+
*
26+
* Satori requires at least one font and throws if it gets none, so a fetch that
27+
* returned nothing took the whole build down with "No fonts are loaded" on
28+
* whichever page happened to be rendering. That was not a rare race: six routes
29+
* build an OG image, `integrations/[slug]` alone is 237 pages, and each render
30+
* fetched two weights subsetted by `&text=` — a per-page URL no cache can reuse.
31+
* Several hundred uncacheable requests to one host, from one CI egress IP, in
32+
* parallel across build workers.
33+
*
34+
* Read once at module scope, per Next's `ImageResponse` guidance. `.ttf`
35+
* because Satori accepts only ttf/otf/woff — the sibling `.woff2` the app
36+
* serves to browsers cannot be reused here.
37+
*
38+
* These live under `public/` so they need no `outputFileTracingIncludes` entry:
39+
* `docker/app.Dockerfile` copies that directory into the runner, which the
40+
* `force-dynamic` share-token card needs since it renders per request.
41+
*
42+
* `process.cwd()` is the app directory in every environment this runs in, not
43+
* just dev and build. The container starts at the monorepo root, but Next's
44+
* generated standalone `server.js` opens with `process.chdir(__dirname)`, and
45+
* that file ships beside `public/` — which is also why `content/` is read this
46+
* way at runtime.
47+
*/
48+
const FONT_DIR = join(process.cwd(), 'public', 'brand', 'fonts')
3049

31-
if (resource) {
32-
const response = await fetch(resource[1])
33-
if (response.status === 200) {
34-
return await response.arrayBuffer()
35-
}
36-
}
37-
} catch {
38-
return null
39-
}
40-
41-
return null
42-
}
50+
const [geistRegular, geistMedium] = await Promise.all([
51+
readFile(join(FONT_DIR, 'Geist-Regular.ttf')),
52+
readFile(join(FONT_DIR, 'Geist-Medium.ttf')),
53+
])
4354

4455
interface LandingOgImageProps {
4556
eyebrow: string
@@ -57,12 +68,6 @@ export async function createLandingOgImage({
5768
pills = [],
5869
domainLabel = 'sim.ai',
5970
}: LandingOgImageProps) {
60-
const text = `${eyebrow}${title}${subtitle}${pills.join('')}${domainLabel}`
61-
const [regularFontData, mediumFontData] = await Promise.all([
62-
loadGoogleFont('Geist', '400', text),
63-
loadGoogleFont('Geist', '500', text),
64-
])
65-
6671
return new ImageResponse(
6772
<div
6873
style={{
@@ -160,26 +165,8 @@ export async function createLandingOgImage({
160165
{
161166
...size,
162167
fonts: [
163-
...(regularFontData
164-
? [
165-
{
166-
name: 'Geist',
167-
data: regularFontData,
168-
style: 'normal' as const,
169-
weight: 400 as const,
170-
},
171-
]
172-
: []),
173-
...(mediumFontData
174-
? [
175-
{
176-
name: 'Geist',
177-
data: mediumFontData,
178-
style: 'normal' as const,
179-
weight: 500 as const,
180-
},
181-
]
182-
: []),
168+
{ name: 'Geist', data: geistRegular, style: 'normal' as const, weight: 400 as const },
169+
{ name: 'Geist', data: geistMedium, style: 'normal' as const, weight: 500 as const },
183170
],
184171
}
185172
)
71.3 KB
Binary file not shown.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Copyright 2024 The Geist Project Authors (https://github.com/vercel/geist-font)
2+
3+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
4+
This license is copied below, and is also available with a FAQ at:
5+
https://openfontlicense.org
6+
7+
8+
-----------------------------------------------------------
9+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10+
-----------------------------------------------------------
11+
12+
PREAMBLE
13+
The goals of the Open Font License (OFL) are to stimulate worldwide
14+
development of collaborative font projects, to support the font creation
15+
efforts of academic and linguistic communities, and to provide a free and
16+
open framework in which fonts may be shared and improved in partnership
17+
with others.
18+
19+
The OFL allows the licensed fonts to be used, studied, modified and
20+
redistributed freely as long as they are not sold by themselves. The
21+
fonts, including any derivative works, can be bundled, embedded,
22+
redistributed and/or sold with any software provided that any reserved
23+
names are not used by derivative works. The fonts and derivatives,
24+
however, cannot be released under any other type of license. The
25+
requirement for fonts to remain under this license does not apply
26+
to any document created using the fonts or their derivatives.
27+
28+
DEFINITIONS
29+
"Font Software" refers to the set of files released by the Copyright
30+
Holder(s) under this license and clearly marked as such. This may
31+
include source files, build scripts and documentation.
32+
33+
"Reserved Font Name" refers to any names specified as such after the
34+
copyright statement(s).
35+
36+
"Original Version" refers to the collection of Font Software components as
37+
distributed by the Copyright Holder(s).
38+
39+
"Modified Version" refers to any derivative made by adding to, deleting,
40+
or substituting -- in part or in whole -- any of the components of the
41+
Original Version, by changing formats or by porting the Font Software to a
42+
new environment.
43+
44+
"Author" refers to any designer, engineer, programmer, technical
45+
writer or other person who contributed to the Font Software.
46+
47+
PERMISSION & CONDITIONS
48+
Permission is hereby granted, free of charge, to any person obtaining
49+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
50+
redistribute, and sell modified and unmodified copies of the Font
51+
Software, subject to the following conditions:
52+
53+
1) Neither the Font Software nor any of its individual components,
54+
in Original or Modified Versions, may be sold by itself.
55+
56+
2) Original or Modified Versions of the Font Software may be bundled,
57+
redistributed and/or sold with any software, provided that each copy
58+
contains the above copyright notice and this license. These can be
59+
included either as stand-alone text files, human-readable headers or
60+
in the appropriate machine-readable metadata fields within text or
61+
binary files as long as those fields can be easily viewed by the user.
62+
63+
3) No Modified Version of the Font Software may use the Reserved Font
64+
Name(s) unless explicit written permission is granted by the corresponding
65+
Copyright Holder. This restriction only applies to the primary font name as
66+
presented to the users.
67+
68+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69+
Software shall not be used to promote, endorse or advertise any
70+
Modified Version, except to acknowledge the contribution(s) of the
71+
Copyright Holder(s) and the Author(s) or with their explicit written
72+
permission.
73+
74+
5) The Font Software, modified or unmodified, in part or in whole,
75+
must be distributed entirely under this license, and must not be
76+
distributed under any other license. The requirement for fonts to
77+
remain under this license does not apply to any document created
78+
using the Font Software.
79+
80+
TERMINATION
81+
This license becomes null and void if any of the above conditions are
82+
not met.
83+
84+
DISCLAIMER
85+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93+
OTHER DEALINGS IN THE FONT SOFTWARE.
71.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)