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
dc2de370
Unverified
Commit
dc2de370
authored
Jan 26, 2021
by
Janicklas Ralph
Committed by
GitHub
Jan 26, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Font optimization - Pass nonce to inlined font definition (#21346)
parent
004ad62d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
5 deletions
+38
-5
post-process.ts
packages/next/next-server/lib/post-process.ts
+8
-5
nonce.js
test/integration/font-optimization/pages/nonce.js
+19
-0
index.test.js
test/integration/font-optimization/test/index.test.js
+11
-0
No files found.
packages/next/next-server/lib/post-process.ts
View file @
dc2de370
...
...
@@ -89,7 +89,7 @@ async function processHTML(
}
class
FontOptimizerMiddleware
implements
PostProcessMiddleware
{
fontDefinitions
:
Array
<
string
>
=
[]
fontDefinitions
:
(
string
|
undefined
)[][]
=
[]
inspect
(
originalDom
:
HTMLElement
,
_data
:
postProcessData
,
...
...
@@ -112,8 +112,10 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
)
.
forEach
((
element
:
HTMLElement
)
=>
{
const
url
=
element
.
getAttribute
(
'data-href'
)
const
nonce
=
element
.
getAttribute
(
'nonce'
)
if
(
url
)
{
this
.
fontDefinitions
.
push
(
url
)
this
.
fontDefinitions
.
push
(
[
url
,
nonce
]
)
}
})
}
...
...
@@ -127,7 +129,7 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
return
markup
}
for
(
const
key
in
this
.
fontDefinitions
)
{
const
url
=
this
.
fontDefinitions
[
key
]
const
[
url
,
nonce
]
=
this
.
fontDefinitions
[
key
]
const
fallBackLinkTag
=
`<link rel="stylesheet" href="
${
url
}
"/>`
if
(
result
.
indexOf
(
`<style data-href="
${
url
}
">`
)
>
-
1
||
...
...
@@ -136,16 +138,17 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
// The font is already optimized and probably the response is cached
continue
}
const
fontContent
=
options
.
getFontDefinition
(
url
)
const
fontContent
=
options
.
getFontDefinition
(
url
as
string
)
if
(
!
fontContent
)
{
/**
* In case of unreachable font definitions, fallback to default link tag.
*/
result
=
result
.
replace
(
'</head>'
,
`
${
fallBackLinkTag
}
</head>`
)
}
else
{
const
nonceStr
=
nonce
?
` nonce="
${
nonce
}
"`
:
''
result
=
result
.
replace
(
'</head>'
,
`<style data-href="
${
url
}
">
${
fontContent
}
</style></head>`
`<style data-href="
${
url
}
"
${
nonceStr
}
>
${
fontContent
}
</style></head>`
)
}
}
...
...
test/integration/font-optimization/pages/nonce.js
0 → 100644
View file @
dc2de370
import
React
from
'react'
import
Head
from
'next/head'
const
Page
=
()
=>
{
return
(
<>
<
Head
>
<
link
href
=
"https://fonts.googleapis.com/css2?family=Modak"
rel
=
"stylesheet"
nonce
=
"VmVyY2Vs"
/>
<
/Head
>
<
div
>
Hi
!<
/div
>
<
/
>
)
}
export
default
Page
test/integration/font-optimization/test/index.test.js
View file @
dc2de370
...
...
@@ -52,6 +52,17 @@ function runTests() {
)
})
it
(
'should pass nonce to the inlined font definition'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/nonce'
)
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"/>'
)
expect
(
html
).
toMatch
(
/<style data-href="https:
\/\/
fonts
\.
googleapis
\.
com
\/
css2
\?
family=Modak" nonce="VmVyY2Vs">.*<
\/
style>/
)
})
it
(
'should inline the google fonts for static pages with Next/Head'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/static-head'
)
expect
(
await
fsExists
(
builtPage
(
'font-manifest.json'
))).
toBe
(
true
)
...
...
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