• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
IT-nose
博客园    首页    新随笔    联系   管理    订阅  订阅

react使用create-react-app创建的项目部署

一、在所有的项目代码编写完成后,react项目直接部署是无法正常访问的

    1、问题一

          网页无法正常的浏览器刷新,刷新会报404错,路由找不到页面

    2、问题二

          路由跳转后,浏览器后退按钮点击后,页面不刷新

    3、问题三

          使用HashRouter的react项目会在路由的时候在路径上添加/#,所以尽量使用BrowserRouter。

二、正确部署

     1、在public文件夹下添加.htaccess文件,用记事本或编辑器打开,将下列代码拷贝到文件中,并保存

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt|svg|woff|ttf|eot)$
RewriteRule . index.html [L]

 2、在react项目的根目录使用  npm run-script build打包文件,显示如下信息为打包成功

Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.

File sizes after gzip:

  118.12 KB  build\static\js\main.781fed84.js
  60.21 KB   build\static\css\main.4ef2f659.css

The project was built assuming it is hosted at the server root.
You can control this with the homepage field 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:

  yarn global add serve
  serve -s build

Find out more about deployment here:

  http://bit.ly/2vY88Kr

  3、最后将打包后生成的build文件部署到服务器上

参考链接:链接一,react项目部署

                  链接二,react项目部署

posted @ 2018-11-27 16:58  detanx_墨鼬漓  阅读(3134)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3