Skip to content

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:

  1. Compatibility: fetch works consistently across browser and server environments
  2. Performance: Optimized data handling in SSR contexts
  3. Future-proofing: Angular's recommended path for HTTP communication in SSR

To resolve this warning and optimize your SSR application:

  1. Open your app's configuration file (app.config.ts)
  2. Modify the provideHttpClient() call to include withFetch()
  3. Verify your application bootstrap configuration

Implementation

typescript
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

FunctionPurposeRequired 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:

  1. Restart your development environment:

    bash
    npm run build && npm start
  2. Run client and server in separate terminals when using both in the same project

  3. Verify you're using Angular v17+:

    json
    "dependencies": {
      "@angular/common": "^17.0.0",
      "@angular/core": "^17.0.0"
    }
  4. 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:

  1. Streaming support: Better handling for large payloads
  2. Native compatibility: Consistent behavior across platforms
  3. Optimized transfer: Efficient transformation to server-readable formats
  4. 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:

typescript
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.