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

Vite迁移React应用后出现index.js意外令牌及随机白屏问题求助

迁移React应用到Vite后随机出现白屏及意外令牌错误问题

最近将React应用从create-react-app迁移到Vite,迁移前未出现此类问题。当前问题表现为:

  • 随机出现白屏,桌面、移动设备的部分浏览器都可能触发
  • 清除浏览器缓存后页面可正常加载,但控制台会抛出index.js存在意外令牌的异常
  • 查阅StackOverflow和GitHub Issues相关帖子后,仍未解决问题

已执行的操作

  • 在vite.config.ts中设置base: "/"并配置构建输出目录
  • 在index.tsx顶部添加代码:window.global ||= window;
  • 将整个应用及认证后的大部分内容设置为懒加载

相关配置文件

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "types": ["vite/client", "vitest/globals"],
    "module": "esnext",
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "jsx": "react-jsx",
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "noFallthroughCasesInSwitch": true,
    "preserveWatchOutput": true
  },
  "include": [
    "src"
  ]
}

vite.config.ts

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  return {
    build: {
      outDir: "build",
      sourcemap: false
    },
    plugins: [
      react(),
      tsconfigPaths()
    ],
    server: {
      host: "localhost",
      port: 3000,
      proxy: {
        '/api': {
          target: env.VITE_APP_PROXY_ENDPOINT || 'https://localhost:7019',
          changeOrigin: true,
          secure: false
        }
      },
      headers: {
        "x-frame-options": "sameorigin",
        "x-content-type-options": "nosniff",
        'Cache-Control': 'public, max-age=43200'
      }
    },
    base: "/"
  }
});

IIS部署的web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReactRouter Routes" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_URI}" pattern="^/(docs)" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.html" />
                </rule>
            </rules>
        </rewrite>
        <httpProtocol>
            <customHeaders>
                //Security Headers Are Here with my Domain Configuration
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

补充说明

清除缓存会触发问题:新部署后页面可能正常加载,但清除缓存后就会出现白屏。

控制台意外令牌错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 20:57:04