为啥我的反应网络应用程序返回一个白色/空白页面?

Posted

技术标签:

【中文标题】为啥我的反应网络应用程序返回一个白色/空白页面?【英文标题】:Why is my react web app returning a white/blank page?为什么我的反应网络应用程序返回一个白色/空白页面? 【发布时间】:2021-08-13 03:20:34 【问题描述】:

我一直在尝试托管一个使用 create-react-app 创建的 React Web 应用程序。我正在使用 Hostinger。我得到的只是这个白色/空白页。

在我的本地服务器上运行良好,在 github 页面上运行良好。 目前,我将 package.json 中的“主页”设置为正确的域。我还尝试将其设置为“。”正如一些人对其他类似问题的建议。

起初我在控制台上遇到 2 个错误:

    样式表未加载,因为它的 MIME 类型“text/html”不是“text/css”。 语法错误:预期的表达式,得到“

经过大量调整和重建后,我仍然得到白色/空白页面,但现在控制台为我提供了多个文件的 404 状态。它们是:

static/css/main.0d200d10.chunk.css static/js/2.f331c7cc.chunk.js static/js/main.0e5eb86e.chunk.js

它现在还为 2 个单独的文件提供 1 个警告:

源“/static/js/2.f331c7cc.chunk.js”的

这就是 index.html 的样子

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>Cachorro Sensível Filmes</title>
  </head>
  <body>
    <noscript>You need to enable javascript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

网站是https://cachorrosensivel.com.br

非常欢迎任何想法。

编辑:我回到重新上传 .htaccess 后提到的 2 个原始错误。如果我在脚本标签中指定“type=text/jsx”并从链接标签中删除“rel=stylesheet”,我不会收到任何错误,但它仍然是空白的。

我正在 Chrome 和 Firefox 上进行测试,并且我已经清除了缓存。

【问题讨论】:

一切都归结为 404 错误。您尚未部署整个应用程序,或者如果您部署了,则未部署到正确的 URL。 ""type=text/jsx" in my script tag" — 这告诉浏览器脚本是用它不理解的语言编写的,所以它根本不会下载它。它会阻止 404 错误,但会使问题变得更糟。 "remove "rel=stylesheet" from the link 标签" — 这告诉浏览器它不是样式表,所以它不会下载……同上 "目前,我将 package.json 中的“主页”设置为正确的域。" — 这告诉那些为你的 React 应用程序下载了 NPM 包的人项目主页在哪里。它与您在哪里部署使用该 React 应用程序构建的网站无关。 我运行 npm run build。 build 文件夹由单个文件和 3 个单独的文件夹(media、css、js)创建。我选择每个文件并将所有文件上传到 public_html。无论我缺少什么,我想都是在运行构建之前。 【参考方案1】:

我把上传到文件管理器的过程搞砸了。它进入 public_html -> 文件/静态文件夹 -> 静态文件夹 -> 文件。

【讨论】:

以上是关于为啥我的反应网络应用程序返回一个白色/空白页面?的主要内容,如果未能解决你的问题,请参考以下文章

为啥在部署我的项目后卡住/空白页面?反应路由器 Dom |参数

为啥 React Webpack 生产版本显示空白页面?

重新加载时反应Nodejs页面空白

为啥我的动态路由返回一个空白页?

为啥 IE 11 显示空白页渲染反应应用

为啥我的网页上jQuery没反应