kit icon indicating copy to clipboard operation
kit copied to clipboard

`handleFetch` implementation bug?

Open ponderingexistence opened this issue 3 years ago • 0 comments

Describe the bug

Let me know if I'm just being dumb but I think I noticed a subtle flaw in the implementation of the handleFetch hook here:

export async function handleFetch({ request, fetch }) {
  if (request.url.startsWith('https://api.yourapp.com/')) {
    // clone the original request, but change the URL
    request = new Request(
      request.url.replace('https://api.yourapp.com/', 'http://localhost:9999/'),
      request
    );
  }
 
  return fetch(request);
}

Here we're sending the request object as the second argument to the constructor of the Request class, which expects an options object containing these properties, a Request object happens to have properties that correspond to the properties that the options object expects (e.g. mode, method, etc.), expect for a few like keepalive and signal, which if I'm not mistaken means that if we have a load function that looks like this:

export async function load({ }) {
  const res = await fetch('...' {
    signal: whatever
  });
}

If this now goes through handleFetch, the signal option gets lost. Am I right?

ponderingexistence avatar Sep 10 '22 18:09 ponderingexistence