Repository: vercel/next.js
Pull request: fix(next/font): allow custom font-family in declarations

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

-->

### What?
This PR fixes an issue where the `font-family` declaration in `next/font/local` is ignored when specified in `declarations`.

### Why?
Currently, Next.js forces the generated variable name instead of respecting the provided `font-family`.  
This change ensures that when `font-family` is explicitly set in `declarations`, it is correctly applied in the generated `@font-face` CSS.

### How?
- Updated `loader.ts` to respect the `font-family` declaration if explicitly defined.
- Removed the restriction on `font-family` in `validate-local-font-function-call.ts`.
- Added tests:
  - `loader.test.ts`: Ensures the `font-family` is correctly used when declared.
  - `validate-local-font-function-call.test.ts`: Confirms that `font-family` is now an allowed declaration.

Closes NEXT-
Fixes #75898


Changed files:
filename: packages/font/src/local/loader.test.ts
patch: @@ -229,5 +229,36 @@ describe('next/font/local loader', () => {
         "
       `)
     })
+
+    test('Custom font-family in declarations', async () => {
+      const { css } = await nextFontLocalFontLoader({
+        functionName: '',
+        data: [
+          {
+            src: './my-font.woff2',
+            declarations: [{ prop: 'font-family', value: 'Custom Font' }],
+          },
+        ],
+        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: Custom Font;
+        src: url(/_next/static/media/my-font.woff2) format('woff2');
+        font-display: swap;
+        }
+        "
+      `)
+    })
   })
 })

filename: packages/font/src/local/loader.ts
patch: @@ -54,12 +54,17 @@ const nextFontLocalFontLoader: FontLoader = async ({
         console.error(`Failed to load font file: ${resolved}\n${e}`)
       }
 
+      // Check if `font-family` is explicitly defined in `declarations`
+      const hasCustomFontFamily = declarations?.some(
+        ({ prop }) => prop === 'font-family'
+      )
+
       // Get all values that should be added to the @font-face declaration
       const fontFaceProperties = [
         ...(declarations
           ? declarations.map(({ prop, value }) => [prop, value])
           : []),
-        ['font-family', variableName],
+        ...(hasCustomFontFamily ? [] : [['font-family', variableName]]),
         ['src', `url(${fontUrl}) format('${format}')`],
         ['font-display', display],
         ...(weight ?? defaultWeight

filename: packages/font/src/local/validate-local-font-function-call.test.ts
patch: @@ -44,6 +44,15 @@ describe('validateLocalFontFunctionCall', () => {
     ).toThrowErrorMatchingInlineSnapshot(`"Invalid declaration prop: \`src\`"`)
   })
 
+  test('Valid font-family declaration', async () => {
+    expect(() =>
+      validateLocalFontFunctionCall('', {
+        src: './font-file.woff2',
+        declarations: [{ prop: 'font-family', value: 'MyCustomFont' }],
+      })
+    ).not.toThrow()
+  })
+
   test('Empty src array', async () => {
     expect(() =>
       validateLocalFontFunctionCall('', {

filename: packages/font/src/local/validate-local-font-function-call.ts
patch: @@ -86,13 +86,9 @@ export function validateLocalFontFunctionCall(
   if (Array.isArray(declarations)) {
     declarations.forEach((declaration) => {
       if (
-        [
-          'font-family',
-          'src',
-          'font-display',
-          'font-weight',
-          'font-style',
-        ].includes(declaration?.prop)
+        ['src', 'font-display', 'font-weight', 'font-style'].includes(
+          declaration?.prop
+        )
       ) {
         nextFontError(`Invalid declaration prop: \`${declaration.prop}\``)
       }