next.js: Unable to use self-hosted fonts using NextJS
I am compiling SCSS and externalizing the resulting CSS into a file using sass-loader and extract-text-webpack-plugin in next.config.js. The compiled CSS file is being emitted into the .next folder. I also have some static resources, i.e. images and fonts that I’m referencing in the stylesheet. These static resources are also being compiled into the .next folder using the file-loader plugin in next.config.js.
However, when the page loads in the browser, I get a 404 on these resources because NextJS seems to prepend their paths with a /_next/webpack/ in the compiled CSS file:
http://54.197.22.181/_next/webpack/52a802f51895f840821c1083434f3d84.ttf
Is there any workaround to this issue? I really need to use SASS and keep the compiled CSS as an external file as against embedded style. So, the CSS-in-JS model isn’t what I’m comfortable with.
For reference, my project is hosted at https://github.com/amitsch666/nano.
Here’s the relevant rule I have in my next.config.js:
{ test: /\.(ttf|eot|png)$/, loader: 'file-loader' }
Here’s the font declaration in my scss file:
@font-face {
font-family: 'Lemonada';
src: url('../static/fonts/Lemonada/Lemonada-Regular.ttf');
src: url('../static/fonts/Lemonada/Lemonada-Bold.ttf');
src: url('../static/fonts/Lemonada/Lemonada-SemiBold.ttf');
src: url('../static/fonts/Lemonada/Lemonada-Light.ttf');
}
- I have searched the issues of this repository and believe that this is not a duplicate.
Update: I have managed to make my site work by routing all _next/webpack/static/ requests to /.next/static in my Express middleware. However, this still seems a tad hackish to me and would love to hear if someone has a more “standard” solution.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 13
- Comments: 19 (2 by maintainers)
One question?
Cant we use urls like this?
https://github.com/rohanray/next-fonts … it supports both local file loaders and url loaders
ya totally! https://github.com/jay-manday/jm-site-react
@BjornMelgaard I had the same problem, a work around this is to just put your png’s and other assets in a
staticdirectory.In server.js put
server.use('/static', express.static('/next'))to create a static route to assets, this should circumvent the issue.yes you’d put your images, font files, etc in the folder
./static. The snippet i posted before will have express serve a static route that doesn’t hit webpack from my understanding. You can then import the images and fonts directly from their like any other file.See https://github.com/zeit/next.js/issues/512#issuecomment-364782832.