Unverified Commit e8fcf8b5 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Add data attributes on @next/font usage (#45296)

Adds `data-next-font` data attribute to the preload tag if added by
`@next/font`.
```js
// Using `size-adjust` fallback font.
<link data-next-font="size-adjust" rel="preload" href="..." as="font" type="font/woff2" crossorigin="anonymous">

// Not using `size-adjust` fallback font.
<link data-next-font="" rel="preload" href="..." as="font" type="font/woff2" crossorigin="anonymous">
```

If no fonts are preloaded, the tag is added on the preconnect tag.

Fixes NEXT-350

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent fb62d7e8
......@@ -155,7 +155,8 @@ const downloadGoogleFonts: FontLoader = async ({
const selfHostedFileUrl = emitFontFile(
fontFileBuffer,
ext,
preloadFontFile
preloadFontFile,
!!adjustFontFallbackMetrics
)
return {
......
......@@ -75,7 +75,12 @@ const fetchFonts: FontLoader = async ({
src.map(async ({ path, style, weight, ext, format }) => {
const resolved = await resolve(path)
const fileBuffer = await promisify(loaderContext.fs.readFile)(resolved)
const fontUrl = emitFontFile(fileBuffer, ext, preload)
const fontUrl = emitFontFile(
fileBuffer,
ext,
preload,
typeof adjustFontFallback === 'undefined' || !!adjustFontFallback
)
let fontMetadata: any
try {
......
......@@ -11,7 +11,12 @@ export type FontLoader = (options: {
variableName: string
data: any[]
config: any
emitFontFile: (content: Buffer, ext: string, preload: boolean) => string
emitFontFile: (
content: Buffer,
ext: string,
preload: boolean,
isUsingSizeAdjust?: boolean
) => string
resolve: (src: string) => string
isDev: boolean
isServer: boolean
......
......@@ -59,12 +59,19 @@ export default async function nextFontLoader(this: any) {
})
)
const emitFontFile = (content: Buffer, ext: string, preload: boolean) => {
const emitFontFile = (
content: Buffer,
ext: string,
preload: boolean,
isUsingSizeAdjust?: boolean
) => {
const opts = { context: this.rootContext, content }
const interpolatedName = loaderUtils.interpolateName(
this,
// Font files ending with .p.(woff|woff2|eot|ttf|otf) are preloaded
`static/media/[hash]${preload ? '.p' : ''}.${ext}`,
`static/media/[hash]${isUsingSizeAdjust ? '-s' : ''}${
preload ? '.p' : ''
}.${ext}`,
opts
)
const outputPath = `${assetPrefix}/_next/${interpolatedName}`
......
......@@ -9,6 +9,8 @@ export type FontLoaderManifest = {
app: {
[moduleRequest: string]: string[]
}
appUsingSizeAdjust: boolean
pagesUsingSizeAdjust: boolean
}
const PLUGIN_NAME = 'FontLoaderManifestPlugin'
......@@ -50,6 +52,8 @@ export class FontLoaderManifestPlugin {
const fontLoaderManifest: FontLoaderManifest = {
pages: {},
app: {},
appUsingSizeAdjust: false,
pagesUsingSizeAdjust: false,
}
if (this.appDirEnabled) {
......@@ -60,6 +64,12 @@ export class FontLoaderManifestPlugin {
/\.(woff|woff2|eot|ttf|otf)$/.test(file)
)
if (!fontLoaderManifest.appUsingSizeAdjust) {
fontLoaderManifest.appUsingSizeAdjust = fontFiles.some((file) =>
file.includes('-s')
)
}
// Font files ending with .p.(woff|woff2|eot|ttf|otf) are preloaded
const preloadedFontFiles: string[] = fontFiles.filter(
(file: string) => /\.p.(woff|woff2|eot|ttf|otf)$/.test(file)
......@@ -86,6 +96,12 @@ export class FontLoaderManifestPlugin {
/\.(woff|woff2|eot|ttf|otf)$/.test(file)
)
if (!fontLoaderManifest.pagesUsingSizeAdjust) {
fontLoaderManifest.pagesUsingSizeAdjust = fontFiles.some((file) =>
file.includes('-s')
)
}
// Font files ending with .p.(woff|woff2|eot|ttf|otf) are preloaded
const preloadedFontFiles: string[] = fontFiles.filter(
(file: string) => /\.p.(woff|woff2|eot|ttf|otf)$/.test(file)
......
......@@ -382,7 +382,14 @@ function getFontLoaderLinks(
return {
preconnect: preconnectToSelf ? (
<link rel="preconnect" href="/" crossOrigin="anonymous" />
<link
data-next-font={
fontLoaderManifest.pagesUsingSizeAdjust ? 'size-adjust' : ''
}
rel="preconnect"
href="/"
crossOrigin="anonymous"
/>
) : null,
preload: preloadedFontFiles
? preloadedFontFiles.map((fontFile) => {
......@@ -395,6 +402,7 @@ function getFontLoaderLinks(
as="font"
type={`font/${ext}`}
crossOrigin="anonymous"
data-next-font={fontFile.includes('-s') ? 'size-adjust' : ''}
/>
)
})
......
......@@ -1492,7 +1492,14 @@ export async function renderToHTMLOrFlight(
return (
<>
{preloadedFontFiles?.length === 0 ? (
<link rel="preconnect" href="/" crossOrigin="anonymous" />
<link
data-next-font={
fontLoaderManifest?.appUsingSizeAdjust ? 'size-adjust' : ''
}
rel="preconnect"
href="/"
crossOrigin="anonymous"
/>
) : null}
{preloadedFontFiles
? preloadedFontFiles.map((fontFile) => {
......@@ -1505,6 +1512,9 @@ export async function renderToHTMLOrFlight(
as="font"
type={`font/${ext}`}
crossOrigin="anonymous"
data-next-font={
fontFile.includes('-s') ? 'size-adjust' : ''
}
/>
)
})
......
......@@ -223,23 +223,26 @@ createNextDescribe(
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/e9b9dc0d8ba35f48.p.woff2',
href: '/_next/static/media/e9b9dc0d8ba35f48-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
expect($('link[as="font"]').get(1).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/b61859a50be14c53.p.woff2',
href: '/_next/static/media/b61859a50be14c53-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
expect($('link[as="font"]').get(2).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/b2104791981359ae.p.woff2',
href: '/_next/static/media/b2104791981359ae-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
})
......@@ -254,24 +257,27 @@ createNextDescribe(
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/e9b9dc0d8ba35f48.p.woff2',
href: '/_next/static/media/e9b9dc0d8ba35f48-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
expect($('link[as="font"]').get(1).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/e1053f04babc7571.p.woff2',
href: '/_next/static/media/e1053f04babc7571-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
expect($('link[as="font"]').get(2).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/feab2c68f2a8e9a4.p.woff2',
href: '/_next/static/media/feab2c68f2a8e9a4-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
})
......@@ -286,17 +292,19 @@ createNextDescribe(
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/e9b9dc0d8ba35f48.p.woff2',
href: '/_next/static/media/e9b9dc0d8ba35f48-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
expect($('link[as="font"]').get(1).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/75c5faeeb9c86969.p.woff2',
href: '/_next/static/media/75c5faeeb9c86969-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
})
......@@ -311,17 +319,19 @@ createNextDescribe(
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/e9b9dc0d8ba35f48.p.woff2',
href: '/_next/static/media/e9b9dc0d8ba35f48-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
expect($('link[as="font"]').get(1).attribs).toEqual({
as: 'font',
crossorigin: '',
href: '/_next/static/media/568e4c6d8123c4d6.p.woff2',
href: '/_next/static/media/568e4c6d8123c4d6-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
})
})
......@@ -334,6 +344,12 @@ createNextDescribe(
// Preconnect
expect($('link[rel="preconnect"]').length).toBe(1)
expect($('link[rel="preconnect"]').get(0).attribs).toEqual({
crossorigin: 'anonymous',
href: '/',
rel: 'preconnect',
'data-next-font': 'size-adjust',
})
// Preload
expect($('link[as="font"]').length).toBe(0)
}
......
import localFont from '@next/font/local'
import { Open_Sans } from '@next/font/google'
const openSans = Open_Sans({
fallback: ['system-ui', 'Arial'],
......@@ -5,6 +6,12 @@ const openSans = Open_Sans({
adjustFontFallback: false,
})
const myFont = localFont({
fallback: ['system-ui', 'Arial'],
src: '../fonts/my-font.woff2',
adjustFontFallback: false,
})
export default function WithFonts() {
return (
<>
......@@ -21,6 +28,9 @@ export default function WithFonts() {
>
{JSON.stringify(openSans)}
</div>
<div id="with-fallback-local" className={myFont.className}>
{JSON.stringify(myFont)}
</div>
</>
)
}
......@@ -46,9 +46,10 @@ describe('@next/font/google basepath', () => {
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/dashboard/_next/static/media/0812efcfaefec5ea.p.woff2',
href: '/dashboard/_next/static/media/0812efcfaefec5ea-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
})
})
......@@ -306,16 +306,18 @@ describe('@next/font/google', () => {
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/0812efcfaefec5ea.p.woff2',
href: '/_next/static/media/0812efcfaefec5ea-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
expect($('link[as="font"]').get(1).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/675c25f648fd6a30.p.woff2',
href: '/_next/static/media/675c25f648fd6a30-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
})
......@@ -331,9 +333,10 @@ describe('@next/font/google', () => {
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/0812efcfaefec5ea.p.woff2',
href: '/_next/static/media/0812efcfaefec5ea-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
})
......@@ -351,11 +354,11 @@ describe('@next/font/google', () => {
.map((el) => el.attribs.href)
.sort()
).toEqual([
'/_next/static/media/02205c9944024f15.p.woff2',
'/_next/static/media/0812efcfaefec5ea.p.woff2',
'/_next/static/media/1deec1af325840fd.p.woff2',
'/_next/static/media/ab6fdae82d1a8d92.p.woff2',
'/_next/static/media/d55edb6f37902ebf.p.woff2',
'/_next/static/media/02205c9944024f15-s.p.woff2',
'/_next/static/media/0812efcfaefec5ea-s.p.woff2',
'/_next/static/media/1deec1af325840fd-s.p.woff2',
'/_next/static/media/ab6fdae82d1a8d92-s.p.woff2',
'/_next/static/media/d55edb6f37902ebf-s.p.woff2',
])
})
......@@ -374,14 +377,14 @@ describe('@next/font/google', () => {
.map((el) => el.attribs.href)
.sort()
).toEqual([
'/_next/static/media/0812efcfaefec5ea.p.woff2',
'/_next/static/media/4f3dcdf40b3ca86d.p.woff2',
'/_next/static/media/560a6db6ac485cb1.p.woff2',
'/_next/static/media/686d1702f12625fe.p.woff2',
'/_next/static/media/86d92167ff02c708.p.woff2',
'/_next/static/media/9ac01b894b856187.p.woff2',
'/_next/static/media/c9baea324111137d.p.woff2',
'/_next/static/media/fb68b4558e2a718e.p.woff2',
'/_next/static/media/0812efcfaefec5ea-s.p.woff2',
'/_next/static/media/4f3dcdf40b3ca86d-s.p.woff2',
'/_next/static/media/560a6db6ac485cb1-s.p.woff2',
'/_next/static/media/686d1702f12625fe-s.p.woff2',
'/_next/static/media/86d92167ff02c708-s.p.woff2',
'/_next/static/media/9ac01b894b856187-s.p.woff2',
'/_next/static/media/c9baea324111137d-s.p.woff2',
'/_next/static/media/fb68b4558e2a718e-s.p.woff2',
])
})
......@@ -398,11 +401,35 @@ describe('@next/font/google', () => {
// From _app
expect($('link[as="font"]').length).toBe(1)
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/0812efcfaefec5ea-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
})
test('font without size adjust', async () => {
const html = await renderViaHTTP(next.url, '/with-fallback')
const $ = cheerio.load(html)
expect($('link[as="font"]').get(1).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/0812efcfaefec5ea.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': '',
})
expect($('link[as="font"]').get(2).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/ab6fdae82d1a8d92.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': '',
})
})
})
......
......@@ -64,17 +64,19 @@ describe('@next/font/google with-font-declarations-file', () => {
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/0812efcfaefec5ea.p.woff2',
href: '/_next/static/media/0812efcfaefec5ea-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
// From /inter
expect($('link[as="font"]').get(1).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/4a7f86e553ee7e51.p.woff2',
href: '/_next/static/media/4a7f86e553ee7e51-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
}
})
......@@ -96,17 +98,19 @@ describe('@next/font/google with-font-declarations-file', () => {
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/0812efcfaefec5ea.p.woff2',
href: '/_next/static/media/0812efcfaefec5ea-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
// From /roboto
expect($('link[as="font"]').get(1).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/9a7e84b4dd095b33.p.woff2',
href: '/_next/static/media/9a7e84b4dd095b33-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
}
})
......@@ -128,17 +132,19 @@ describe('@next/font/google with-font-declarations-file', () => {
expect($('link[as="font"]').get(0).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/0812efcfaefec5ea.p.woff2',
href: '/_next/static/media/0812efcfaefec5ea-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
// From /local-font
expect($('link[as="font"]').get(1).attribs).toEqual({
as: 'font',
crossorigin: 'anonymous',
href: '/_next/static/media/2a931eed088772c9.p.woff2',
href: '/_next/static/media/2a931eed088772c9-s.p.woff2',
rel: 'preload',
type: 'font/woff2',
'data-next-font': 'size-adjust',
})
}
})
......
......@@ -49,6 +49,7 @@ describe('@next/font/google without-preloaded-fonts without _app', () => {
crossorigin: 'anonymous',
href: '/',
rel: 'preconnect',
'data-next-font': 'size-adjust',
})
// Preload
......@@ -108,6 +109,7 @@ describe('@next/font/google no preloads with _app', () => {
crossorigin: 'anonymous',
href: '/',
rel: 'preconnect',
'data-next-font': 'size-adjust',
})
// Preload
......@@ -124,6 +126,7 @@ describe('@next/font/google no preloads with _app', () => {
crossorigin: 'anonymous',
href: '/',
rel: 'preconnect',
'data-next-font': 'size-adjust',
})
// Preload
......
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