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 retry for fetching subsetted Google Fonts #56583

Merged
merged 5 commits into from
Oct 20, 2023

Conversation

berlysia
Copy link
Contributor

@berlysia berlysia commented Oct 8, 2023

What?

Add the same re-retrieval process for subseted font files of Google Font as for CSS files.

Why?

It was reported in #45080 that Japanese fonts such as Noto Sans JP were frequently Failed to fetch.
A retry process was added in #51890, but it did not resolve the issue completely ( #51890 (comment) ).

Here is my reproduction code with 13.5.5-canary.4 (please run locally).
https://stackblitz.com/edit/stackblitz-starters-n8zxlq?file=app%2Fpage.tsx

And my local error log is here(folded)
$ npm run -- dev

> nextjs@0.1.0 dev
> next dev

 ⚠ Port 3000 is in use, trying 3001 instead.
  ▲ Next.js 13.5.5-canary.4
  - Local:        http://localhost:3001

 ✓ Ready in 23.9s
 ○ Compiling /page ...
FetchError: request to https://fonts.gstatic.com/s/notosansjp/v52/-F6jfjtqLzI2JPCgQBnw7HFyzSD-AsregP8VFBEj757Y0rw_qMHVdbR2L8Y9QTJ1LwkRmR5GprQAe69m.4.woff2 failed, reason: 
    at ClientRequest.<anonymous> (/mnt/c/Users/berlysia/Downloads/stackblitz-starters-n8zxlq/node_modules/next/dist/compiled/node-fetch/index.js:1:65756)
    at ClientRequest.emit (node:events:514:28)
    at TLSSocket.socketErrorListener (node:_http_client:495:9)
    at TLSSocket.emit (node:events:514:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  type: 'system',
  errno: 'ETIMEDOUT',
  code: 'ETIMEDOUT'
}
 ⨯ Failed to download `Noto Sans JP` from Google Fonts. Using fallback font instead.

Failed to fetch `Noto Sans JP` from Google Fonts.}
FetchError: request to https://fonts.gstatic.com/s/notosansjp/v52/-F6jfjtqLzI2JPCgQBnw7HFyzSD-AsregP8VFBEj757Y0rw_qMHVdbR2L8Y9QTJ1LwkRmR5GprQAe69m.28.woff2 failed, reason: 
    at ClientRequest.<anonymous> (/mnt/c/Users/berlysia/Downloads/stackblitz-starters-n8zxlq/node_modules/next/dist/compiled/node-fetch/index.js:1:65756)
    at ClientRequest.emit (node:events:514:28)
    at TLSSocket.socketErrorListener (node:_http_client:495:9)
    at TLSSocket.emit (node:events:514:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)
    at runNextTicks (node:internal/process/task_queues:64:3)
    at listOnTimeout (node:internal/timers:540:9)
    at process.processTimers (node:internal/timers:514:7) {
  type: 'system',
  errno: 'ETIMEDOUT',
  code: 'ETIMEDOUT'
}
...(15 errors emitted)

I've found that the issue is not limited to fetching CSS, fetching subset font files is also failing.
By adding retry handling to the fetch of individual subseted font files as well, I (almost) never see Failed to fetch anymore.

The issue tends to become more apparent when downloading a larger number of subsetted fonts.
This suggests that the problem is more likely to occur with larger fonts, such as those designed for CJK languages.

How?

Add the same re-retrieval process for subseted font files of Google Font as for CSS files.

Related to #51890 #53239 #45080 #53279

@ijjk ijjk added Font (next/font) Related to Next.js Font Optimization. type: next labels Oct 8, 2023
@@ -1,6 +1,7 @@
// @ts-ignore
import fetch from 'next/dist/compiled/node-fetch'
import { getProxyAgent } from './get-proxy-agent'
import { retry } from './retry'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use async-retry instead of the self-baked solution.

