Response Type and URL
What You'll Learn
You'll learn how to interpret response.type and response.url to understand where a response came from and whether redirects occurred during the fetch.
Understanding Response Type
When you fetch a resource, the response.type property tells you the nature of the response based on security and origin rules. It has three main values:
-
"basic"– A same-origin response. Full access to headers and body. This is what you get when fetching from your own server. -
"cors"– A cross-origin response that explicitly allowed your request via CORS headers. You can read it, but only certain headers are exposed. -
"opaque"– A cross-origin response from ano-corsrequest. You can't read the body or most headers—it's like a black box. Useful for loading images or scripts you don't need to inspect.
Think of it like mail: basic mail is open and readable, CORS mail comes with a permission slip attached, and opaque mail is sealed—you know it arrived, but can't peek inside.
Tracking Redirects with response.url
The response.url property shows the final URL after any redirects. If you fetch https://example.com/old and it redirects to https://example.com/new, response.url will be https://example.com/new.
This is crucial for:
- Detecting unexpected redirects
- Logging the actual resource location
- Security checks (did my request end up somewhere unexpected?)
fetch('https://api.example.com/resource')
.then(response => {
console.log('Type:', response.type); // "cors"
console.log('Final URL:', response.url); // Might differ if redirected
console.log('Status:', response.status);
});
Key Takeaway: Use response.type to understand response security context (basic, cors, opaque) and response.url to track the final destination after redirects—essential for debugging and security validation.