Loading a React production build directly through Bundle.main.url and WKWebView is causing my server requests Origin header to be set as “file://” due to which I get CORS Error.
We are using Telegraph server as well in our app and according to their documentation:
// You can also serve custom urls, for example the Demo folder in your bundle
let demoBundleURL = Bundle.main.url(forResource: "Demo", withExtension: nil)!
server.serveDirectory(demoBundleURL, "/demo")
We want to know if it is possible to serve a React production build using the above syntax and if yes, how?
Any guidance would be appreciated.
Thanks.
WHAT WE TRIED:-
We tried loading our React production build directly through WKWebView but the Origin header of the requests get set to “file://” which leads to CORS issue.
We tried serving our React production build using the syntax mentioned in Telegraph’s GitHub repo but that leads to a white screen on my app. (My guess is that Telegraph is not capable of serving React production builds this way). We were expecting this method to serve our React production build so that we can workaround the WKWebView Origin Header “file://” issue.
This is our code (not complete code, just relevant parts, for reference):
import Telegraph
public var server: Server!
public var webView: WKWebView!
// other code
let buildURL = Bundle.main.url(forResource: "build", withExtension: nil)!
server.serveDirectory(buildURL, "/build")
let url = URL(string: "https://**-MY IP HERE-**:9999/build")
let request = URLRequest(url: url!)
webView.load(request)
Chaithra Salian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.