使用 AWS Amplify Hosting 保留查询字符串 - React

roc*_*ter 10 amazon-s3 reactjs react-router aws-amplify

我目前有一个 React 应用程序react-router-dom,它可以在本地运行,但在使用AWS Amplify部署时却不能。

问题是,当我路由到 URL 并使用查询字符串时,浏览器会重定向并删除查询字符串。

例如,当我路由到 时/path?value1=foo&value2=bar,浏览器得到 301 并重定向到 url:/0000000X/path/如下所示

在此处输入图片说明

我已经使用哈希值以及久经考验/path/?value1=foo&value2=bar的中提到的方法这篇文章没有运气。

下面是我的实现:

按钮.js

href: '/path?value1=foo&value2=bar'

路径.js

import queryString from 'query-string'
const values = queryString.parse(this.props.location.search)
console.log(values.value1) 
console.log(values.value2) 
Run Code Online (Sandbox Code Playgroud)

路由.js

 <AuthenticatedRoute path="/path" exact component={Path} props={childProps} />
Run Code Online (Sandbox Code Playgroud)

所有这些都可以在本地正常工作,重定向,我可以访问查询字符串参数,但不能在 S3 中访问。提前致谢!

小智 6

将此添加到 AWS Amplify 的重写和重定向:

[
    {
        "source": "</^((?!\\.(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$).)*$/>",
        "target": "/index.html",
        "status": "200",
        "condition": null
    },
    {
        "source": "/<*>",
        "target": "/",
        "status": "404",
        "condition": null
    }
]
Run Code Online (Sandbox Code Playgroud)