Currently my worker (domain.com) requests to “app.domain.com/wp-json/etc” - this brings up some CORS issues + I expose my API.
I was wondering if you could somehow write a worker script that would check if /wp-json/ was included in the path, and then request app.domain.com/wp-json/etc?
So if I made a request to:
http://domain.com/wp-json/test
It would return contents from:
http://app.domain.com/wp-json/test
This way URLs would be cleaner plus I believe I could avoid annoying CORS issues.
Yes that’s defiently possible. This is how I achieved it.
async function handleEvent(event) {
const url = new URL(event.request.url)
[....]
/**
* WP-JSON
*/
if (url.pathname.includes('wp-json') || url.pathname.includes('api/') || url.pathname.includes('wp-content/')) {
url.hostname = 'app.your-wordpress-site.com';
if (url.pathname.includes('api/')) {
url.pathname = url.pathname.replace('api/', 'wp-json/');
}
return await fetch(url, event.request);
}
The API is 50% faster now because of no CORS issues.