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 - Pass nonce to inlined font definition #21346

Merged
merged 21 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 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
9fd0e72
Remove warning message from postprocess step
janicklas-ralph Jan 7, 2021
eefc0fe
Fix lint
janicklas-ralph Jan 8, 2021
d61f7db
Merge branch 'canary' of github.com:zeit/next.js into font-optimization
janicklas-ralph Jan 11, 2021
9bec143
Remove unwanted code
janicklas-ralph Jan 11, 2021
ab489fb
Merge branch 'canary' into font-optimization
kodiakhq[bot] Jan 13, 2021
8cadfa4
Merge branch 'canary' of github.com:zeit/next.js into font-optimization
janicklas-ralph Jan 19, 2021
0038089
Pass nonce to inlined font definition
janicklas-ralph Jan 19, 2021
f514276
Merge branch 'font-optimization' of github.com:janicklas-ralph/next.j…
janicklas-ralph Jan 19, 2021
d2fea4b
Adding test case for nonce
janicklas-ralph Jan 20, 2021
33acbe5
Merge branch 'canary' of github.com:zeit/next.js into font-optimization
janicklas-ralph Jan 20, 2021
9163e4e
Merge branch 'canary' of github.com:zeit/next.js into font-optimization
janicklas-ralph Jan 26, 2021
7a3b6a3
Fix lint
janicklas-ralph Jan 26, 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
Prev Previous commit
Next Next commit
Adding test case for nonce
  • Loading branch information
janicklas-ralph committed Jan 20, 2021
commit d2fea4bb794df25972401fadaa19e2dd19f315b2
4 changes: 3 additions & 1 deletion packages/next/next-server/lib/post-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
.forEach((element: HTMLElement) => {
const url = element.getAttribute('data-href')
const nonce = element.getAttribute('nonce')

if (url) {
this.fontDefinitions.push([url, nonce])
}
Expand Down Expand Up @@ -144,9 +145,10 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
*/
result = result.replace('</head>', `${fallBackLinkTag}</head>`)
} else {
const nonceStr = nonce ? ` nonce="${nonce}"` : ''
result = result.replace(
'</head>',
`<style data-href="${url}" nonce="${nonce}">${fontContent}</style></head>`
`<style data-href="${url}"${nonceStr}>${fontContent}</style></head>`
)
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/integration/font-optimization/pages/nonce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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
11 changes: 11 additions & 0 deletions test/integration/font-optimization/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down