Return original webpage response without using fetch()?

I have a Cloudflare worker script that loads on a specific route pattern. From thorough testing, I’ve come to see that having a CF worker proxy is causing webpages in this route pattern to load twice as slow because it uses fetch(), thus the requests basically loads twice. Is it possible to just return the original response in the code and bypass CF workers without using fetch()? I took a look at the examples and looked into this further but I wasn’t able to find any other solution.
5 Replies
Chaika
Chaika13mo ago
What is your worker doing? If you just have
export default {
async fetch(request) {
return fetch(request);
},
};
export default {
async fetch(request) {
return fetch(request);
},
};
It's only going to be one request to your origin. The worker fetches through to your origin and returns it. If you have ctx.passThroughOnException (or are on Workers free with fail open) and your Worker is erroring, that's the only case I could think of Workers would send a second request without you doing it on your code If you're trying to measure it, make sure you're looking at actual origin requests and not CF Logs and such, you'd see the subrequest as a separate log in Live Logs for example
monkev_see_monkev_do
Thanks! I was able to find the issue. I was calling this getOriginalPage() function:
async function getOriginalPage(request) {
const originalResponse = await fetch(request); // Fetch the original response from the origin server
const originalResponseText = await originalResponse.text(); // Read the original response body as text
return originalResponseText;
}
async function getOriginalPage(request) {
const originalResponse = await fetch(request); // Fetch the original response from the origin server
const originalResponseText = await originalResponse.text(); // Read the original response body as text
return originalResponseText;
}
I believe I added this in awhile ago to battle 1120 error issues I experienced before. Now I'm just calling return fetch(request) anytime I need the original response.
you'd see the subrequest as a separate log in Live Logs for example
I was only able to only to see the origin requests -- https://jmp.sh/qoO5frXv -- in my code I am fetching some data on an external domain and was expecting to see those subrequests in the Live Logs. Perhaps these are not supposed to show?
Jumpshare
CF logs.png
Shared with Jumpshare
Chaika
Chaika13mo ago
Nice! Yea, you can clone the original response as well, just have to pass it around a bit
I was only able to only to see the origin requests -- https://jmp.sh/qoO5frXv -- in my code I am fetching some data on an external domain and was expecting to see those subrequests in the Live Logs. Perhaps these are not supposed to show?
Sorry looks like I confused you. You're looking at Tail Logs in that instance. I was talking about Instant Logs, or Logpush which do separate out subrequests. Their behavior has confused people in the past
No description
Chaika
Chaika13mo ago
In Tail Logs you'll only see the original request
monkev_see_monkev_do
Ah ok, thanks! Makes sense
Want results from more Discord servers?
Add your server