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. ...@@ -6,7 +6,7 @@ Preload is enabled for a font that is missing a specified subset.
#### Possible Ways to Fix It #### 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 - On a font per font basis by adding it to the function call
...@@ -29,6 +29,17 @@ module.exports = { ...@@ -29,6 +29,17 @@ module.exports = {
If both are configured, the subset in the function call is used. 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 ### 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' ...@@ -3,8 +3,6 @@ import type { AdjustFontFallback, FontLoader } from 'next/font'
import { calculateSizeAdjustValues } from 'next/dist/server/font-utils' import { calculateSizeAdjustValues } from 'next/dist/server/font-utils'
// @ts-ignore // @ts-ignore
import * as Log from 'next/dist/build/output/log' import * as Log from 'next/dist/build/output/log'
// @ts-ignore
import chalk from 'next/dist/compiled/chalk'
import { import {
fetchCSSFromGoogleFonts, fetchCSSFromGoogleFonts,
fetchFontFile, fetchFontFile,
...@@ -38,8 +36,6 @@ const downloadGoogleFonts: FontLoader = async ({ ...@@ -38,8 +36,6 @@ const downloadGoogleFonts: FontLoader = async ({
isServer, isServer,
loaderContext, loaderContext,
}) => { }) => {
const subsets = config?.subsets || []
const { const {
fontFamily, fontFamily,
weights, weights,
...@@ -50,18 +46,8 @@ const downloadGoogleFonts: FontLoader = async ({ ...@@ -50,18 +46,8 @@ const downloadGoogleFonts: FontLoader = async ({
fallback, fallback,
adjustFontFallback, adjustFontFallback,
variable, variable,
subsets: callSubsets, subsets,
} = validateData(functionName, data) } = validateData(functionName, data, config)
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`
)
}
const fontAxes = getFontAxes( const fontAxes = getFontAxes(
fontFamily, fontFamily,
...@@ -142,8 +128,7 @@ const downloadGoogleFonts: FontLoader = async ({ ...@@ -142,8 +128,7 @@ const downloadGoogleFonts: FontLoader = async ({
) { ) {
fontFiles.push({ fontFiles.push({
googleFontFileUrl, googleFontFileUrl,
preloadFontFile: preloadFontFile: !!preload && subsets.includes(currentSubset),
!!preload && (callSubsets ?? subsets).includes(currentSubset),
}) })
} }
} }
......
...@@ -18,28 +18,51 @@ type FontOptions = { ...@@ -18,28 +18,51 @@ type FontOptions = {
fallback?: string[] fallback?: string[]
adjustFontFallback: boolean adjustFontFallback: boolean
variable?: string 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 { let {
weight, weight,
style, style,
display = 'optional',
preload = true, preload = true,
display = preload ? 'optional' : 'swap',
axes, axes,
fallback, fallback,
adjustFontFallback = true, adjustFontFallback = true,
variable, variable,
subsets, subsets: callSubsets,
} = data[0] || ({} as any) } = data[0] || ({} as any)
const subsets = callSubsets ?? config?.subsets ?? []
if (functionName === '') { if (functionName === '') {
nextFontError(`@next/font/google has no default export`) nextFontError(`@next/font/google has no default export`)
} }
const fontFamily = functionName.replace(/_/g, ' ') const fontFamily = functionName.replace(/_/g, ' ')
const fontFamilyData = (fontData as any)[fontFamily] 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) { if (!fontWeights) {
nextFontError(`Unknown font \`${fontFamily}\``) nextFontError(`Unknown font \`${fontFamily}\``)
} }
......
...@@ -13,8 +13,17 @@ const fetch = require('node-fetch') ...@@ -13,8 +13,17 @@ const fetch = require('node-fetch')
import type { CssVariable, NextFont, NextFontWithVariable, Display } from '../types' import type { CssVariable, NextFont, NextFontWithVariable, Display } from '../types'
` `
const fontData = {} const fontData = {}
const ignoredSubsets = [
'menu',
'japanese',
'korean',
'chinese-simplified',
'chinese-hongkong',
'chinese-traditional',
]
for (let { family, fonts, axes, subsets } of familyMetadataList) { 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 weights = new Set()
const styles = new Set() const styles = new Set()
...@@ -45,6 +54,7 @@ const fetch = require('node-fetch') ...@@ -45,6 +54,7 @@ const fetch = require('node-fetch')
weights: [...weights], weights: [...weights],
styles: [...styles], styles: [...styles],
axes: hasVariableFont ? axes : undefined, axes: hasVariableFont ? axes : undefined,
subsets,
} }
const optionalIfVariableFont = hasVariableFont ? '?' : '' const optionalIfVariableFont = hasVariableFont ? '?' : ''
...@@ -66,10 +76,10 @@ const fetch = require('node-fetch') ...@@ -66,10 +76,10 @@ const fetch = require('node-fetch')
style?: ${formatUnion(styleTypes)} | Array<${formatUnion(styleTypes)}> style?: ${formatUnion(styleTypes)} | Array<${formatUnion(styleTypes)}>
display?:Display display?:Display
variable?: T variable?: T
preload?:boolean ${hasPreloadableSubsets ? 'preload?:boolean' : ''}
fallback?: string[] fallback?: string[]
adjustFontFallback?: boolean adjustFontFallback?: boolean
subsets?: Array<${formatUnion(subsets)}> ${hasPreloadableSubsets ? `subsets?: Array<${formatUnion(subsets)}>` : ''}
${ ${
optionalAxes optionalAxes
? `axes?:(${formatUnion(optionalAxes.map(({ tag }) => tag))})[]` ? `axes?:(${formatUnion(optionalAxes.map(({ tag }) => tag))})[]`
......
...@@ -495,12 +495,12 @@ module.exports = { ...@@ -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; 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-face {
font-family: 'Fraunces'; font-family: 'Fraunces';
font-style: normal; font-style: normal;
font-weight: 400; font-weight: 400;
font-display: optional; font-display: swap;
src: url(${path.join( src: url(${path.join(
__dirname, __dirname,
'./fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTBv7Tp05GNyXkb24.woff2' './fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTBv7Tp05GNyXkb24.woff2'
...@@ -512,7 +512,7 @@ module.exports = { ...@@ -512,7 +512,7 @@ module.exports = {
font-family: 'Fraunces'; font-family: 'Fraunces';
font-style: normal; font-style: normal;
font-weight: 400; font-weight: 400;
font-display: optional; font-display: swap;
src: url(${path.join( src: url(${path.join(
__dirname, __dirname,
'./fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTB_7Tp05GNyXkb24.woff2' './fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTB_7Tp05GNyXkb24.woff2'
...@@ -524,19 +524,19 @@ module.exports = { ...@@ -524,19 +524,19 @@ module.exports = {
font-family: 'Fraunces'; font-family: 'Fraunces';
font-style: normal; font-style: normal;
font-weight: 400; font-weight: 400;
font-display: optional; font-display: swap;
src: url(${path.join( src: url(${path.join(
__dirname, __dirname,
'./fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTCf7Tp05GNyXk.woff2' './fonts/6NUh8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib1603gg7S2nfgRYIctxuTCf7Tp05GNyXk.woff2'
)}) format('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; 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-face {
font-family: 'Indie Flower'; font-family: 'Indie Flower';
font-style: normal; font-style: normal;
font-weight: 400; font-weight: 400;
font-display: optional; font-display: swap;
src: url(${path.join( src: url(${path.join(
__dirname, __dirname,
'./fonts/m8JVjfNVeKWVnh3QMuKkFcZVaUuH99GUDg.woff2' './fonts/m8JVjfNVeKWVnh3QMuKkFcZVaUuH99GUDg.woff2'
......
...@@ -24,7 +24,7 @@ describe('next/jest', () => { ...@@ -24,7 +24,7 @@ describe('next/jest', () => {
import localFont from "@next/font/local"; import localFont from "@next/font/local";
import { Inter } from "@next/font/google"; import { Inter } from "@next/font/google";
const inter = Inter(); const inter = Inter({ subsets: ["latin"] });
const myFont = localFont({ src: "./my-font.woff2" }); const myFont = localFont({ src: "./my-font.woff2" });
const Comp = dynamic(() => import("../components/comp"), { const Comp = dynamic(() => import("../components/comp"), {
......
...@@ -166,7 +166,7 @@ describe('@next/font/google loader', () => { ...@@ -166,7 +166,7 @@ describe('@next/font/google loader', () => {
variableName: 'myFont', variableName: 'myFont',
}) })
).rejects.toThrowErrorMatchingInlineSnapshot( ).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unknown font \`Unknown Font\`"` `"Cannot read properties of undefined (reading 'subsets')"`
) )
}) })
...@@ -337,6 +337,62 @@ describe('@next/font/google loader', () => { ...@@ -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."` `"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 () => { 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