fix: retry google fonts fetch on fail (#11217)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Almeida
2025-10-26 22:15:12 +00:00
committed by GitHub
parent 4f9475d937
commit 4c0d667773
3 changed files with 50 additions and 23 deletions

View File

@@ -68,6 +68,7 @@
"next-mdx-remote-client": "^2.1.7",
"next-themes": "^0.4.6",
"nuqs": "^2.7.2",
"p-retry": "^7.1.0",
"react": "^19.2.0",
"react-aria": "^3.44.0",
"react-aria-components": "^1.13.0",

View File

@@ -1,5 +1,6 @@
import { generateOGImage } from 'fumadocs-ui/og';
import { notFound } from 'next/navigation';
import pRetry, { AbortError } from 'p-retry';
import { source } from '@/lib/source';
export function generateStaticParams() {
@@ -10,19 +11,26 @@ export function generateStaticParams() {
}
async function loadGoogleFont(font: string, text: string) {
const url = `https://fonts.googleapis.com/css2?family=${font}&text=${encodeURIComponent(text)}`;
const css = await (await fetch(url)).text();
// eslint-disable-next-line prefer-named-capture-group
const resource = /src: url\((.+)\) format\('(opentype|truetype)'\)/.exec(css);
return pRetry(
async () => {
const url = `https://fonts.googleapis.com/css2?family=${font}&text=${encodeURIComponent(text)}`;
const css = await (await fetch(url)).text();
// eslint-disable-next-line prefer-named-capture-group
const resource = /src: url\((.+)\) format\('(opentype|truetype)'\)/.exec(css);
if (resource) {
const response = await fetch(resource[1]!);
if (response.status === 200) {
return response.arrayBuffer();
}
}
if (resource) {
const response = await fetch(resource[1]!);
if (response.status === 200) {
return response.arrayBuffer();
}
}
throw new Error('failed to load font data');
throw new AbortError('failed to load font data');
},
{
retries: 3,
},
);
}
export async function GET(_req: Request, { params }: { params: Promise<{ slug: string[] }> }) {