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

使用CSXJS、Vite和React创建Chrome扩展新项目时遇MIME类型错误

解决CSXJS创建Chrome扩展时的MIME类型错误

报错信息:

main.jsx:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

问题核心

Chrome扩展对ES模块的MIME类型校验严格,.jsx文件被识别为text/plain而非正确的application/javascript,导致模块加载失败。

修复步骤

  • 调整Manifest配置
    在manifest.json中,确保脚本入口指向CSXJS构建后的JS文件(而非原始.jsx),同时正确声明type: "module":

    {
      "background": {
        "service_worker": "dist/background.js",
        "type": "module"
      },
      "content_scripts": [
        {
          "matches": ["<all_urls>"],
          "js": ["dist/content.js"],
          "type": "module"
        }
      ]
    }
    
  • 修正CSXJS开发服务器MIME类型
    如果是开发模式下的问题,在CSXJS配置文件中添加MIME类型映射,确保.jsx文件以正确类型返回:

    // csxjs.config.js
    export default {
      server: {
        headers: {
          'Content-Type': 'application/javascript'
        }
      }
    }
    
  • 配置Web可访问资源
    若仍无效,在manifest.json中明确指定.jsx文件的MIME类型:

    {
      "web_accessible_resources": [
        {
          "resources": ["*.jsx"],
          "matches": ["<all_urls>"],
          "content_type": "application/javascript"
        }
      ]
    }
    
  • 优先使用编译后的产物
    避免直接加载未编译的.jsx源码,始终使用CSXJS构建输出的dist或build目录下的JS文件作为扩展脚本入口。

内容的提问来源于stack exchange,提问作者Abhishek kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 21:05:05