我的生产服务器的 create-react-app 加载缓慢

create-react-app loading slowly of my production server

我正在尝试通过 google.But 搜索这个问题,但对我没有帮助。 我有一个 create-react-app 的演示。 运行 它在本地主机 (npm start) 中,当浏览器禁用 cache.for 示例时效果很好: localhost

步骤是 运行 npm run build 在 localhost.then scp -r build server:/home/deploy/app/.运行 它由 nginx。 然后打开浏览器到运行,那个初始化或者禁用缓存时刷新很慢。例如: server

你会发现在服务器中加载 500KB js 文件需要 15 秒。

我猜这与带宽有关。我的服务器带宽是 1M/s.but 我不确定。

ps: 不好意思我忘记声明具体环境了,但是我做了这些steps.

您是否缺少 build 步骤?

如果是,请尝试 npm run buildyarn build。它会将您的应用程序的优化版本输出到 ./build 目录。然后,您可以使用 nginx 或其他服务器设置将其托管在您的服务器上。 更多信息在这里:official docs

当您执行 npm start 时,npm 会启动您应用的 开发版本 。 它包括一些调试代码、错误检查和实时刷新。

如果您运行在生产环境中使用它,我不建议您运行使用服务器上的 npm start 使用 Web 应用程序。

更好的解决方案是 运行 npm run-script build,这样您会得到这样的回复:

Creating an optimized production build...
Compiled successfully.

File sizes after gzip:

  48.12 KB  build/static/js/main.9fdf0e48.js
  288 B     build/static/css/main.cacbacc7.css

The project was built assuming it is hosted at the server root.
To override this, specify the homepage in your package.json.
For example, add this to build it for GitHub Pages:

  "homepage" : "http://myname.github.io/myapp",

The build folder is ready to be deployed.
You may serve it with a static server:

  sudo npm install -g serve
  serve -s build

您可以执行 serve -s build 或设置 nginx 或 apache 来提供文件(它只是 html、css 和 js)。您也可以使用 Github 页面来托管它。