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

Bug fix: Font optimization #15644

Merged
merged 86 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
eff6343
adding a font gatherer webpack plugin
prateekbh Jun 15, 2020
9ed7962
fixing dep
prateekbh Jun 15, 2020
3b85140
Initial commit with framework for post processing middleware
atcastle Jun 18, 2020
3934f53
Merge branch 'post-process-optimization' of https://github.com/azukar…
prateekbh Jun 18, 2020
d5ed404
generating the style tags
prateekbh Jun 23, 2020
39fcfcd
WIP font optimizations
prateekbh Jun 24, 2020
8a932d1
working static pages font replacement
prateekbh Jun 25, 2020
126f904
minifying stylesheets
prateekbh Jun 25, 2020
069cbfa
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jun 25, 2020
27bdbfe
Merge branch 'canary' of https://github.com/zeit/next.js into post-pr…
prateekbh Jun 25, 2020
6a14652
Merge branch 'post-process-optimization' into font-optims
prateekbh Jun 25, 2020
d5137c3
WIP font-optimization changes
prateekbh Jun 30, 2020
7a79523
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jun 30, 2020
0f89a6c
adding comments
prateekbh Jun 30, 2020
2f45a7b
switching from filter to map
prateekbh Jun 30, 2020
6f9e1e7
fixing server path
prateekbh Jun 30, 2020
210a5cf
Fixing serverless builds
prateekbh Jun 30, 2020
ca93eba
reverting unwated change
prateekbh Jun 30, 2020
0bad5cb
removing commented code
prateekbh Jun 30, 2020
463ca85
adding experimental flag
prateekbh Jun 30, 2020
004ebcb
code resuability
prateekbh Jun 30, 2020
6acdce5
renaming flags
prateekbh Jun 30, 2020
2289980
renaming flags
prateekbh Jun 30, 2020
0d0c96d
guarding behind a flag
prateekbh Jul 1, 2020
45e7929
removing Promise.allSettled
prateekbh Jul 1, 2020
2602b31
Bug fixes and adding tests
prateekbh Jul 2, 2020
5cde0b2
sending experimental flags
prateekbh Jul 3, 2020
fcf567c
bug fixes and adding tests
prateekbh Jul 3, 2020
f70f936
adding serverless tests
prateekbh Jul 3, 2020
3ef9d55
minor improvement
prateekbh Jul 3, 2020
98e41c7
fixing experimental flag
prateekbh Jul 3, 2020
b8917c5
adding tests for serverless apps
prateekbh Jul 4, 2020
0dabf09
bug fix and adding tests for serverless
prateekbh Jul 4, 2020
9bfd563
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 4, 2020
e17d425
uniform logic for inert font stylesheets
prateekbh Jul 5, 2020
c397f6c
sending process.env value to SSR case
prateekbh Jul 5, 2020
5f7ddd1
updating lock file
prateekbh Jul 5, 2020
a0a9c14
Merge branch 'canary' into font-optims
prateekbh Jul 5, 2020
c7fe9e9
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 5, 2020
a21bba1
Merge branch 'font-optims' of https://github.com/azukaru/next.js into…
prateekbh Jul 5, 2020
d2be443
type fixes
prateekbh Jul 5, 2020
b7cd379
bug fix in test
prateekbh Jul 5, 2020
05cc713
bug fixes
prateekbh Jul 6, 2020
623a965
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 6, 2020
dc83f9d
fixing serializable value bug
prateekbh Jul 6, 2020
1e23ca3
vanity changes
prateekbh Jul 6, 2020
97f89fe
query bug fixes
prateekbh Jul 6, 2020
cb3ff87
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 6, 2020
3eaade2
moving ast types to dep
prateekbh Jul 6, 2020
6000fae
shifting font urls to a constant
prateekbh Jul 7, 2020
20ab602
removing dependency from recast
prateekbh Jul 7, 2020
91b5bb4
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 7, 2020
cb5e320
fixing flag name and adding tests
prateekbh Jul 8, 2020
cf4d9d1
Merge branch 'canary' into font-optims
prateekbh Jul 8, 2020
bf09fcb
addressing comments
prateekbh Jul 9, 2020
bd2f156
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 9, 2020
815d279
adding tests for emulated serverless builds
prateekbh Jul 15, 2020
58ad186
removes the need to fetch font from network in render and test fixes
prateekbh Jul 15, 2020
8de7630
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 15, 2020
6d600ab
fixing type issues
prateekbh Jul 15, 2020
477937b
Merge branch 'canary' into font-optims
prateekbh Jul 15, 2020
a462650
fix for ie 11
prateekbh Jul 16, 2020
3762863
Merge branch 'font-optims' of https://github.com/azukaru/next.js into…
prateekbh Jul 16, 2020
af2c9d6
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 16, 2020
45a90ed
removed the const due to tree shaking problem
prateekbh Jul 17, 2020
b3f7aea
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 17, 2020
03eb098
increasing the error.js size as it now has filter logic for font opti…
prateekbh Jul 17, 2020
7f5680a
Merge branch 'canary' into font-optims
prateekbh Jul 17, 2020
2d77d04
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 18, 2020
90f41fb
Update index.test.js
prateekbh Jul 20, 2020
0b6ee6b
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 20, 2020
eeb5375
Merge branch 'font-optims' of https://github.com/azukaru/next.js into…
prateekbh Jul 20, 2020
9f7f904
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 28, 2020
eb7f10e
Merge branch 'canary' into font-optims
prateekbh Jul 28, 2020
420ec28
fixing font manifest require for serverless
prateekbh Jul 29, 2020
da0de9d
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 29, 2020
ba73613
Merge branch 'font-optims' of https://github.com/azukaru/next.js into…
prateekbh Jul 29, 2020
52ed9ce
switching to fake module approach to inline font manifest
prateekbh Jul 30, 2020
b478785
Merge branch 'canary' of https://github.com/zeit/next.js into font-op…
prateekbh Jul 30, 2020
e658792
reverting changes from serverless loader
prateekbh Jul 30, 2020
f4b912b
more descriptive comments
prateekbh Jul 30, 2020
1157c84
Merge branch 'canary' into font-optims
prateekbh Aug 3, 2020
6d52b50
using webpackRequireFn to inline font definition
prateekbh Aug 3, 2020
faae463
Merge branch 'font-optims' of https://github.com/azukaru/next.js into…
prateekbh Aug 3, 2020
539f346
fixing tests
prateekbh Aug 4, 2020
3aa833a
Merge branch 'canary' into font-optims
prateekbh Aug 4, 2020
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
11 changes: 5 additions & 6 deletions packages/next/build/webpack/loaders/next-serverless-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { loader } from 'webpack'
import { API_ROUTE } from '../../../lib/constants'
import {
BUILD_MANIFEST,
FONT_MANIFEST,
REACT_LOADABLE_MANIFEST,
ROUTES_MANIFEST,
} from '../../../next-server/lib/constants'
Expand Down Expand Up @@ -59,10 +58,6 @@ const nextServerlessLoader: loader.Loader = function () {
'/'
)
const routesManifest = join(distDir, ROUTES_MANIFEST).replace(/\\/g, '/')
const fontManifest = join(distDir, 'serverless', FONT_MANIFEST).replace(
/\\/g,
'/'
)

