Unverified Commit 5f2c9d0b authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Update subset validation in @next/font/google and fix CJK bug (#44594)

Currently there's a bug when selecting Chinese, Japanese or Korean (CJK)
as subsets.
```js
const notoSans = Noto_Sans_JP({
  subsets: ['japanese'],
})
```
It actually doesn't work, nothing preloads. This PR solves this by
removing CJK languages as candidates for preloading. The reason is that
they contain so many glyphs that each font-family is split up in 100+
font files. It doesn't make sense to preload all of them.

So CJK users will have to disable preloading.
```js
const notoSansJapanese = Noto_Sans_JP({
  weight: '400',
  preload: false,
})
```
In case you do manually disable preloading like above, the default
`font-display` is changed to `swap`.

This PR also improves the validation errors of subsets.
1. Providing unknown subset
```
`@next/font` error:
Unknown subset `japanese` for font `Inter`.
Available subsets: `cyrillic`, `cyrillic-ext`, `greek`, `greek-ext`, `latin`, `latin-ext`, `vietnamese`
```
2. Missing specified subset. The error has a link with further
instructions.
```
`@next/font` error:
Missing selected subsets for font `Inter`. Please specify subsets in the function call or in your `next.config.js`. Read more: https://nextjs.org/docs/messages/google-fonts-missing-subsets
```

fixes NEXT-336

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 2c499722
......@@ -6,7 +6,7 @@ Preload is enabled for a font that is missing a specified subset.
#### Possible Ways to Fix It
Specify which subsets to preload for that font.
##### Specify which subsets to preload for that font.
- On a font per font basis by adding it to the function call
......@@ -29,6 +29,17 @@ module.exports = {
If both are configured, the subset in the function call is used.
##### Disable preloading for that font
If it's not possible to preload your intended subset you can disable preloading.
```js
const notoSansJapanese = Noto_Sans_JP({
weight: '400',
preload: false,
})
```
### Useful Links
[Specifying a subset](https://beta.nextjs.org/docs/optimizing/fonts#specifying-a-subset)
[Specifying a subset](https://nextjs.org/docs/basic-features/font-optimization#specifying-a-subset)
This diff is collapsed.
This diff is collapsed.
......@@ -3,8 +3,6 @@ import type { AdjustFontFallback, FontLoader } from 'next/font'
import { calculateSizeAdjustValues } from 'next/dist/server/font-utils'
// @ts-ignore
import * as Log from 'next/dist/build/output/log'
// @ts-ignore
import chalk from 'next/dist/compiled/chalk'
import {
fetchCSSFromGoogleFonts,
fetchFontFile,
......@@ -38,8 +36,6 @@ const downloadGoogleFonts: FontLoader = async ({
isServer,
loaderContext,
}) => {
const subsets = config?.subsets || []
const {
fontFamily,
weights,
......@@ -50,18 +46,8 @@ const downloadGoogleFonts: FontLoader = async ({
fallback,
adjustFontFallback,
variable,
subsets: callSubsets,
} = validateData(functionName, data)
if (isServer && preload && !callSubsets && !config?.subsets) {
Log.warn(
`The ${chalk.bold('@next/font/google')} font ${chalk.bold(
fontFamily
)} has no selected subsets. Please specify subsets in the function call or in your ${chalk.bold(
'next.config.js'
)}, otherwise no fonts will be preloaded. Read more: https://nextjs.org/docs/messages/google-fonts-missing-subsets`
)
}
subsets,
} = validateData(functionName, data, config)
const fontAxes = getFontAxes(
fontFamily,
......@@ -142,8 +128,7 @@ const downloadGoogleFonts: FontLoader = async ({
) {
fontFiles.push({
googleFontFileUrl,
preloadFontFile:
!!preload && (callSubsets ?? subsets).includes(currentSubset),
preloadFontFile: !!preload && subsets.includes(currentSubset),
})
}
}
......
......@@ -18,28 +18,51 @@ type FontOptions = {
fallback?: string[]
adjustFontFallback: boolean
variable?: string
subsets?: string[]
subsets: string[]
}
export function validateData(functionName: string, data: any): FontOptions {
export function validateData(
functionName: string,
data: any,
config: any
): FontOptions {
let {
weight,
style,
display = 'optional',
preload = true,
display = preload ? 'optional' : 'swap',
axes,
fallback,
adjustFontFallback = true,
variable,
subsets,
subsets: callSubsets,
} = data[0] || ({} as any)
const subsets = callSubsets ?? config?.subsets ?? []
if (functionName === '') {
nextFontError(`@next/font/google has no default export`)
}
const fontFamily = functionName.replace(/_/g, ' ')
const fontFamilyData = (fontData as any)[fontFamily]
const fontWeights = fontFamilyData?.weights
if (preload && !callSubsets && !config?.subsets) {
nextFontError(
`Missing selected subsets for font \`${fontFamily}\`. Please specify subsets in the function call or in your \`next.config.js\`. Read more: https://nextjs.org/docs/messages/google-fonts-missing-subsets`
)
}
const availableSubsets = fontFamilyData.subsets
subsets.forEach((subset: string) => {
if (!availableSubsets.includes(subset)) {
nextFontError(
`Unknown subset \`${subset}\` for font \`${fontFamily}\`.\nAvailable subsets: ${formatValues(
availableSubsets
)}`
)
}
})
const fontWeights = fontFamilyData.weights
if (!fontWeights) {
nextFontError(`Unknown font \`${fontFamily}\``)
}
......
......@@ -13,8 +13,17 @@ const fetch = require('node-fetch')
import type { CssVariable, NextFont, NextFontWithVariable, Display } from '../types'
`
const fontData = {}
const ignoredSubsets = [
'menu',
'japanese',
'korean',
'chinese-simplified',
'chinese-hongkong',
'chinese-traditional',
]
for (let { family, fonts, axes, subsets } of familyMetadataList) {
subsets = subsets.filter((subset) => subset !== 'menu')
subsets = subsets.filter((subset) => !ignoredSubsets.includes(subset))
const hasPreloadableSubsets = subsets.length > 0
const weights = new Set()
const styles = new Set()
......@@ -45,6 +54,7 @@ const fetch = require('node-fetch')
weights: [...weights],
styles: [...styles],
axes: hasVariableFont ? axes : undefined,
subsets,
}
const optionalIfVariableFont = hasVariableFont ? '?' : ''
......@@ -66,10 +76,10 @@ const fetch = require('node-fetch')
style?: ${formatUnion(styleTypes)} | Array<${formatUnion(styleTypes)}>
display?:Display
variable?: T
preload?:boolean
${hasPreloadableSubsets ? 'preload?:boolean' : ''}
fallback?: string[]
adjustFontFallback?: boolean
subsets?: Array<${formatUnion(subsets)}>
${hasPreloadableSubsets ? `subsets?: Array<${formatUnion(subsets)}>` : ''}
${
optionalAxes
? `axes?:(${formatUnion(optionalAxes.map(({ tag }) => tag))})[]`
......
......@@ -495,12 +495,12 @@ module.exports = {
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
`,
'https://fonts.googleapis.com/css2?family=Fraunces:wght@400&display=optional': `/* vietnamese */
'https://fonts.googleapis.com/css2?family=Fraunces:wght@400&display=swap': `/* vietnamese */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 400;
font-display: optional;
font-display: swap;
src: url(${path.join(
__dirname,
'./fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTBv7Tp05GNyXkb24.woff2'
......@@ -512,7 +512,7 @@ module.exports = {
font-family: 'Fraunces';
font-style: normal;
font-weight: 400;
font-display: optional;
font-display: swap;
src: url(${path.join(
__dirname,
'./fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTB_7Tp05GNyXkb24.woff2'
......@@ -524,19 +524,19 @@ module.exports = {
font-family: 'Fraunces';
font-style: normal;
font-weight: 400;
font-display: optional;
font-display: swap;
src: url(${path.join(
__dirname,
'./fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTCf7Tp05GNyXk.woff2'
)}) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}`,
'https://fonts.googleapis.com/css2?family=Indie+Flower:wght@400&display=optional': `/* latin */
'https://fonts.googleapis.com/css2?family=Indie+Flower:wght@400&display=swap': `/* latin */
@font-face {
font-family: 'Indie Flower';
font-style: normal;
font-weight: 400;
font-display: optional;
font-display: swap;
src: url(${path.join(
__dirname,
'./fonts/m8JVjfNVeKWVnh3QMuKkFcZVaUuH99GUDg.woff2'
......
......@@ -24,7 +24,7 @@ describe('next/jest', () => {
import localFont from "@next/font/local";
import { Inter } from "@next/font/google";
const inter = Inter();
const inter = Inter({ subsets: ["latin"] });
const myFont = localFont({ src: "./my-font.woff2" });
const Comp = dynamic(() => import("../components/comp"), {
......
......@@ -166,7 +166,7 @@ describe('@next/font/google loader', () => {
variableName: 'myFont',
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unknown font \`Unknown Font\`"`
`"Cannot read properties of undefined (reading 'subsets')"`
)
})
......@@ -337,6 +337,62 @@ describe('@next/font/google loader', () => {
`"Unexpected \`variable\` in weight array for font \`Inter\`. You only need \`variable\`, it includes all available weights."`
)
})
test('Invalid subset in call', async () => {
await expect(
loader({
functionName: 'Inter',
data: [{ weight: ['100', 'variable'], subsets: ['latin', 'oops'] }],
config: { subsets: ['ignored'] },
emitFontFile: jest.fn(),
resolve: jest.fn(),
loaderContext: {} as any,
isDev: false,
isServer: true,
variableName: 'myFont',
})
).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unknown subset \`oops\` for font \`Inter\`.
Available subsets: \`cyrillic\`, \`cyrillic-ext\`, \`greek\`, \`greek-ext\`, \`latin\`, \`latin-ext\`, \`vietnamese\`"
`)
})
test('Invalid subset in config', async () => {
await expect(
loader({
functionName: 'Inter',
data: [{ weight: ['100', 'variable'] }],
config: { subsets: ['whoops'] },
emitFontFile: jest.fn(),
resolve: jest.fn(),
loaderContext: {} as any,
isDev: false,
isServer: true,
variableName: 'myFont',
})
).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unknown subset \`whoops\` for font \`Inter\`.
Available subsets: \`cyrillic\`, \`cyrillic-ext\`, \`greek\`, \`greek-ext\`, \`latin\`, \`latin-ext\`, \`vietnamese\`"
`)
})
test('Missing subsets in config and call', async () => {
await expect(
loader({
functionName: 'Inter',
data: [{ weight: ['100', 'variable'] }],
config: {},
emitFontFile: jest.fn(),
resolve: jest.fn(),
loaderContext: {} as any,
isDev: false,
isServer: true,
variableName: 'myFont',
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Missing selected subsets for font \`Inter\`. Please specify subsets in the function call or in your \`next.config.js\`. Read more: https://nextjs.org/docs/messages/google-fonts-missing-subsets"`
)
})
})
it('should not send duplicate requests when several font variants use the same font file', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment