Angular makes Fetch API the default

Angular is finally making Fetch API the default. It was long overdue. Let me explain what’s changing and why it matters. 🔍 WHAT ARE FETCH AND XHR? XMLHttpRequest (XHR) has been the backbone of HTTP requests in browsers since the early 2000s. It’s callback-based, imperative, and designed for a different era of JavaScript. Fetch API arrived in 2015 — Promise-based, composable, and built for the modern async/await world we actually live in. Both do the same job. But how they do it is very different. ⚡ KEY DIFFERENCES 𝗘𝗿𝗿𝗼𝗿 𝗛𝗮𝗻𝗱𝗹𝗶𝗻𝗴 XHR fires onerror on failure. Fetch only rejects on network-level errors — a 404 or 500 still resolves. You must check response.ok manually. Easy gotcha. 𝗦𝘁𝗿𝗲𝗮𝗺𝗶𝗻𝗴 Fetch exposes response.body as a ReadableStream. XHR gives you the whole payload or nothing. 𝗨𝗽𝗹𝗼𝗮𝗱 𝗣𝗿𝗼𝗴𝗿𝗲𝘀𝘀 XHR wins here. xhr.upload.onprogress gives you byte-level tracking. Fetch has no upload progress API — still the main reason to keep XHR around for file uploads. 𝗖𝗮𝗻𝗰𝗲𝗹𝗹𝗮𝘁𝗶𝗼𝗻 XHR has xhr.abort(). Fetch uses AbortController — more composable, one controller can cancel multiple requests. 𝗞𝗲𝗲𝗽𝗮𝗹𝗶𝘃𝗲 Fetch supports { keepalive: true } — requests complete even after the page closes. Perfect for analytics. XHR dies on unload. 𝗡𝗼𝗱𝗲.𝗷𝘀 𝗦𝘂𝗽𝗽𝗼𝗿𝘁 This is the big one. XHR is browser-only. Fetch is native in Node.js 18+. 🅰️ WHY ANGULAR 22 MAKES FETCH THE DEFAULT Angular has been investing heavily in SSR and hydration. And here’s the problem XHR creates: it doesn’t exist in Node.js natively. Every Angular SSR setup using XHR needed polyfills or mocks just to run on the server. That’s fragile, adds complexity, and breaks the isomorphic code story. Fetch just works — same API, same code, running on both server and browser. This matters because: → HttpClient’s transfer cache (server fetches data once, browser reuses it during hydration) integrates cleanly with Fetch → Streaming responses open the door for better SSR performance patterns → keepalive enables reliable analytics on unload — something modern apps need → No more polyfill gymnastics for Node environments XHR isn’t gone — you can still scope it per-route where you need upload progress. But Fetch is now the default engine powering HttpClient. #Angular #WebDevelopment #JavaScript #Frontend #SSR #TypeScript #AngularDeveloper

To view or add a comment, sign in

Explore content categories