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

使用create-react-app+Express部署时Service Worker注册MIME类型错误求助

Fixing "Unsupported MIME type ('text/html')" for Create React App Service Worker with Express

Hey there, let's get that service worker registration error sorted out! This is a super common hiccup when deploying a Create React App (CRA) with an Express backend, and it almost always boils down to how your server is serving static files or handling routes. Here's how to fix it step by step:

1. Double-Check Your Static File Setup

First, make sure your Express server is correctly pointing to CRA's built static files. Your code should include something like this, where the path points to your CRA project's build folder:

const express = require('express');
const path = require('path');
const app = express();

// Serve static assets from the CRA build directory
app.use(express.static(path.join(__dirname, 'client/build')));

If the path is incorrect (e.g., you mixed up folder names), the server won't find service-worker.js and will fall back to returning an HTML file (like your index.html or 404 page) instead—this is exactly what causes the MIME type mismatch.

2. Fix the Catch-All Route Order

Most people add a catch-all route to let React Router handle client-side routing, but if this route comes before your static file setup, it will "catch" the request for service-worker.js and return HTML instead of the actual script.

Wrong order (causes the error):

// ❌ Catch-all route first - blocks static file requests
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});

app.use(express.static(path.join(__dirname, 'client/build')));

Correct order:

// ✅ Serve static files first
app.use(express.static(path.join(__dirname, 'client/build')));

// Then handle client-side routing with catch-all
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});

This way, when the browser requests /service-worker.js, the static middleware finds and serves the correct file before the catch-all route kicks in.

3. Verify MIME Type Configuration (Optional)

Express's express.static usually sets the correct MIME type automatically, but if you're still having issues, you can explicitly enforce the right type for .js files using the mime-types package:

const mime = require('mime-types');

// Add this before your static middleware
app.use((req, res, next) => {
  if (req.path.endsWith('.js')) {
    res.setHeader('Content-Type', mime.lookup('.js'));
  }
  next();
});

app.use(express.static(path.join(__dirname, 'client/build')));

This is a backup step—most of the time, fixing the route order will resolve the problem on its own.

4. Clear Browser Cache and Test Again

Browsers love to cache old responses, so even after fixing your server, you might still see the error until you clear cached files. In Firefox:

  • Press Ctrl + Shift + Delete (or Cmd + Shift + Delete on Mac)
  • Select "Cached Web Content" and click "Clear Now"
  • Test your app in a private window to avoid any leftover cache

Wrap-Up

The root cause here is that your server was returning an HTML file instead of the actual service-worker.js script, hence the "text/html" MIME type error. Adjusting your Express route order and ensuring static files are served correctly should get your service worker registered without issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:03:52