Unverified Commit dc2de370 authored by Janicklas Ralph's avatar Janicklas Ralph Committed by GitHub

Font optimization - Pass nonce to inlined font definition (#21346)

parent 004ad62d
...@@ -89,7 +89,7 @@ async function processHTML( ...@@ -89,7 +89,7 @@ async function processHTML(
} }
class FontOptimizerMiddleware implements PostProcessMiddleware { class FontOptimizerMiddleware implements PostProcessMiddleware {
fontDefinitions: Array<string> = [] fontDefinitions: (string | undefined)[][] = []
inspect( inspect(
originalDom: HTMLElement, originalDom: HTMLElement,
_data: postProcessData, _data: postProcessData,
...@@ -112,8 +112,10 @@ class FontOptimizerMiddleware implements PostProcessMiddleware { ...@@ -112,8 +112,10 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
) )
.forEach((element: HTMLElement) => { .forEach((element: HTMLElement) => {
const url = element.getAttribute('data-href') const url = element.getAttribute('data-href')
const nonce = element.getAttribute('nonce')
if (url) { if (url) {
this.fontDefinitions.push(url) this.fontDefinitions.push([url, nonce])
} }
}) })
} }
...@@ -127,7 +129,7 @@ class FontOptimizerMiddleware implements PostProcessMiddleware { ...@@ -127,7 +129,7 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
return markup return markup
} }
for (const key in this.fontDefinitions) { for (const key in this.fontDefinitions) {
const url = this.fontDefinitions[key] const [url, nonce] = this.fontDefinitions[key]
const fallBackLinkTag = `<link rel="stylesheet" href="${url}"/>` const fallBackLinkTag = `<link rel="stylesheet" href="${url}"/>`
if ( if (
result.indexOf(`<style data-href="${url}">`) > -1 || result.indexOf(`<style data-href="${url}">`) > -1 ||
...@@ -136,16 +138,17 @@ class FontOptimizerMiddleware implements PostProcessMiddleware { ...@@ -136,16 +138,17 @@ class FontOptimizerMiddleware implements PostProcessMiddleware {
// The font is already optimized and probably the response is cached // The font is already optimized and probably the response is cached
continue continue
} }
const fontContent = options.getFontDefinition(url) const fontContent = options.getFontDefinition(url as string)
if (!fontContent) { if (!fontContent) {
/** /**
* In case of unreachable font definitions, fallback to default link tag. * In case of unreachable font definitions, fallback to default link tag.
*/ */
result = result.replace('</head>', `${fallBackLinkTag}</head>`) result = result.replace('</head>', `${fallBackLinkTag}</head>`)
} else { } else {
const nonceStr = nonce ? ` nonce="${nonce}"` : ''
result = result.replace( result = result.replace(
'</head>', '</head>',
`<style data-href="${url}">${fontContent}</style></head>` `<style data-href="${url}"${nonceStr}>${fontContent}</style></head>`
) )
} }
} }
......
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
...@@ -52,6 +52,17 @@ function runTests() { ...@@ -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 () => { it('should inline the google fonts for static pages with Next/Head', async () => {
const html = await renderViaHTTP(appPort, '/static-head') const html = await renderViaHTTP(appPort, '/static-head')
expect(await fsExists(builtPage('font-manifest.json'))).toBe(true) expect(await fsExists(builtPage('font-manifest.json'))).toBe(true)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment