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

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

问题描述

我使用 React 和 React Router 构建了一个 SPA.我也在使用 https://github.com/facebookincubator/create-react-app 因为它是一个非常简单的应用程序.当我使用 webpack 进行开发时,我可以很好地看到页面.但是,在我使用 npm run buildcreate-react-app 为生产构建后,我通常会得到 HTML 文件以及 css 和 js.我把所有东西都上传到了 S3,但是当我去页面时,我只看到空白页面

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

这就是我看到的

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

我猜是这样的,因为 S3 默认为 index.html 而我无法更改它.而且 React Router 不知道如何处理 index.html 但我也有 / 根作为默认值,但我仍然看到一个空白页面.不知道如何解决这个问题?

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?

这是我的路由器

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')
);

这是 creawte-react-app 使用的模板,它在开发中运行良好.

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>

推荐答案

您正试图在静态网页上使用 browserHistory.您应该使用 hashHistory 用于静态页面.

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

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

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.

如果你有一个 index.html 的路由,它会在页面加载时匹配并且看起来工作正常.如果你的应用有一个 到一个 /other 路由,你甚至可以点击它,URL 将更改为 example.com/other.

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.

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

当您使用 hashHistory 时,在确定位置时唯一考虑的 URL 部分是哈希之后的部分.

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

如果您在使用 hashHistory 时导航到 example.com/index.html,您会注意到 URL 已更改为 example/com/index.html#/.为您插入哈希,并设置为根(/ 的绝对路径),如果 URL 不包含一个.

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.

回到前面的示例,其中 链接到 /other,当点击该链接时,URL 将更改为 example.com/index.html#/other.现在,如果您直接导航到该 URL,服务器将加载 example.com/index.html,React Router 将检查哈希,看到它是 #/other并将初始位置设置为 /other 路线.

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天全站免登陆