const escapedBuildId = escapeRegexp(buildId)
const pageIsDynamicRoute = isDynamicRoute(page)
Expand Down Expand Up @@ -441,7 +436,11 @@ const nextServerlessLoader: loader.Loader = function () {

if (process.env.__NEXT_OPTIMIZE_FONTS) {
renderOpts.optimizeFonts = true
renderOpts.fontManifest = require('${fontManifest}')
/**
* __webpack_require__.__NEXT_FONT_MANIFEST__ is added by
* font-stylesheet-gathering-plugin
*/
renderOpts.fontManifest = __webpack_require__.__NEXT_FONT_MANIFEST__;
process.env['__NEXT_OPTIMIZE_FONT'+'S'] = true
}
let result = await renderToHTML(req, res, "${page}", Object.assign({}, getStaticProps ? { ...(parsedUrl.query.amp ? { amp: '1' } : {}) } : parsedUrl.query, nowParams ? nowParams : params, _params, isFallback ? { __nextFallback: 'true' } : {}), renderOpts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface VisitorMap {
export class FontStylesheetGatheringPlugin {
compiler?: Compiler
gatheredStylesheets: Array<string> = []
manifestContent: FontManifest = []

private parserHandler = (
factory: CompilationType.NormalModuleFactory
Expand Down Expand Up @@ -102,22 +103,42 @@ export class FontStylesheetGatheringPlugin {
this.parserHandler
)
compiler.hooks.make.tapAsync(this.constructor.name, (compilation, cb) => {
// @ts-ignore
if (compilation.options.output.path.endsWith('serverless')) {
/**
* Inline font manifest for serverless case only.
* For target: server drive the manifest through physical file and less of webpack magic.
*/
const mainTemplate = compilation.mainTemplate
mainTemplate.hooks.requireExtensions.tap(
this.constructor.name,
(source: string) => {
return `${source}
// Font manifest declaration
${
mainTemplate.requireFn
}.__NEXT_FONT_MANIFEST__ = ${JSON.stringify(
this.manifestContent
)};`
}
)
}
compilation.hooks.finishModules.tapAsync(
this.constructor.name,
async (_: any, modulesFinished: Function) => {
const fontDefinitionPromises = this.gatheredStylesheets.map((url) =>
getFontDefinitionFromNetwork(url)
)
let manifestContent: FontManifest = []

this.manifestContent = []
for (let promiseIndex in fontDefinitionPromises) {
manifestContent.push({
this.manifestContent.push({
url: this.gatheredStylesheets[promiseIndex],
content: await fontDefinitionPromises[promiseIndex],
})
}
compilation.assets['font-manifest.json'] = new RawSource(
JSON.stringify(manifestContent, null, ' ')
JSON.stringify(this.manifestContent, null, ' ')
)
modulesFinished()
}
Expand Down
4 changes: 4 additions & 0 deletions test/integration/font-optimization/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ describe('Font optimization for SSR apps', () => {
`module.exports = { experimental: {optimizeFonts: true} }`,
'utf8'
)

if (fs.pathExistsSync(join(appDir, '.next'))) {
await fs.remove(join(appDir, '.next'))
}
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7807,6 +7807,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"
integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0=

[email protected]:
version "1.1.1"
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"
integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0=

header-case@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/header-case/-/header-case-1.0.1.tgz#9535973197c144b09613cd65d317ef19963bd02d"
Expand Down