'How to serve apple-app-site-association file on /apple-app-site-association page in ReactJS
I'm having a lot of trouble with serving a apple-app-site-association file in ReactJS project.
I've searched a lot of GitHub issues pages (for ReactJS, Gatsby, and Angular), forums, and a lot of online communities, and it seems that I can't find a solution for this.
What I've tried is:
- Adding the file into public/.well-known folder.
- Adding a separate route via react-router on path "/apple-app-site-association" and returning an tag with file
- Adding
<link rel="apple-app-site-association" href="%PUBLIC_URL%/.well-known/apple-app-site-association">
into public/index.html
Testing through the "aasa-validator" returns:
Your file's 'content-type' header was not found or was not recognized.
Keep in mind that:
- The apple-app-site-association JSON file must not have a .json file extension.
- It has to be on "/apple-app-site-association" or "./well-known/apple-app-site-association" links on the website.
- I can't use a redirect to another page/link.
Thanks in advance!
Ps. If it helps, I'm using a Heroku for deployment.
Solution 1:[1]
You can serve the file using React Router. Put this in your index.js or App.js:
const reload = () => window.location.reload();
<Route path="/apple-app-site-association" onEnter={reload} />
More details here: https://brainbank.cc/jamie/lessons/programming-react/serve-static-file-txt-html-via-react-router
Solution 2:[2]
several days have passed and I didn't find an answer or solution. (And I really hate to see an unanswered stackoverflow question when I'm searching for a solution)
I've talked to a lot of people and pretty much everyone said this is impossible. Reason for that is that ReactJS is unable to return JSON type of response upon client sending a GET request.
So we transfered the file onto the back-end side which is working perfectly.
tl;dr It's not possible.
Solution 3:[3]
As discussed here, it is possible to server apple-app-site-association
from NextJs.
Next.js uses the extension to automatically detect the
content-type
of the file. Since the file is extensionless, it's served as binary content—this should be compatible with Apple's verification.If Apple requires specific headers, you can overwrite this type:
// next.config.js module.exports = { experimental: { headers() { return [ { source: "/.well-known/apple-app-site-association", headers: [{ key: "content-type", value: "application/json" }] } ]; } } };
Solution 4:[4]
For CRA: The file apple-app-site-association should be added to the public folder or public/.well-known folder. This alone won't make it work. Because it is served with a content-type of application/octet-stream. To fix it, we should use a solution provided by the hosting provider.
For Firebase: I am using Firebase for hosting my CRA app. So, for firebase, it is possible to specify the response headers for the path. The following gist shows how to do this. Make sure that the appAssociation should also be set to prevent firebase dynamic links from serving some other AASA file.
firebase.json config
{
"hosting": {
"public": "public",
"headers": [
{
"source": "/.well-known/apple-app-site-association",
"headers": [
{
"key": "Content-Type",
"value": "application/json"
}
]
}
],
"appAssociation": "NONE"
}
}
Solution 5:[5]
Disclaimer: This is only applicable if you have an Nginx implementation.
I got stuck on this and couldn't figure out a way to add the apple site association file to the react code in any way. However, I was able to solve the issue by looking outside the react box and inside my server configuration box i.e. Nginx.
Here are the steps:
- In the server store the AASA or apple-app-site-association.json file in a particular location - I chose to store it at (had to create two certifications and ios directories)
/var/www/certificates/ios/
- Now go to the sites-available folder
cd /etc/nginx/sites-available/
- Here, you will find the file with your web app's Nginx configuration information. Let's say, the name of my file was example-web-app, so open the file using vim with sudo privileges:
sudo vim mayank-web-app
- Now inside this file, you need to find
location / {
- Just above this line paste this code block
location /apple-app-site-association {
alias /var/www/certificates/ios/;
index apple-app-site-association.json
autoindex on; }
- Now save the file and exit, by pressing esc and typing
wq!
- Now check if the changes are valid by typing
sudo nginx -t
You should get this as output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- After this you will have to go to the sites-enabled directory
cd ../sites-enabled 9. Update the symlink here sudo ln -s /etc/nginx/sites-available/example-web-app .
- Now reload the nginx service
sudo service nginx reload
- Now you can check this with two methods:
a. https://branch.io/resources/aasa-validator/#resultsbox.
b. https://example.com/apple-app-site-association
Solution 6:[6]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user1761177 |
Solution 2 | PeeKey |
Solution 3 | Ponleu |
Solution 4 | |
Solution 5 | |
Solution 6 | Andrew |