如何移除Semantic-UI CSS中的内嵌字体以适配CSP限制?
Hey, let's get this sorted fast—your problem boils down to Semantic UI defaulting to embedding fonts as base64 data: URIs, which clashes with your deployment's Content Security Policy. The simplest fix is to configure Webpack to output font files as relative paths instead of embedding them. Here's the minimal toolset and steps:
1. Use Webpack's Built-in Asset Handling (or file-loader for older versions)
- For Webpack 5: You don't need extra packages—Webpack has built-in
asset/resourcehandling for this. - For Webpack 4: Install
file-loaderif you haven't already:npm install file-loader --save-dev
2. Update Your Webpack Config
Add or modify the rule for font files in your webpack.config.js to replace any existing url-loader setup that's generating base64:
module.exports = { // ... rest of your config module: { rules: [ // ... other loaders (JS, CSS, etc.) { test: /\.(woff|woff2|eot|ttf|otf)$/i, // Webpack 5: use this line type: 'asset/resource', // Webpack 4: replace the line above with this: // loader: 'file-loader', generator: { // Output fonts to a dedicated folder with hash for caching filename: 'fonts/[name].[hash:8].[ext]' } } ] } };
3. Verify Semantic UI CSS Processing
Make sure you're importing Semantic UI's CSS directly into your project (e.g., import 'semantic-ui-css/semantic.min.css') instead of using a CDN. Webpack will automatically rewrite the font references in the CSS to point to the relative path of the output font files.
Why This Is the Fastest Solution
- No fancy tools or complex overrides—just a single Webpack config change
- Works with your existing React/Semantic UI setup without touching business code
- Generates properly cached font files that fit your deployment's relative path requirements
Quick note: If you're using
semantic-ui-reactwithout the fullsemantic-ui-csspackage, double-check that your CSS source is being processed by Webpack (not a pre-built CDN file) to ensure the font paths get updated correctly.
内容的提问来源于stack exchange,提问作者Brian Jordan

