Skip to content

Commit de42719

Browse files
janicklas-ralphkodiakhq[bot]
andauthoredMay 19, 2021
Fix font optimization failing on some builds (#25071)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent b9b35d4 commit de42719

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed
 

‎packages/next/build/webpack/plugins/font-stylesheet-gathering-plugin.ts

+31-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,19 @@ export class FontStylesheetGatheringPlugin {
114114
}
115115

116116
this.gatheredStylesheets.push(props.href)
117+
118+
if (isWebpack5) {
119+
const buildInfo = parser?.state?.module?.buildInfo
120+
121+
if (buildInfo) {
122+
buildInfo.valueDependencies.set(
123+
FONT_MANIFEST,
124+
this.gatheredStylesheets
125+
)
126+
}
127+
}
117128
}
129+
118130
// React JSX transform:
119131
parser.hooks.call
120132
.for('_jsx')
@@ -161,8 +173,24 @@ export class FontStylesheetGatheringPlugin {
161173
}
162174
compilation.hooks.finishModules.tapAsync(
163175
this.constructor.name,
164-
async (_: any, modulesFinished: Function) => {
165-
const fontDefinitionPromises = this.gatheredStylesheets.map((url) =>
176+
async (modules: any, modulesFinished: Function) => {
177+
let fontStylesheets = this.gatheredStylesheets
178+
179+
if (isWebpack5) {
180+
const fontUrls = new Set<string>()
181+
modules.forEach((module: any) => {
182+
const fontDependencies = module?.buildInfo?.valueDependencies?.get(
183+
FONT_MANIFEST
184+
)
185+
if (fontDependencies) {
186+
fontDependencies.forEach((v: string) => fontUrls.add(v))
187+
}
188+
})
189+
190+
fontStylesheets = Array.from(fontUrls)
191+
}
192+
193+
const fontDefinitionPromises = fontStylesheets.map((url) =>
166194
getFontDefinitionFromNetwork(url)
167195
)
168196

@@ -173,7 +201,7 @@ export class FontStylesheetGatheringPlugin {
173201
if (css) {
174202
const content = await minifyCss(css)
175203
this.manifestContent.push({
176-
url: this.gatheredStylesheets[promiseIndex],
204+
url: fontStylesheets[promiseIndex],
177205
content,
178206
})
179207
}

‎test/integration/font-optimization/test/index.test.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,22 @@ describe('Font Optimization', () => {
210210

211211
expect(testCss).toStrictEqual(snapshotCss)
212212
})
213+
214+
// Re-run build to check if it works when build is cached
215+
it('should work when build is cached', async () => {
216+
await nextBuild(appDir)
217+
const testJson = JSON.parse(
218+
await fs.readFile(builtPage('font-manifest.json'), {
219+
encoding: 'utf-8',
220+
})
221+
)
222+
expect(testJson.length).toBeGreaterThan(0)
223+
})
213224
}
214225

215226
describe('Font optimization for SSR apps', () => {
216227
beforeAll(async () => {
217-
await fs.writeFile(
218-
nextConfig,
219-
`module.exports = { experimental: {optimizeFonts: true} }`,
220-
'utf8'
221-
)
228+
await fs.writeFile(nextConfig, `module.exports = { }`, 'utf8')
222229

223230
if (fs.pathExistsSync(join(appDir, '.next'))) {
224231
await fs.remove(join(appDir, '.next'))
@@ -237,7 +244,7 @@ describe('Font Optimization', () => {
237244
beforeAll(async () => {
238245
await fs.writeFile(
239246
nextConfig,
240-
`module.exports = { target: 'serverless', experimental: {optimizeFonts: true} }`,
247+
`module.exports = { target: 'serverless' }`,
241248
'utf8'
242249
)
243250
await nextBuild(appDir)
@@ -254,7 +261,7 @@ describe('Font Optimization', () => {
254261
beforeAll(async () => {
255262
await fs.writeFile(
256263
nextConfig,
257-
`module.exports = { target: 'experimental-serverless-trace', experimental: {optimizeFonts: true} }`,
264+
`module.exports = { target: 'experimental-serverless-trace' }`,
258265
'utf8'
259266
)
260267
await nextBuild(appDir)

0 commit comments

Comments
 (0)
Please sign in to comment.