hey when using `fetch request ` is there

hey, when using fetch(request), is there a way to modify the path when fetching the request from the origin? I want it to use the proxied DNS target but I want to alter the path...
9 Replies
gonz
gonzOPβ€’3y ago
I've been creating the full fetch URL myself but I wondered if I could keep the target from the DNS record and just alter the path... just feels weird pointing the proxied record to any random name since I will be overriding it so I figured maybe I can use the name that it points to so it has meaning...
kian
kianβ€’3y ago
const url = new URL(request.url)
url.pathname = β€œ/new”

return fetch(url, request)
const url = new URL(request.url)
url.pathname = β€œ/new”

return fetch(url, request)
gonz
gonzOPβ€’3y ago
request.url is the requested URL though... not the origin URL? or does Cloudflare then do behind the scenes work to fetch from the origin?
kian
kianβ€’3y ago
If you put a Worker on top of an origin, there is no difference between the URLs. Fetching the same URL in a Worker falls through to the origin
gonz
gonzOPβ€’3y ago
thank you @kian helpful as always! @kian just trying to test this locally by putting a random URL as the target (which should most definitely return a 404) and it keeps saying that the origin response is a 200... odd πŸ€” Using wrangler in dev mode. @kian so odd.. I set the target address as www.google.com. Surely fetch(request) should return a 404 as our pages don't exist in www.google.com yet the origin seems to return 200 πŸ€” @kian ok I discovered what the issue was πŸ€¦β€β™‚οΈ I had a lingering origin rule that was rewriting the destination... driving me mad! however, it looks like I can't use this anyway because it connects to the destination (in my case storage.googleapis.com) but the Host header is the original URL, and it needs to be storage.googleapis.com otherwise it doesn't work as it has no idea how to handle the URL
kian
kianβ€’3y ago
Just fetch that URL instead
gonz
gonzOPβ€’3y ago
yeah, that's what I was doing originally.. so I guess I'll go back to making the "target" in the DNS name any random name since it's not going to be used...
kian
kianβ€’3y ago
You can use a custom domain rather than a route for your Worker
gonz
gonzOPβ€’3y ago
oh to avoid the proxied DNS record? yeah, that's an option

Did you find this page helpful?