oblador / react-native-vector-icons

Customizable Icons for React Native with support for image source and full styling.
https://oblador.github.io/react-native-vector-icons/
MIT License
17.31k stars 2.12k forks source link

Fonts "downloadable font: rejected by sanitizer" or "Failed to decode downloaded font" #1594

Open jlounds opened 4 months ago

jlounds commented 4 months ago

Environment

Description

Web fonts for the icons are not being downloaded / interpreted by Firefox, Chrome, or Safari and only show the dreaded square box. (Firefox shows a Unicode number as well)

Firefox error message in developer tools console:

downloadable font: rejected by sanitizer (font-family: "MaterialCommunityIcons" style:normal weight:400 stretch:100 src index:0) source: http://localhost:19006/static/media/MaterialCommunityIcons.7527ec3ffabe608ca630.ttf

Chrome:

Failed to decode downloaded font: http://localhost:19006/static/media/MaterialCommunityIcons.7527ec3ffabe608ca630.ttf
OTS parsing error: invalid sfntVersion: 1702391919

I confirmed that the file in /static/media is being downloaded. In fact, I tried uploading it to https://transfonter.org/ to see if I could convert it to a different format and manually add it, but even that failed saying the "Font file is corrupted."

Code

I tried following the instructions in the readme, but I think Expo and/or the version of webpack is complicating things. But since I am new to all of this, I don't know how to narrow it down.

module.exports = async function (env, argv) { const config = await createExpoWebpackConfigAsync(env, argv); config.module.rules.push( { test: /.ttf$/, loader: "url-loader", include: path.resolve(__dirname, "node_modules/react-native-vector-icons"), } ); return config; };

 - Excerpt from App.web.js

import materialCommunityIconFont from 'react-native-vector-icons/Fonts/MaterialCommunityIcons.ttf'; const moreFonts = @font-face { src: url(${materialCommunityIconFont}); font-family: "MaterialCommunityIcons"; };

const moreFontsStyle = document.createElement('style'); moreFontsStyle.type = 'text/css';

if (moreFontsStyle.styleSheet) { moreFontsStyle.styleSheet.cssText = moreFonts; } else { moreFontsStyle.appendChild(document.createTextNode(moreFonts)); } document.head.appendChild(moreFontsStyle);

c-goettert commented 3 months ago

This stack-overflow answer led me to the solution that fixed it for me. In your webpack-config change this:

{
  test: /\.ttf$/,
  loader: "url-loader", // or directly file-loader
  include: path.resolve(__dirname, "node_modules/react-native-vector-icons"),
}

to this:

{
  test: /\.ttf$/,
  type: 'asset/resource',
  include: path.resolve(__dirname, "node_modules/react-native-vector-icons"),
}

This should probably also be updated in the readme.