Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timeout to next/font/google in dev #46834

Merged
merged 3 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/font/src/google/fetch-css-from-google-fonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { nextFontError } from '../next-font-error'
*/
export async function fetchCSSFromGoogleFonts(
url: string,
fontFamily: string
fontFamily: string,
isDev: boolean
): Promise<string> {
// Check if mocked responses are defined, if so use them instead of fetching from Google Fonts
let mockedResponse: string | undefined
Expand All @@ -28,12 +29,18 @@ export async function fetchCSSFromGoogleFonts(
// Just use the mocked CSS if it's set
cssResponse = mockedResponse
} else {
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 3000)
const res = await fetch(url, {
// Add a timeout in dev
signal: isDev ? controller.signal : undefined,
headers: {
// The file format is based off of the user agent, make sure woff2 files are fetched
'user-agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
},
}).finally(() => {
clearTimeout(timeoutId)
})

if (!res.ok) {
Expand Down
13 changes: 11 additions & 2 deletions packages/font/src/google/fetch-font-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fetch from 'next/dist/compiled/node-fetch'
/**
* Fetch the url and return a buffer with the font file.
*/
export async function fetchFontFile(url: string) {
export async function fetchFontFile(url: string, isDev: boolean) {
// Check if we're using mocked data
if (process.env.NEXT_FONT_GOOGLE_MOCKED_RESPONSES) {
// If it's an absolute path, read the file from the filesystem
Expand All @@ -15,6 +15,15 @@ export async function fetchFontFile(url: string) {
return Buffer.from(url)
}

const arrayBuffer = await fetch(url).then((r: any) => r.arrayBuffer())
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 3000)
const arrayBuffer = await fetch(url, {
// Add a timeout in dev
signal: isDev ? controller.signal : undefined,
})
.then((r: any) => r.arrayBuffer())
.finally(() => {
clearTimeout(timeoutId)
})
return Buffer.from(arrayBuffer)
}
6 changes: 2 additions & 4 deletions packages/font/src/google/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const nextFontGoogleFontLoader: FontLoader = async ({
emitFontFile,
isDev,
isServer,
loaderContext,
}) => {
const {
fontFamily,
Expand Down Expand Up @@ -83,7 +82,7 @@ const nextFontGoogleFontLoader: FontLoader = async ({
// Fetch CSS from Google Fonts or get it from the cache
let fontFaceDeclarations = hasCachedCSS
? cssCache.get(url)
: await fetchCSSFromGoogleFonts(url, fontFamily).catch(() => null)
: await fetchCSSFromGoogleFonts(url, fontFamily, isDev).catch(() => null)
if (!hasCachedCSS) {
cssCache.set(url, fontFaceDeclarations ?? null)
} else {
Expand All @@ -109,7 +108,7 @@ const nextFontGoogleFontLoader: FontLoader = async ({
// Download the font file or get it from cache
const fontFileBuffer = hasCachedFont
? fontCache.get(googleFontFileUrl)
: await fetchFontFile(googleFontFileUrl).catch(() => null)
: await fetchFontFile(googleFontFileUrl, isDev).catch(() => null)
if (!hasCachedFont) {
fontCache.set(googleFontFileUrl, fontFileBuffer ?? null)
} else {
Expand Down Expand Up @@ -157,7 +156,6 @@ const nextFontGoogleFontLoader: FontLoader = async ({
css: updatedCssResponse,
}
} catch (err) {
loaderContext.cacheable(false)
if (isDev) {
if (isServer) {
console.error(err)
Expand Down