使用React Route部署到S3后查看空白页面 [英] Seeing empty page after deployed to S3 with React Route

查看:285
本文介绍了使用React Route部署到S3后查看空白页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用React和React Router构建SPA。我也在使用 https://github.com/facebookincubator/create-react-app,因为它是一个非常简单的应用程序。当我使用webpack开发时,我可以看到页面正常。然而,在我使用 npm run build 从 create-react-app 生成生产后,我得到了HTML文件和css, js通常。我上传了所有内容到S3,但是当我进入页面时,我只能看到空白页面。



这就是我所看到的

 <! -  react-empty:1  - > 

我猜这是因为S3缺省为 index.html 我无法改变这一点。并且React Router不知道如何处理 index.html ,但我也有默认的 / root,但是我仍然看到一个空白页。不知道如何解决这个问题?



这是我的路由器

  ReactDOM.render(
< Router history = {browserHistory}>
< Route path =/component = {App}>
< IndexRoute component = {Home} / >
< Route path =question2component = {Question2} />
<路径路径=question3component = {Question3} />
< =谢谢组件= {谢谢} />
< / Route>
< / Router> ;,
document.getElementById('root')
);

这是模板 creawte-react-app

 <!doctype html> 
< html lang =en>
< head>
< meta charset =utf-8>
< meta name =viewportcontent =width = device-width,initial-scale = 1>
< link rel =快捷图标href =%PUBLIC_URL%/ favicon.ico>
<! -
注意上面标签中使用%PUBLIC_URL%。
在编译过程中将被替换为`public`文件夹的URL。
只有`public`文件夹内的文件可以从HTML中引用。

与/favicon.ico或favicon.ico不同,%PUBLIC_URL%/ favicon.ico将使
在客户端路由和非根公共URL。
学习如何通过运行`npm run build`来配置非根公用URL。
- >
< title> React App< / title>
< / head>
< body>
< div id =root>< / div>
<! -
这个HTML文件是一个模板。
如果您直接在浏览器中打开它,您将看到一个空白页面。

您可以将网页字体,元标记或分析添加到此文件。
构建步骤会将捆绑脚本放入< body>标签。

开始开发,运行`npm start`。
要创建一个产品包,请使用`npm run build`。
- >
< / body>
< / html>


解决方案

您正在尝试使用 browserHistory 在静态网页上。您应该使用 hashHistory 为静态页面。



为什么当您使用静态页面的浏览器历史?



当React Router首次安装时,它(实际上是它使用的 history 模块)检查当前URL以确定初始位置。使用 browserHistory ,这是域之后的任何内容,所以 example.com/index.html 的初始位置会 /index.html



如果您有 index.html ,当页面加载并且事情似乎可以正常工作时,这将匹配。如果你的应用有一个< Link> / other 路由,你甚至可以点击它,URL将改为 example.com/other

但是,由于您使用的是静态网页,因此无法链接到 example.com/other 。如果有人尝试加载该页面,他们将收到404错误,因为服务器没有 / other 页面来提供服务。



输入 hashHistory



当您使用 hashHistory

如果您导航到,那么在确定位置时要考虑的URL的唯一部分就是哈希后面的内容。

example.com/index.html 在使用 hashHistory 时,您会注意到URL已更改为 example / com /index.html#/ 。如果URL不包含一个,那么将为您插入散列并将其设置为根( / 的绝对路径)。



回到之前的例子,其中< Link> 链接到 / other ,该链接被点击后,网址将变为 example.com/index.html#/other 。现在,如果直接导航到该URL,服务器将加载 example.com/index.html ,React Router将检查散列,看它是#/ other 并将初始位置设置为 / other 路线。


I build a SPA with React and React Router. I'm also using https://github.com/facebookincubator/create-react-app as it's a really simple application. When I'm developing using webpack I can see the page fine. However, after I build for production using npm run build from create-react-app I get HTML file and css and js normally. I uploaded everything to S3 but when I go to the page I only get blank page

And this is what I'm seeing

<!-- react-empty: 1 -->

I'm guessing it's like this because S3 is default to index.html and I cannot change that. And React Router doesn't know how to deal with index.html but I also have / root as the default but I'm still seeing a blank page. Not sure how to fix this issue?

Here's my Router

ReactDOM.render(
  <Router history={browserHistory}>
    <Route path="/" component={App}>
      <IndexRoute component={Home} />
      <Route path="question2" component={Question2} />
      <Route path="question3" component={Question3} />
      <Route path="thankyou" component={Thankyou} />
    </Route>
  </Router>,
  document.getElementById('root')
);

And this is the template creawte-react-app use and it's working fine on development.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <!--
      Notice the use of %PUBLIC_URL% in the tag 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>React App</title>
  </head>
  <body>
    <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`.
      To create a production bundle, use `npm run build`.
    -->
  </body>
</html>

解决方案

You are attempting to use a browserHistory on a static web page. You should use a hashHistory for static pages.

Why happens when you use browser history for static pages?

When React Router is first mounting, it (actually the history module that it uses) examines the current URL to determine the initial location. With a browserHistory, this is anything after the domain, so example.com/index.html's initial location would be /index.html.

If you have a route for index.html, that will be matched when the page loads and things might appear to work. If your app has a <Link> to an /other route, you could even click on it and the URL would be changed to example.com/other.

However, because you are using a static web page, you could not link to example.com/other. If someone were to try to load that page, they would receive a 404 error because the server does not have an /other page to serve.

Enter hashHistory

When you use hashHistory, the only part of the URL that is considered when determining the location is what comes after the hash.

If you navigate to example.com/index.html while using a hashHistory, you will notice that the URL is changed to example/com/index.html#/. The hash is inserted for you, and set as the root (absolute path of /), if the URL doesn't include one.

Going back to the previous example where a <Link> links to /other, when that link is clicked the URL will change to example.com/index.html#/other. Now, if you navigate directly to that URL, the server will load example.com/index.html, React Router will examine the hash, see that it is #/other and set the initial location as the /other route.

这篇关于使用React Route部署到S3后查看空白页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