Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
1ea8bdcd
Unverified
Commit
1ea8bdcd
authored
Aug 04, 2020
by
Prateek Bhatnagar
Committed by
GitHub
Aug 04, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bug fix: Font optimization (#15644)
- Fixes the serverless build for font optimizations
parent
aef6726e
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
9 deletions
+38
-9
next-serverless-loader.ts
...ages/next/build/webpack/loaders/next-serverless-loader.ts
+5
-6
font-stylesheet-gathering-plugin.ts
...build/webpack/plugins/font-stylesheet-gathering-plugin.ts
+24
-3
index.test.js
test/integration/font-optimization/test/index.test.js
+4
-0
yarn.lock
yarn.lock
+5
-0
No files found.
packages/next/build/webpack/loaders/next-serverless-loader.ts
View file @
1ea8bdcd
...
@@ -6,7 +6,6 @@ import { loader } from 'webpack'
...
@@ -6,7 +6,6 @@ import { loader } from 'webpack'
import
{
API_ROUTE
}
from
'../../../lib/constants'
import
{
API_ROUTE
}
from
'../../../lib/constants'
import
{
import
{
BUILD_MANIFEST
,
BUILD_MANIFEST
,
FONT_MANIFEST
,
REACT_LOADABLE_MANIFEST
,
REACT_LOADABLE_MANIFEST
,
ROUTES_MANIFEST
,
ROUTES_MANIFEST
,
}
from
'../../../next-server/lib/constants'
}
from
'../../../next-server/lib/constants'
...
@@ -59,10 +58,6 @@ const nextServerlessLoader: loader.Loader = function () {
...
@@ -59,10 +58,6 @@ const nextServerlessLoader: loader.Loader = function () {
'/'
'/'
)
)
const
routesManifest
=
join
(
distDir
,
ROUTES_MANIFEST
).
replace
(
/
\\
/g
,
'/'
)
const
routesManifest
=
join
(
distDir
,
ROUTES_MANIFEST
).
replace
(
/
\\
/g
,
'/'
)
const
fontManifest
=
join
(
distDir
,
'serverless'
,
FONT_MANIFEST
).
replace
(
/
\\
/g
,
'/'
)
const
escapedBuildId
=
escapeRegexp
(
buildId
)
const
escapedBuildId
=
escapeRegexp
(
buildId
)
const
pageIsDynamicRoute
=
isDynamicRoute
(
page
)
const
pageIsDynamicRoute
=
isDynamicRoute
(
page
)
...
@@ -441,7 +436,11 @@ const nextServerlessLoader: loader.Loader = function () {
...
@@ -441,7 +436,11 @@ const nextServerlessLoader: loader.Loader = function () {
if (process.env.__NEXT_OPTIMIZE_FONTS) {
if (process.env.__NEXT_OPTIMIZE_FONTS) {
renderOpts.optimizeFonts = true
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
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)
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)
...
...
packages/next/build/webpack/plugins/font-stylesheet-gathering-plugin.ts
View file @
1ea8bdcd
...
@@ -18,6 +18,7 @@ interface VisitorMap {
...
@@ -18,6 +18,7 @@ interface VisitorMap {
export
class
FontStylesheetGatheringPlugin
{
export
class
FontStylesheetGatheringPlugin
{
compiler
?:
Compiler
compiler
?:
Compiler
gatheredStylesheets
:
Array
<
string
>
=
[]
gatheredStylesheets
:
Array
<
string
>
=
[]
manifestContent
:
FontManifest
=
[]
private
parserHandler
=
(
private
parserHandler
=
(
factory
:
CompilationType
.
NormalModuleFactory
factory
:
CompilationType
.
NormalModuleFactory
...
@@ -102,22 +103,42 @@ export class FontStylesheetGatheringPlugin {
...
@@ -102,22 +103,42 @@ export class FontStylesheetGatheringPlugin {
this
.
parserHandler
this
.
parserHandler
)
)
compiler
.
hooks
.
make
.
tapAsync
(
this
.
constructor
.
name
,
(
compilation
,
cb
)
=>
{
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
(
compilation
.
hooks
.
finishModules
.
tapAsync
(
this
.
constructor
.
name
,
this
.
constructor
.
name
,
async
(
_
:
any
,
modulesFinished
:
Function
)
=>
{
async
(
_
:
any
,
modulesFinished
:
Function
)
=>
{
const
fontDefinitionPromises
=
this
.
gatheredStylesheets
.
map
((
url
)
=>
const
fontDefinitionPromises
=
this
.
gatheredStylesheets
.
map
((
url
)
=>
getFontDefinitionFromNetwork
(
url
)
getFontDefinitionFromNetwork
(
url
)
)
)
let
manifestContent
:
FontManifest
=
[]
this
.
manifestContent
=
[]
for
(
let
promiseIndex
in
fontDefinitionPromises
)
{
for
(
let
promiseIndex
in
fontDefinitionPromises
)
{
manifestContent
.
push
({
this
.
manifestContent
.
push
({
url
:
this
.
gatheredStylesheets
[
promiseIndex
],
url
:
this
.
gatheredStylesheets
[
promiseIndex
],
content
:
await
fontDefinitionPromises
[
promiseIndex
],
content
:
await
fontDefinitionPromises
[
promiseIndex
],
})
})
}
}
compilation
.
assets
[
'font-manifest.json'
]
=
new
RawSource
(
compilation
.
assets
[
'font-manifest.json'
]
=
new
RawSource
(
JSON
.
stringify
(
manifestContent
,
null
,
' '
)
JSON
.
stringify
(
this
.
manifestContent
,
null
,
' '
)
)
)
modulesFinished
()
modulesFinished
()
}
}
...
...
test/integration/font-optimization/test/index.test.js
View file @
1ea8bdcd
...
@@ -82,6 +82,10 @@ describe('Font optimization for SSR apps', () => {
...
@@ -82,6 +82,10 @@ describe('Font optimization for SSR apps', () => {
`module.exports = { experimental: {optimizeFonts: true} }`
,
`module.exports = { experimental: {optimizeFonts: true} }`
,
'utf8'
'utf8'
)
)
if
(
fs
.
pathExistsSync
(
join
(
appDir
,
'.next'
)))
{
await
fs
.
remove
(
join
(
appDir
,
'.next'
))
}
await
nextBuild
(
appDir
)
await
nextBuild
(
appDir
)
appPort
=
await
findPort
()
appPort
=
await
findPort
()
app
=
await
nextStart
(
appDir
,
appPort
)
app
=
await
nextStart
(
appDir
,
appPort
)
...
...
yarn.lock
View file @
1ea8bdcd
...
@@ -7807,6 +7807,11 @@ he@1.1.1:
...
@@ -7807,6 +7807,11 @@ he@1.1.1:
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"
integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0=
integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0=
he@1.1.1:
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:
header-case@^1.0.0:
version "1.0.1"
version "1.0.1"
resolved "https://registry.yarnpkg.com/header-case/-/header-case-1.0.1.tgz#9535973197c144b09613cd65d317ef19963bd02d"
resolved "https://registry.yarnpkg.com/header-case/-/header-case-1.0.1.tgz#9535973197c144b09613cd65d317ef19963bd02d"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment