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

CRA PWA TypeScript生产环境Service Worker混合内容错误求助

问题

我通过npx create-react-app my-app --template cra-template-pwa-typescript创建了一个默认的React PWA项目,仅修改了index.tsx中的一行代码——将serviceWorkerRegistration.unregister();改为serviceWorkerRegistration.register();。

本地环境下,执行npm run build后用serve -s build运行完全正常,但部署到生产环境后,加载service worker时出现了混合内容错误:

Mixed Content: The page at 'https://example.com/service-worker.js' was loaded over HTTPS, but requested an insecure resource 'http://example.com/index'. This request has been blocked; the content must be served over HTTPS.

本地使用HTTP协议时没有这个问题,我已经尝试过以下操作:

  • 修改manifest.json中的scope和start_url字段
  • 修改serviceWorkerRegistration.tsx中的注册scope为"/"
  • 验证process.env.PUBLIC_URL输出的地址正确

我的Nginx配置如下:

server {
        listen 80;
        server_name example.com;
        location / {
                proxy_pass http://localhost:3000;
                proxy_redirect / $scheme://$http_host/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_read_timeout 20d;
                proxy_buffering off;
        }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 22:13:10