Check if a file exists via a HEAD request

suggest change

An asynchronous HEAD request can check whether an HTTP resource is available without downloading its response body:

async function resourceExists(url, { signal } = {}) {
  const response = await fetch(url, { method: 'HEAD', signal });
  return response.ok;
}

This tests HTTP availability, not whether a path exists on the server's file system. Servers may disable HEAD, authentication may hide a resource, and cross-origin requests require appropriate CORS headers. Network failures reject the promise and should be handled by the caller.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents