mirror of
https://github.com/simstudioai/sim.git
synced 2026-08-29 02:27:35 +08:00
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.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { createLandingOgImage } from '@/app/(landing)/og-utils'
|
||||
|
||||
/**
|
||||
* Renders a real PNG. The bundled fonts are the point: Satori throws
|
||||
* "No fonts are loaded" if it receives an empty `fonts` array, which is how a
|
||||
* failed Google Fonts fetch used to take the whole build down.
|
||||
*/
|
||||
describe('landing OG image', () => {
|
||||
it('renders a PNG using the bundled Geist fonts', async () => {
|
||||
const response = await createLandingOgImage({
|
||||
eyebrow: 'Sim integration',
|
||||
title: 'Mintlify Integration',
|
||||
subtitle: 'Deploy, edit, search, and measure Mintlify documentation',
|
||||
pills: ['12 tools', 'API key auth', 'Free to start'],
|
||||
domainLabel: 'sim.ai/integrations/mintlify',
|
||||
})
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
const bytes = new Uint8Array(await response.arrayBuffer())
|
||||
expect(bytes.byteLength).toBeGreaterThan(1000)
|
||||
// PNG magic number — proves Satori laid the text out and resvg rasterized it.
|
||||
expect(Array.from(bytes.slice(0, 8))).toEqual([137, 80, 78, 71, 13, 10, 26, 10])
|
||||
}, 30_000)
|
||||
})
|
||||
@@ -1,3 +1,5 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import { ImageResponse } from 'next/og'
|
||||
import { SimLogoFull } from '@/app/(landing)/components/og-sim-logo'
|
||||
|
||||
@@ -18,28 +20,37 @@ function getTitleFontSize(title: string): number {
|
||||
return TITLE_FONT_SIZE.large
|
||||
}
|
||||
|
||||
async function loadGoogleFont(
|
||||
font: string,
|
||||
weights: string,
|
||||
text: string
|
||||
): Promise<ArrayBuffer | null> {
|
||||
try {
|
||||
const url = `https://fonts.googleapis.com/css2?family=${font}:wght@${weights}&text=${encodeURIComponent(text)}`
|
||||
const css = await (await fetch(url)).text()
|
||||
const resource = css.match(/src: url\(([^)]+)\) format\('(opentype|truetype|woff2?)'\)/)
|
||||
/**
|
||||
* Geist, read from the repo rather than fetched from Google Fonts.
|
||||
*
|
||||
* Satori requires at least one font and throws if it gets none, so a fetch that
|
||||
* returned nothing took the whole build down with "No fonts are loaded" on
|
||||
* whichever page happened to be rendering. That was not a rare race: six routes
|
||||
* build an OG image, `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, in
|
||||
* parallel across build workers.
|
||||
*
|
||||
* Read once at module scope, per Next's `ImageResponse` guidance. `.ttf`
|
||||
* because Satori accepts only ttf/otf/woff — the sibling `.woff2` the app
|
||||
* serves to browsers cannot be reused here.
|
||||
*
|
||||
* These live under `public/` so they need no `outputFileTracingIncludes` entry:
|
||||
* `docker/app.Dockerfile` copies that directory into the runner, which the
|
||||
* `force-dynamic` share-token card needs since it renders per request.
|
||||
*
|
||||
* `process.cwd()` is the app directory in every environment this runs in, not
|
||||
* just dev and build. The container starts at the monorepo root, but Next's
|
||||
* generated standalone `server.js` opens with `process.chdir(__dirname)`, and
|
||||
* that file ships beside `public/` — which is also why `content/` is read this
|
||||
* way at runtime.
|
||||
*/
|
||||
const FONT_DIR = join(process.cwd(), 'public', 'brand', 'fonts')
|
||||
|
||||
if (resource) {
|
||||
const response = await fetch(resource[1])
|
||||
if (response.status === 200) {
|
||||
return await response.arrayBuffer()
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
const [geistRegular, geistMedium] = await Promise.all([
|
||||
readFile(join(FONT_DIR, 'Geist-Regular.ttf')),
|
||||
readFile(join(FONT_DIR, 'Geist-Medium.ttf')),
|
||||
])
|
||||
|
||||
interface LandingOgImageProps {
|
||||
eyebrow: string
|
||||
@@ -57,12 +68,6 @@ export async function createLandingOgImage({
|
||||
pills = [],
|
||||
domainLabel = 'sim.ai',
|
||||
}: LandingOgImageProps) {
|
||||
const text = `${eyebrow}${title}${subtitle}${pills.join('')}${domainLabel}`
|
||||
const [regularFontData, mediumFontData] = await Promise.all([
|
||||
loadGoogleFont('Geist', '400', text),
|
||||
loadGoogleFont('Geist', '500', text),
|
||||
])
|
||||
|
||||
return new ImageResponse(
|
||||
<div
|
||||
style={{
|
||||
@@ -160,26 +165,8 @@ export async function createLandingOgImage({
|
||||
{
|
||||
...size,
|
||||
fonts: [
|
||||
...(regularFontData
|
||||
? [
|
||||
{
|
||||
name: 'Geist',
|
||||
data: regularFontData,
|
||||
style: 'normal' as const,
|
||||
weight: 400 as const,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(mediumFontData
|
||||
? [
|
||||
{
|
||||
name: 'Geist',
|
||||
data: mediumFontData,
|
||||
style: 'normal' as const,
|
||||
weight: 500 as const,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
{ name: 'Geist', data: geistRegular, style: 'normal' as const, weight: 400 as const },
|
||||
{ name: 'Geist', data: geistMedium, style: 'normal' as const, weight: 500 as const },
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,93 @@
|
||||
Copyright 2024 The Geist Project Authors (https://github.com/vercel/geist-font)
|
||||
|
||||
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
||||
This license is copied below, and is also available with a FAQ at:
|
||||
https://openfontlicense.org
|
||||
|
||||
|
||||
-----------------------------------------------------------
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
-----------------------------------------------------------
|
||||
|
||||
PREAMBLE
|
||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||
development of collaborative font projects, to support the font creation
|
||||
efforts of academic and linguistic communities, and to provide a free and
|
||||
open framework in which fonts may be shared and improved in partnership
|
||||
with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves. The
|
||||
fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply
|
||||
to any document created using the fonts or their derivatives.
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such. This may
|
||||
include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software components as
|
||||
distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||
or substituting -- in part or in whole -- any of the components of the
|
||||
Original Version, by changing formats or by porting the Font Software to a
|
||||
new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical
|
||||
writer or other person who contributed to the Font Software.
|
||||
|
||||
PERMISSION & CONDITIONS
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||
redistribute, and sell modified and unmodified copies of the Font
|
||||
Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components,
|
||||
in Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the corresponding
|
||||
Copyright Holder. This restriction only applies to the primary font name as
|
||||
presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole,
|
||||
must be distributed entirely under this license, and must not be
|
||||
distributed under any other license. The requirement for fonts to
|
||||
remain under this license does not apply to any document created
|
||||
using the Font Software.
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are
|
||||
not met.
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
||||
Binary file not shown.
Reference in New Issue
Block a user