Unverified Commit e822c809 authored by Steven's avatar Steven Committed by GitHub

fix: add `?dpl` to fonts in `/_next/static/media` part 2 (#82488)

This is a follow up to PR https://github.com/vercel/next.js/pull/82384
which fixed it for `<link>` but not for `next/font` that embeds local
fonts in css.


https://nextjs.org/docs/14/app/building-your-application/deploying#version-skew
parent ee8286f1
...@@ -124,6 +124,7 @@ describe('next/font/google loader', () => { ...@@ -124,6 +124,7 @@ describe('next/font/google loader', () => {
mockFetchResource.mockResolvedValue(Buffer.from('OK')) mockFetchResource.mockResolvedValue(Buffer.from('OK'))
const { css } = await nextFontGoogleFontLoader({ const { css } = await nextFontGoogleFontLoader({
functionName, functionName,
deploymentId: undefined,
data: [ data: [
{ {
adjustFontFallback: false, adjustFontFallback: false,
......
...@@ -5,6 +5,7 @@ describe('next/font/local loader', () => { ...@@ -5,6 +5,7 @@ describe('next/font/local loader', () => {
test('Default CSS', async () => { test('Default CSS', async () => {
const { css } = await nextFontLocalFontLoader({ const { css } = await nextFontLocalFontLoader({
functionName: '', functionName: '',
deploymentId: undefined,
data: [{ src: './my-font.woff2' }], data: [{ src: './my-font.woff2' }],
emitFontFile: () => '/_next/static/media/my-font.woff2', emitFontFile: () => '/_next/static/media/my-font.woff2',
resolve: jest.fn(), resolve: jest.fn(),
...@@ -28,9 +29,37 @@ describe('next/font/local loader', () => { ...@@ -28,9 +29,37 @@ describe('next/font/local loader', () => {
`) `)
}) })
test('with dpl query string', async () => {
const { css } = await nextFontLocalFontLoader({
functionName: '',
deploymentId: 'dpl_123',
data: [{ src: './my-font.woff2' }],
emitFontFile: () => '/_next/static/media/my-font.woff2',
resolve: jest.fn(),
isDev: false,
isServer: true,
variableName: 'myFont',
loaderContext: {
fs: {
readFile: (_: string, cb: any) => cb(null, 'fontdata'),
},
} as any,
})
expect(css).toMatchInlineSnapshot(`
"@font-face {
font-family: myFont;
src: url(/_next/static/media/my-font.woff2?dpl=dpl_123) format('woff2');
font-display: swap;
}
"
`)
})
test('Weight and style', async () => { test('Weight and style', async () => {
const { css } = await nextFontLocalFontLoader({ const { css } = await nextFontLocalFontLoader({
functionName: '', functionName: '',
deploymentId: undefined,
data: [{ src: './my-font.woff2', weight: '100 900', style: 'italic' }], data: [{ src: './my-font.woff2', weight: '100 900', style: 'italic' }],
emitFontFile: () => '/_next/static/media/my-font.woff2', emitFontFile: () => '/_next/static/media/my-font.woff2',
resolve: jest.fn(), resolve: jest.fn(),
...@@ -59,6 +88,7 @@ describe('next/font/local loader', () => { ...@@ -59,6 +88,7 @@ describe('next/font/local loader', () => {
test('Other properties', async () => { test('Other properties', async () => {
const { css } = await nextFontLocalFontLoader({ const { css } = await nextFontLocalFontLoader({
functionName: '', functionName: '',
deploymentId: undefined,
data: [ data: [
{ {
src: './my-font.woff2', src: './my-font.woff2',
...@@ -95,6 +125,7 @@ describe('next/font/local loader', () => { ...@@ -95,6 +125,7 @@ describe('next/font/local loader', () => {
test('Multiple weights default style', async () => { test('Multiple weights default style', async () => {
const { css } = await nextFontLocalFontLoader({ const { css } = await nextFontLocalFontLoader({
functionName: '', functionName: '',
deploymentId: undefined,
data: [ data: [
{ {
style: 'italic', style: 'italic',
...@@ -171,6 +202,7 @@ describe('next/font/local loader', () => { ...@@ -171,6 +202,7 @@ describe('next/font/local loader', () => {
test('Multiple styles default weight', async () => { test('Multiple styles default weight', async () => {
const { css } = await nextFontLocalFontLoader({ const { css } = await nextFontLocalFontLoader({
functionName: '', functionName: '',
deploymentId: undefined,
data: [ data: [
{ {
weight: '400', weight: '400',
...@@ -233,6 +265,7 @@ describe('next/font/local loader', () => { ...@@ -233,6 +265,7 @@ describe('next/font/local loader', () => {
test('Custom font-family in declarations', async () => { test('Custom font-family in declarations', async () => {
const { css } = await nextFontLocalFontLoader({ const { css } = await nextFontLocalFontLoader({
functionName: '', functionName: '',
deploymentId: undefined,
data: [ data: [
{ {
src: './my-font.woff2', src: './my-font.woff2',
......
...@@ -19,6 +19,7 @@ const nextFontLocalFontLoader: FontLoader = async ({ ...@@ -19,6 +19,7 @@ const nextFontLocalFontLoader: FontLoader = async ({
data, data,
emitFontFile, emitFontFile,
resolve, resolve,
deploymentId,
loaderContext, loaderContext,
}) => { }) => {
const { const {
...@@ -45,6 +46,10 @@ const nextFontLocalFontLoader: FontLoader = async ({ ...@@ -45,6 +46,10 @@ const nextFontLocalFontLoader: FontLoader = async ({
preload, preload,
typeof adjustFontFallback === 'undefined' || !!adjustFontFallback typeof adjustFontFallback === 'undefined' || !!adjustFontFallback
) )
// Should match behavior in get-asset-query-string.ts
const qs = deploymentId
? `${fontUrl.includes('?') ? '&' : '?'}dpl=${deploymentId}`
: ''
// Try to load font metadata from the font file using fontkit. // Try to load font metadata from the font file using fontkit.
// The data is used to calculate the fallback font override values. // The data is used to calculate the fallback font override values.
...@@ -66,7 +71,7 @@ const nextFontLocalFontLoader: FontLoader = async ({ ...@@ -66,7 +71,7 @@ const nextFontLocalFontLoader: FontLoader = async ({
? declarations.map(({ prop, value }) => [prop, value]) ? declarations.map(({ prop, value }) => [prop, value])
: []), : []),
...(hasCustomFontFamily ? [] : [['font-family', variableName]]), ...(hasCustomFontFamily ? [] : [['font-family', variableName]]),
['src', `url(${fontUrl}) format('${format}')`], ['src', `url(${fontUrl + qs}) format('${format}')`],
['font-display', display], ['font-display', display],
...(weight ?? defaultWeight ...(weight ?? defaultWeight
? [['font-weight', weight ?? defaultWeight]] ? [['font-weight', weight ?? defaultWeight]]
......
...@@ -19,6 +19,7 @@ export type FontLoader = (options: { ...@@ -19,6 +19,7 @@ export type FontLoader = (options: {
resolve: (src: string) => string resolve: (src: string) => string
isDev: boolean isDev: boolean
isServer: boolean isServer: boolean
deploymentId: string | undefined
loaderContext: any loaderContext: any
}) => Promise<{ }) => Promise<{
css: string css: string
......
...@@ -2454,6 +2454,7 @@ export default async function getBaseWebpackConfig( ...@@ -2454,6 +2454,7 @@ export default async function getBaseWebpackConfig(
isEdgeRuntime: isEdgeServer, isEdgeRuntime: isEdgeServer,
targetWeb: isClient || isEdgeServer, targetWeb: isClient || isEdgeServer,
assetPrefix: config.assetPrefix || '', assetPrefix: config.assetPrefix || '',
deploymentId: config.deploymentId,
sassOptions: config.sassOptions, sassOptions: config.sassOptions,
productionBrowserSourceMaps: config.productionBrowserSourceMaps, productionBrowserSourceMaps: config.productionBrowserSourceMaps,
future: config.future, future: config.future,
......
...@@ -62,6 +62,7 @@ export function getNextFontLoader( ...@@ -62,6 +62,7 @@ export function getNextFontLoader(
isDev: ctx.isDevelopment, isDev: ctx.isDevelopment,
isServer: ctx.isServer, isServer: ctx.isServer,
assetPrefix: ctx.assetPrefix, assetPrefix: ctx.assetPrefix,
deploymentId: ctx.deploymentId,
fontLoaderPath, fontLoaderPath,
postcss, postcss,
}, },
......
...@@ -19,6 +19,7 @@ export async function buildConfiguration( ...@@ -19,6 +19,7 @@ export async function buildConfiguration(
isEdgeRuntime, isEdgeRuntime,
targetWeb, targetWeb,
assetPrefix, assetPrefix,
deploymentId,
sassOptions, sassOptions,
productionBrowserSourceMaps, productionBrowserSourceMaps,
future, future,
...@@ -36,6 +37,7 @@ export async function buildConfiguration( ...@@ -36,6 +37,7 @@ export async function buildConfiguration(
isEdgeRuntime: boolean isEdgeRuntime: boolean
targetWeb: boolean targetWeb: boolean
assetPrefix: string assetPrefix: string
deploymentId: string | undefined
sassOptions: any sassOptions: any
productionBrowserSourceMaps: boolean productionBrowserSourceMaps: boolean
transpilePackages: NextConfigComplete['transpilePackages'] transpilePackages: NextConfigComplete['transpilePackages']
...@@ -61,6 +63,7 @@ export async function buildConfiguration( ...@@ -61,6 +63,7 @@ export async function buildConfiguration(
? assetPrefix.slice(0, -1) ? assetPrefix.slice(0, -1)
: assetPrefix : assetPrefix
: '', : '',
deploymentId,
sassOptions, sassOptions,
productionBrowserSourceMaps, productionBrowserSourceMaps,
transpilePackages, transpilePackages,
......
...@@ -19,6 +19,7 @@ export type ConfigurationContext = { ...@@ -19,6 +19,7 @@ export type ConfigurationContext = {
targetWeb: boolean targetWeb: boolean
assetPrefix: string assetPrefix: string
deploymentId: string | undefined
sassOptions: any sassOptions: any
productionBrowserSourceMaps: boolean productionBrowserSourceMaps: boolean
......
...@@ -45,6 +45,7 @@ export default async function nextFontLoader(this: any) { ...@@ -45,6 +45,7 @@ export default async function nextFontLoader(this: any) {
assetPrefix, assetPrefix,
fontLoaderPath, fontLoaderPath,
postcss: getPostcss, postcss: getPostcss,
deploymentId,
} = this.getOptions() } = this.getOptions()
if (assetPrefix && !/^\/|https?:\/\//.test(assetPrefix)) { if (assetPrefix && !/^\/|https?:\/\//.test(assetPrefix)) {
...@@ -66,7 +67,7 @@ export default async function nextFontLoader(this: any) { ...@@ -66,7 +67,7 @@ export default async function nextFontLoader(this: any) {
* NextFontManifestPlugin uses this to see if fallback fonts are being used. * NextFontManifestPlugin uses this to see if fallback fonts are being used.
* This is used to collect stats on fallback fonts usage by the Google Aurora team. * This is used to collect stats on fallback fonts usage by the Google Aurora team.
*/ */
const emitFontFile = ( const emitFontFile: Parameters<FontLoader>[0]['emitFontFile'] = (
content: Buffer, content: Buffer,
ext: string, ext: string,
preload: boolean, preload: boolean,
...@@ -109,6 +110,7 @@ export default async function nextFontLoader(this: any) { ...@@ -109,6 +110,7 @@ export default async function nextFontLoader(this: any) {
), ),
isDev, isDev,
isServer, isServer,
deploymentId,
loaderContext: this, loaderContext: this,
}) })
) )
......
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