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
相关产品推荐
相关产品推荐