@berlysia berlysia force-pushed the add-retry-for-font-fetching branch from bf906a4 to b3ceafc Compare October 9, 2023 09:25
@berlysia berlysia requested review from a team as code owners October 9, 2023 09:25
"@types/fontkit": "2.0.0",
"@vercel/ncc": "0.34.0",
"async-retry": "1.3.3",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: async-retry is already in devDeps at root and create-next-app

🔗 #56583 (comment)

@@ -1,24 +1,10 @@
// @ts-ignore
import fetch from 'next/dist/compiled/node-fetch'
// eslint-disable-next-line import/no-extraneous-dependencies
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import/no-extraneous-dependencies is now useless because of ncc, right?

introduced by #13482
disable directives: https://github.com/search?q=repo%3Avercel%2Fnext.js+%22import%2Fno-extraneous-dependencies%22+%22eslint-disable%22&type=code

current configuration:

next.js/.eslintrc.json

Lines 163 to 176 in 9b2f29e

{
"files": ["packages/**"],
"excludedFiles": [
"packages/next/taskfile*.js",
"packages/next/webpack.config.js"
],
"rules": {
"no-shadow": ["warn", { "builtinGlobals": false }],
"import/no-extraneous-dependencies": [
"error",
{ "devDependencies": false }
]
}
},

@berlysia berlysia force-pushed the add-retry-for-font-fetching branch from b3ceafc to db57027 Compare October 9, 2023 09:44
(e as Error).message + `\n\nRetrying ${attempt}/${retries}...`
)
},
minTimeout: 100,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: The cooldown time for retries is based on the original code.
However, the second retry and later, the wait time doubles with each subsequent retry. This behavior is due to the default settings of async-retry and can be modified if needed. async-retry API

Copy link
Contributor

@SukkaW SukkaW Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the default "Exponential Backoff" behavior of async-retry and IMHO we should keep it.

@berlysia berlysia force-pushed the add-retry-for-font-fetching branch from db57027 to b422eaa Compare October 11, 2023 21:49
@berlysia berlysia changed the title Apply retry utility to fetching Google Font subsetted files Add retry for fetching subsetted Google Fonts Oct 11, 2023
@ijjk
Copy link
Member

ijjk commented Oct 13, 2023

Allow CI Workflow Run

  • approve CI run for commit: b422eaa

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

2 similar comments
@ijjk
Copy link
Member

ijjk commented Oct 13, 2023

Allow CI Workflow Run

  • approve CI run for commit: b422eaa

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@ijjk
Copy link
Member

ijjk commented Oct 13, 2023

Allow CI Workflow Run

  • approve CI run for commit: b422eaa

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@ijjk
Copy link
Member

ijjk commented Oct 13, 2023

Stats from current PR

