toy*_*toy 5 html javascript amazon-s3 reactjs react-router
我使用 React 和 React Router 构建了一个 SPA。我还使用https://github.com/facebookincubator/create-react-app因为它是一个非常简单的应用程序。当我使用 webpack 进行开发时,我可以很好地看到页面。npm run build但是,在我使用from进行生产构建后create-react-app我通常会获得 HTML 文件、css 和 js。我将所有内容上传到 S3,但是当我转到该页面时,我只看到空白页面
这就是我所看到的
<!-- react-empty: 1 -->
Run Code Online (Sandbox Code Playgroud)
我猜是这样的,因为 S3 是默认的index.html,我无法更改它。React Router 不知道如何处理index.html,但我也将/root 作为默认值,但我仍然看到一个空白页面。不知道如何解决这个问题?
这是我的路由器
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')
);
Run Code Online (Sandbox Code Playgroud)
这就是模板的creawte-react-app使用,并且在开发中运行良好。
<!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>
Run Code Online (Sandbox Code Playgroud)
browserHistory您正尝试在静态网页上使用 a 。您应该将 a 用于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如果您在使用 a 时导航到hashHistory,您会注意到 URL 已更改为example/com/index.html#/。/如果 URL 不包含哈希值,则会为您插入哈希值并将其设置为根( 的绝对路径)。
回到前面的示例,其中<Link>链接到/other,当单击该链接时,URL 将更改为example.com/index.html#/other。现在,如果您直接导航到该 URL,服务器将加载example.com/index.html,React Router 将检查哈希值,查看它是否正确#/other,并将初始位置设置为路由/other。