The browser rejects fonts built by webpack with OTS parsing error
OTS parsing errorShort answer
OTS parsing error means the browser got a file that is not a valid font. In webpack 5, add one asset/resource rule for font extensions, check that output.publicPath points to where the font files are served, and stop an old file-loader rule from processing the same files again.
Fix it fast
1Find out which cause you have
Open the font URL from the browser's network tab in a new tab. If you see an HTML page or a 404, the file path is wrong. If the file downloads, check the build rule.
ls dist/ dist/fonts/2Match what you see, then jump to its fix
- 1
No font files in dist, or the build fails on the font importNo rule emits the font filesFix - 2
The font request returns an HTML page or a 404The public path points to the wrong placeFix - 3
Duplicated or broken font files after a webpack 5 upgradeAn old file-loader rule processes the fonts a second timeFix
- 1
What does the error look like?
OTS parsing error
Cause 1 of 3
No rule emits the font files
1Check
Webpack 5 handles files through asset modules. Without a rule for the font extensions, fonts are not emitted as files.
grep -n "woff\|ttf\|asset" webpack.config.js2Fix
Add an asset/resource rule that matches font formats. It emits each file and injects its URL into the bundle. Use generator.filename to put them in a folder.
{
test: /\.(woff2?|eot|ttf|otf)$/i,
type: 'asset/resource',
generator: {
filename: 'fonts/[hash][ext][query]',
},
},3Verify
Rebuild. The font files now exist in dist/fonts.
npx webpack
ls dist/fontsConfirmed by webpack.js.org
Cause 2 of 3
The public path points to the wrong place
1Check
Assets are loaded from output.publicPath joined with the file name. If the site serves the files from another URL, the browser downloads something that is not a font.
grep -n "publicPath" webpack.config.js2Fix
Set output.publicPath to the URL the assets are really served from.
output: {
publicPath: 'https://cdn.example.com/assets/',
},3Verify
Rebuild and request one font URL directly. It returns the file.
curl -I https://cdn.example.com/assets/fonts/example.woff2Confirmed by webpack.js.org
Cause 3 of 3
An old file-loader rule processes the fonts a second time
1Check
If you kept file-loader or url-loader during migration, Asset Modules process the same files again and can duplicate assets.
grep -n "file-loader\|url-loader" webpack.config.js package.json2Fix
Replace the old loader with asset/resource. If you must keep it for now, set type to javascript/auto on that rule.
{
test: /\.(woff2?|eot|ttf|otf)$/i,
use: ['file-loader'],
type: 'javascript/auto',
},3Verify
Rebuild and check that each font appears once in dist.
ls dist/ dist/fonts/Confirmed by webpack.js.org
How do I stop it coming back?
- Use asset modules, not file-loader and url-loader, in a new webpack 5 setup.
- Set output.publicPath explicitly for every environment.
- Open a font URL directly in the browser after the first deploy.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix