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

Font optimization #21676

Merged
merged 32 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
edabb95
Updating native-url version
janicklas-ralph Apr 4, 2020
c147099
Bump version
janicklas-ralph Apr 5, 2020
63b04ca
Merge branch 'canary' of github.com:zeit/next.js into canary
janicklas-ralph Apr 16, 2020
df52e5e
Merge branch 'canary' of github.com:zeit/next.js into canary
janicklas-ralph May 4, 2020
1baaf87
Merge branch 'canary' of github.com:zeit/next.js into canary
janicklas-ralph Aug 14, 2020
68bff2a
Merge branch 'canary' of github.com:zeit/next.js into canary
janicklas-ralph Aug 24, 2020
5f3c00a
Merge branch 'canary' of github.com:zeit/next.js into canary
janicklas-ralph Sep 24, 2020
bceebd3
Merge branch 'canary' of github.com:janicklas-ralph/next.js; branch '…
janicklas-ralph Dec 4, 2020
b3921d3
Merge branch 'canary' of github.com:zeit/next.js into canary
janicklas-ralph Jan 7, 2021
9c6456f
Merge branch 'canary' of github.com:zeit/next.js into canary
janicklas-ralph Jan 29, 2021
7f37a0f
Enable font optimization by dafault
janicklas-ralph Jan 29, 2021
16b5cf2
Refactor test case
janicklas-ralph Jan 30, 2021
df6120d
Merge branch 'canary' of github.com:zeit/next.js into font
janicklas-ralph Feb 1, 2021
6c217d0
Merge branch 'canary' of github.com:zeit/next.js into font
janicklas-ralph Feb 23, 2021
e2b637c
Merge remote-tracking branch 'upstream/canary' into font
ijjk Mar 8, 2021
646edce
Update build-output test
ijjk Mar 8, 2021
064a9ab
Merge branch 'canary' of github.com:zeit/next.js into font
janicklas-ralph Mar 9, 2021
e1b801b
Moving optimizeFonts flag outside experimental and default true
janicklas-ralph Mar 9, 2021
1f724a3
Merge branch 'font' of github.com:janicklas-ralph/next.js into font
janicklas-ralph Mar 9, 2021
2a10bca
Remove flag in tests
janicklas-ralph Mar 9, 2021
a4f5193
Merge branch 'canary' of github.com:zeit/next.js into font
janicklas-ralph Mar 9, 2021
75cfad7
Merge remote-tracking branch 'upstream/canary' into font
ijjk Mar 16, 2021
0ad4bbe
Merge branch 'canary' of github.com:zeit/next.js into font
janicklas-ralph Mar 31, 2021
03e6eb6
Font bugfix. Handle failure to download fonts during build
janicklas-ralph Apr 1, 2021
9fe777d
Merge branch 'font' of github.com:janicklas-ralph/next.js into font
janicklas-ralph Apr 1, 2021
4457df9
Update error message
janicklas-ralph Apr 1, 2021
08c0f1d
Refactor minify css function
janicklas-ralph Apr 1, 2021
ff04f68
Fix lint
janicklas-ralph Apr 1, 2021
0afba62
Merge branch 'canary' of github.com:zeit/next.js into font
janicklas-ralph Apr 1, 2021
7dfea0c
Using log module to wrap message
janicklas-ralph Apr 1, 2021
cf771cd
Merge branch 'canary' of github.com:zeit/next.js into font
janicklas-ralph Apr 1, 2021
964a8bc
Merge branch 'canary' into font
ijjk Apr 5, 2021
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
2 changes: 1 addition & 1 deletion packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export default async function build(
BUILD_MANIFEST,
PRERENDER_MANIFEST,
REACT_LOADABLE_MANIFEST,
config.experimental.optimizeFonts
config.optimizeFonts
? path.join(
isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
FONT_MANIFEST
Expand Down
6 changes: 3 additions & 3 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ export default async function getBaseWebpackConfig(
config.experimental.reactMode
),
'process.env.__NEXT_OPTIMIZE_FONTS': JSON.stringify(
config.experimental.optimizeFonts && !dev
config.optimizeFonts && !dev
),
'process.env.__NEXT_OPTIMIZE_IMAGES': JSON.stringify(
config.experimental.optimizeImages
Expand Down Expand Up @@ -1141,7 +1141,7 @@ export default async function getBaseWebpackConfig(
distDir,
}),
new ProfilingPlugin(),
config.experimental.optimizeFonts &&
config.optimizeFonts &&
!dev &&
isServer &&
(function () {
Expand Down Expand Up @@ -1241,7 +1241,7 @@ export default async function getBaseWebpackConfig(
plugins: config.experimental.plugins,
reactStrictMode: config.reactStrictMode,
reactMode: config.experimental.reactMode,
optimizeFonts: config.experimental.optimizeFonts,
optimizeFonts: config.optimizeFonts,
optimizeImages: config.experimental.optimizeImages,
optimizeCss: config.experimental.optimizeCss,
scrollRestoration: config.experimental.scrollRestoration,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ export default async function exportApp(
subFolders,
buildExport: options.buildExport,
serverless: isTargetLikeServerless(nextConfig.target),
optimizeFonts: nextConfig.experimental.optimizeFonts,
optimizeFonts: nextConfig.optimizeFonts,
optimizeImages: nextConfig.experimental.optimizeImages,
optimizeCss: nextConfig.experimental.optimizeCss,
parentSpanId: pageExportSpan.id,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/next-server/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const defaultConfig: NextConfig = {
trailingSlash: false,
i18n: null,
productionBrowserSourceMaps: false,
optimizeFonts: true,
experimental: {
cpus: Math.max(
1,
Expand All @@ -73,7 +74,6 @@ export const defaultConfig: NextConfig = {
reactMode: 'legacy',
workerThreads: false,
pageEnv: false,
optimizeFonts: false,
optimizeImages: false,
optimizeCss: false,
scrollRestoration: false,
Expand Down
8 changes: 4 additions & 4 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ export default class Server {
ampOptimizerConfig: this.nextConfig.experimental.amp?.optimizer,
basePath: this.nextConfig.basePath,
images: JSON.stringify(this.nextConfig.images),
optimizeFonts: this.nextConfig.experimental.optimizeFonts && !dev,
optimizeFonts: this.nextConfig.optimizeFonts && !dev,
fontManifest:
this.nextConfig.experimental.optimizeFonts && !dev
this.nextConfig.optimizeFonts && !dev
? requireFontManifest(this.distDir, this._isLikeServerless)
: null,
optimizeImages: this.nextConfig.experimental.optimizeImages,
Expand Down Expand Up @@ -272,9 +272,9 @@ export default class Server {

/**
* This sets environment variable to be used at the time of SSR by head.tsx.
* Using this from process.env allows targetting both serverless and SSR by calling
* Using this from process.env allows targeting both serverless and SSR by calling
* `process.env.__NEXT_OPTIMIZE_IMAGES`.
* TODO(atcastle@): Remove this when experimental.optimizeImages are being clened up.
* TODO(atcastle@): Remove this when experimental.optimizeImages are being cleaned up.
*/
if (this.renderOpts.optimizeFonts) {
process.env.__NEXT_OPTIMIZE_FONTS = JSON.stringify(true)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/build-output/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ describe('Build Output', () => {
expect(parseFloat(indexFirstLoad)).toBeCloseTo(64.6, 1)
expect(indexFirstLoad.endsWith('kB')).toBe(true)

expect(parseFloat(err404Size) - 3.7).toBeLessThanOrEqual(0)
expect(parseFloat(err404Size)).toBeCloseTo(3.7, 1)
expect(err404Size.endsWith('kB')).toBe(true)

expect(parseFloat(err404FirstLoad)).toBeCloseTo(67.8, 0)
expect(err404FirstLoad.endsWith('kB')).toBe(true)

expect(parseFloat(sharedByAll)).toBeCloseTo(64.3, 1)
expect(parseFloat(sharedByAll)).toBeCloseTo(64.4, 1)
expect(sharedByAll.endsWith('kB')).toBe(true)

if (_appSize.endsWith('kB')) {
Expand Down
27 changes: 16 additions & 11 deletions test/integration/font-optimization/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
initNextServerScript,
} from 'next-test-utils'
import fs from 'fs-extra'
import cheerio from 'cheerio'

jest.setTimeout(1000 * 60 * 2)

Expand Down Expand Up @@ -54,13 +55,21 @@ function runTests() {

it('should pass nonce to the inlined font definition', async () => {
const html = await renderViaHTTP(appPort, '/nonce')
const $ = cheerio.load(html)
expect(await fsExists(builtPage('font-manifest.json'))).toBe(true)
expect(html).toContain(
'<link rel="stylesheet" nonce="VmVyY2Vs" data-href="https://fonts.googleapis.com/css2?family=Modak"/>'

const link = $(
'link[rel="stylesheet"][data-href="https://fonts.googleapis.com/css2?family=Modak"]'
)
expect(html).toMatch(
/<style data-href="https:\/\/fonts\.googleapis\.com\/css2\?family=Modak" nonce="VmVyY2Vs">.*<\/style>/
const nonce = link.attr('nonce')
const style = $(
'style[data-href="https://fonts.googleapis.com/css2?family=Modak"]'
)
const styleNonce = style.attr('nonce')

expect(link).toBeDefined()
expect(nonce).toBe('VmVyY2Vs')
expect(styleNonce).toBe('VmVyY2Vs')
})

it('should inline the google fonts for static pages with Next/Head', async () => {
Expand Down Expand Up @@ -117,11 +126,7 @@ function runTests() {

describe('Font optimization for SSR apps', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { experimental: {optimizeFonts: true} }`,
'utf8'
)
await fs.writeFile(nextConfig, `module.exports = {}`, 'utf8')

if (fs.pathExistsSync(join(appDir, '.next'))) {
await fs.remove(join(appDir, '.next'))
Expand All @@ -140,7 +145,7 @@ describe('Font optimization for serverless apps', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless', experimental: {optimizeFonts: true} }`,
`module.exports = { target: 'serverless' }`,
'utf8'
)
await nextBuild(appDir)
Expand All @@ -157,7 +162,7 @@ describe('Font optimization for emulated serverless apps', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'experimental-serverless-trace', experimental: {optimizeFonts: true} }`,
`module.exports = { target: 'experimental-serverless-trace' }`,
'utf8'
)
await nextBuild(appDir)
Expand Down