Angular HttpClient fetch for SSR
Understanding the Warning
When migrating to Angular 17 with Server-Side Rendering (SSR), you may encounter this warning in your console:
NG02801: Angular detected that `HttpClient` is not configured to use `fetch` APIs. It's strongly recommended to enable `fetch` for applications that use Server-Side Rendering for better performance and compatibility. To enable `fetch`, add the `withFetch()` to the `provideHttpClient()` call at the root of the application.
This is a non-blocking warning, not an error, but it highlights important improvements for SSR applications:
- Compatibility:
fetch
works consistently across browser and server environments - Performance: Optimized data handling in SSR contexts
- Future-proofing: Angular's recommended path for HTTP communication in SSR
Recommended Solution
To resolve this warning and optimize your SSR application:
- Open your app's configuration file (
app.config.ts
) - Modify the
provideHttpClient()
call to includewithFetch()
- Verify your application bootstrap configuration
Implementation
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideClientHydration } from '@angular/platform-browser';
import { provideHttpClient, withFetch } from '@angular/common/http';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(withFetch()),
provideRouter(routes),
provideClientHydration()
]
};
Key Components
Function | Purpose | Required for SSR |
---|---|---|
provideHttpClient() | Creates HTTP client instance | ✓ |
withFetch() | Enables Fetch API backend | ✓ |
provideClientHydration() | Enables DOM hydration | ✓ |
provideRouter() | Configures application routes | ✓ |
Troubleshooting Persistent Warnings
If the warning persists after implementing the solution:
Restart your development environment:
bashnpm run build && npm start
Run client and server in separate terminals when using both in the same project
Verify you're using Angular v17+:
json"dependencies": { "@angular/common": "^17.0.0", "@angular/core": "^17.0.0" }
Ensure no duplicate
provideHttpClient()
calls exist elsewhere in your application
Compatibility Note
The withFetch()
option requires Angular version 16.1 or later. Earlier versions should continue using the traditional HTTP backend for SSR.
Why fetch Improves SSR Performance
Angular recommends fetch for SSR due to:
- Streaming support: Better handling for large payloads
- Native compatibility: Consistent behavior across platforms
- Optimized transfer: Efficient transformation to server-readable formats
- Simplified logic: Unified code paths for client/server execution
By enabling fetch, you eliminate potential hydration mismatches between server-rendered content and client execution, improving both SEO performance and user experience.
Alternative Approaches
Manual Provider Configuration
If using explicit providers instead of Angular 17's function-based providers:
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
HttpClientModule
]
})
export class AppModule {}
This traditional approach automatically enables fetch in Angular 17+ without configuration. However, the explicit withFetch()
method is recommended for clarity and optimization control.
Legacy Note
Applications using HttpClientModule
are automatically transitioned to fetch in Angular 17. Explicit withFetch()
is equivalent and preferred in new provider-based configuration.