Default Build
General Overall increase ⚠️
vercel/next.js canary berlysia/next.js add-retry-for-font-fetching Change
buildDuration 10.6s 10.5s N/A
buildDurationCached 6.1s 6.1s N/A
nodeModulesSize 174 MB 174 MB ⚠️ +1.16 kB
nextStartRea..uration (ms) 515ms 497ms N/A
Client Bundles (main, webpack)
vercel/next.js canary berlysia/next.js add-retry-for-font-fetching Change
199-HASH.js gzip 27.5 kB 27.5 kB
3f784ff6-HASH.js gzip 53.3 kB 53.3 kB
99.HASH.js gzip 182 B 182 B
framework-HASH.js gzip 45.3 kB 45.3 kB
main-app-HASH.js gzip 254 B 251 B N/A
main-HASH.js gzip 32.9 kB 32.9 kB N/A
webpack-HASH.js gzip 1.75 kB 1.75 kB N/A
Overall change 126 kB 126 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary berlysia/next.js add-retry-for-font-fetching Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary berlysia/next.js add-retry-for-font-fetching Change
_app-HASH.js gzip 206 B 205 B N/A
_error-HASH.js gzip 182 B 180 B N/A
amp-HASH.js gzip 506 B 505 B N/A
css-HASH.js gzip 322 B 323 B N/A
dynamic-HASH.js gzip 2.57 kB 2.57 kB N/A
edge-ssr-HASH.js gzip 260 B 259 B N/A
head-HASH.js gzip 350 B 350 B
hooks-HASH.js gzip 369 B 369 B
image-HASH.js gzip 4.36 kB 4.36 kB N/A
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.64 kB 2.63 kB N/A
routerDirect..HASH.js gzip 312 B 311 B N/A
script-HASH.js gzip 385 B 384 B N/A
withRouter-HASH.js gzip 307 B 308 B N/A
1afbb74e6ecf..834.css gzip 106 B 106 B
Overall change 1.08 kB 1.08 kB
Client Build Manifests
vercel/next.js canary berlysia/next.js add-retry-for-font-fetching Change
_buildManifest.js gzip 484 B 482 B N/A
Overall change 0 B 0 B
Rendered Page Sizes
vercel/next.js canary berlysia/next.js add-retry-for-font-fetching Change
index.html gzip 528 B 529 B N/A
link.html gzip 543 B 542 B N/A
withRouter.html gzip 524 B 524 B
Overall change 524 B 524 B
Edge SSR bundle Size
vercel/next.js canary berlysia/next.js add-retry-for-font-fetching Change
edge-ssr.js gzip 94 kB 94 kB N/A
page.js gzip 155 kB 155 kB N/A
Overall change 0 B 0 B
Middleware size
vercel/next.js canary berlysia/next.js add-retry-for-font-fetching Change
middleware-b..fest.js gzip 624 B 624 B
middleware-r..fest.js gzip 150 B 151 B N/A
middleware.js gzip 22.9 kB 22.9 kB N/A
edge-runtime..pack.js gzip 1.92 kB 1.92 kB
Overall change 2.55 kB 2.55 kB
Commit: 9cde18b

@smorimoto
Copy link
Contributor

This PR picks up where I missed it and I think we should merge this and do a point release soon as this addresses a pretty serious issue.
We have not seen instability on the Vercel builder for months, but in the last few days it has become unstable even on the Vercel builder, and manually clicking redeploy over and over again is a waste of time.
We build Next.js an incredible number of times a day all over the world, so I'd like to see the Vercel team do better. This addresses things, but doesn't fix anything.

@berlysia berlysia force-pushed the add-retry-for-font-fetching branch from b422eaa to 4e3d0a4 Compare October 16, 2023 18:24
@styfle styfle added the CI approved Approve running CI for fork label Oct 20, 2023
Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks! 🎉

@kodiakhq kodiakhq bot merged commit c84f5ed into vercel:canary Oct 20, 2023
53 of 58 checks passed
@berlysia berlysia deleted the add-retry-for-font-fetching branch October 20, 2023 18:58
@smorimoto
Copy link
Contributor

@styfle Thanks for taking care of this!
Tell Gabe I was so grateful 😄

@styfle
Copy link
Member

styfle commented Oct 20, 2023

Reverting due to an error #57154

kodiakhq bot pushed a commit that referenced this pull request Oct 20, 2023
Reverts #56583 due to missing dependency

```
app/layout.jsx
An error occured in 'next/font'.
Error: Cannot find module 'async-retry'
Require stack:
- node_modules/•pnpm/next@13.5.7-canary.12_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/@next/font/dist/google/retry.js
- node_modules/-pnpm/next@13.5.7-canary-12_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/@next/font/dist/google/fetch-css-from-google-fonts.js
```
@berlysia
Copy link
Contributor Author

Hello @styfle ,

I noticed that this PR was reverted. I'm confident that my changes still can resolve the issue.

However, I'm not certain why the Cannot find module error is occurring. I've suggested two approaches to resolve this error:

Which approach do you prefer, or another?

I'm not familiar with the behavior of the ncc library or the way of next.js , so I can't say with confidence which solution is best at this moment.

@github-actions github-actions bot added the locked label Nov 7, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CI approved Approve running CI for fork Font (next/font) Related to Next.js Font Optimization. locked type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants