You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除Semantic-UI CSS中的内嵌字体以适配CSP限制?

Fixing Semantic UI + Webpack Font CSP Issue (No Data URIs)

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/resource handling for this.
  • For Webpack 4: Install file-loader if 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-react without the full semantic-ui-css package, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:50:43