Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep query strings with AWS Amplify Hosting - React

I currently have a react app using react-router-dom that works locally but not when deployed using AWS Amplify.

The problem is that when I route to a URL and use query strings, the browser gets a redirect and drops the query strings.

For example when I route to /path?value1=foo&value2=bar, the browser gets a 301 and redirects to url: /0000000X/path/ as shown below

enter image description here

I have tried using hash values as well as the /path/?value1=foo&value2=bar approach as mentioned in this post with no luck.

Below is my implementation:

Button.js

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

Path.js

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

Routes.js

 <AuthenticatedRoute path="/path" exact component={Path} props={childProps} />

All of this works fine locally, redirects and I can access the query string params, but not in S3. Thanks in advance!

like image 635
rocketlobster Avatar asked Aug 23 '19 00:08

rocketlobster


1 Answers

Add this to AWS Amplify's Rewrites and Redirect:

[
    {
        "source": "</^((?!\\.(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$).)*$/>",
        "target": "/index.html",
        "status": "200",
        "condition": null
    },
    {
        "source": "/<*>",
        "target": "/",
        "status": "404",
        "condition": null
    }
]
like image 147
Ricardo Lozano Avatar answered Sep 28 '22 12:09

Ricardo Lozano