Resolve missing fonts in Webpack and React by ensuring proper CSS @font-face declarations, verifying file paths in your css-loader configuration, and handling font weight subsets correctly. This guide addresses common pitfalls where browsers fail to load custom typefaces despite correct imports.
Understanding the Font Loading Pipeline
When you integrate custom fonts into a React application managed by Webpack, the process involves several layers of abstraction. Unlike simple HTML projects where you link directly to a font file, modern builds require Webpack to treat font files as assets that must be processed, copied, or inlined. The most common failure point occurs when the CSS rule referencing the font cannot locate the actual binary file generated during the build process. Webpack’s module system treats different file types differently. If your CSS references a font using a relative path like../fonts/my-font.woff2, Webpack must know how to handle that extension. By default, older configurations might not have an explicit rule for font files, causing the import to fail silently or throw a module resolution error. Modern Webpack versions (5+) include built-in asset modules, but misconfiguration here is the primary cause of "fonts not connecting."
You must ensure that your webpack.config.js includes a rule that matches font extensions such as .woff, .woff2, .ttf, and .eot. Without this, the browser receives a CSS file that points to a non-existent URL, resulting in the fallback font being displayed instead of your intended typeface.
Configuring Asset Modules
In Webpack 5, the recommended approach is to use Asset Modules. You should add a rule to your configuration that identifies font files and emits them into your output directory. This ensures that the compiled CSS contains valid URLs pointing to the emitted files.module.exports = {
// ... other config
module: {
rules: [
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
generator: {
filename: 'static/fonts/[name][hash][ext]'
}
},
// ... other rules
]
}
};
This configuration tells Webpack to copy any matched font file to the static/fonts folder within your build output, appending a hash to prevent caching issues. The key here is that the type: 'asset/resource' directive handles the emission automatically, removing the need for external plugins like file-loader which are deprecated in newer versions.
Verifying CSS @font-face Declarations
Even with correct Webpack configuration, your CSS must correctly define the font family. A common mistake is omitting theformat() descriptor or using incorrect source URLs. When you write @font-face, you are mapping a logical name (like 'MyCustomFont') to physical files.
Ensure that your CSS uses absolute paths relative to the CSS file's location if you are importing fonts via JavaScript, or rely on Webpack’s URL rewriting. If you are using css-loader, it will automatically resolve relative paths in url() directives within your CSS. However, if you are manually writing paths, you must ensure they align with the final structure of your dist or build folder.
If you are experiencing issues similar to those described in Deployment to Vercel Failed: Troubleshooting JS, CSS, and Asset Folders, check whether your base path is set correctly. On static hosting platforms, the root path might differ from your local development environment, causing font URLs to break if they are hardcoded incorrectly.
Handling Subset Fonts and Weights
Modern web fonts often come in subsets to reduce file size. If you are using a service like Google Fonts or a custom subset generator, you might encounter issues where only certain weights or character sets load. This is often due to theunicode-range property in your @font-face declaration.
If your CSS specifies a narrow unicode range, the browser will only fetch the font if the text on the page falls within that range. For debugging, temporarily remove the unicode-range property to see if the font loads fully. If it does, the issue is likely with the subset generation rather than Webpack itself.
Additionally, ensure that you are loading all necessary weights. A common error is defining font-weight: 700 in your styles but only providing a 400 weight in your @font-face rules. Browsers will not substitute a bold font with a regular one if the specific weight is missing; they will simply fall back to the default system font.
Debugging Common Symptoms
When fonts appear to "not connect," the symptoms can be subtle. The text might render in a different font entirely, or there might be a brief flash of unstyled text (FOIT) before the font loads. To diagnose these issues, follow these steps:- Check Network Tab: Open your browser’s developer tools and go to the Network tab. Filter by "Font" or look for 404 errors. If you see requests for font files returning 404, the path in your CSS is incorrect or Webpack failed to emit the file.
- Inspect Computed Styles: Select the element with the custom font and check the Computed tab. Look at the
font-familyproperty. If it shows a generic fallback likesans-serifinstead of your custom name, the@font-facerule was not applied successfully. - Verify MIME Types: Ensure your server or local dev server serves font files with the correct MIME types (
font/woff2,font/ttf). Incorrect headers can block font loading in strict security modes.
Frequently Asked Questions
Why do my fonts work locally but break after deployment?
This usually happens because the base path for assets changes between development and production. Ensure your Webpack configuration uses a public path that matches your deployment target, or use relative URLs in your CSS that resolve correctly regardless of the domain.
Can I use inline SVGs instead of font files?
Yes, using inline SVGs or icon fonts like FontAwesome can bypass some Webpack asset issues. However, for standard typography, @font-face remains the standard. Inline SVGs are better suited for icons rather than body text due to accessibility and scalability constraints.
How do I fix FOIT (Flash of Invisible Text)?
Add font-display: swap; to your @font-face declarations. This tells the browser to display the fallback font immediately and swap in the custom font once it has finished downloading, preventing invisible text during the load process.
What if I am using Create React App (CRA)?
If you are using CRA, you do not need to configure Webpack directly. Instead, place your font files in the public folder and reference them using absolute paths in your CSS (e.g., /fonts/myfont.woff2). CRA handles the rest automatically.
Why are my font weights not applying correctly?
Check that your @font-face rule explicitly defines the font-weight matching the value used in your CSS. If you use font-weight: 700, you must have a corresponding @font-face with font-weight: 700. Browsers do not fake boldness unless configured to do so.