Check if a file exists via a HEAD request
suggest changeAn 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents