Skip to content

Conversation

jozsefsallai
Copy link

Validating local fonts fails with an "Unexpected file" error if the extension in the provided font file's path is not lowercase (e.g. "MYFONT.WOFF"). This PR converts the path into all lowercase before checking it with the regex. I decided not to use the case insensitive regex flag to avoid breaking later logic that uses the result of the check.

A new unit test was also included to test for case insensitivity.


Somewhat related suggestion: the error message is a bit vague at the moment and it's not clear that the error happens due to an unsupported extension. It took me quite a bit to debug the issue when I faced it and I would've never realized why it happens if I didn't take a look at the source code 😅 Would be neat if the error message specifically pointed this out (and maybe even displayed a list of supported extensions).

@ijjk ijjk added Font (next/font) Related to Next.js Font Optimization. type: next labels Sep 2, 2025
@ijjk
Copy link
Member

ijjk commented Sep 2, 2025

Allow CI Workflow Run

  • approve CI run for commit: a39c06e

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Comment on lines 74 to 76
const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(
fontFile.path?.toLowerCase()
)?.[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optional chaining on fontFile.path?.toLowerCase() protects against null/undefined values, but would still throw a TypeError if path is a non-string value (like a number or object). Consider adding type checking to handle all cases:

const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(
  typeof fontFile.path === 'string' ? fontFile.path.toLowerCase() : String(fontFile.path || '')
)?.[1]

This ensures the regex always runs against a string value, preventing potential runtime errors.

Suggested change
const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(
fontFile.path?.toLowerCase()
)?.[1]
const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(
typeof fontFile.path === 'string' ? fontFile.path.toLowerCase() : String(fontFile.path || '')
)?.[1]

Spotted by Diamond

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Font (next/font) Related to Next.js Font Optimization. type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants