Angular

How do I implement retry + offline support with Resource API?

March 18, 2026

download ready
Thank You
Your submission has been received.
We will be in touch and contact you soon!

Resource API retry + offline support uses refetchOn with connectivity signals and custom loaders that detect network state. It automatically retries failed requests when connectivity restores and supports exponential backoff via loader configuration.

Code

import { Component, signal, resource, effect } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  template: `
    @if (users.status() === 'loading') { Loading... }
    @else if (users.hasValue()) {
      @for (user of users.value(); track user.id) {
        <div>{{ user.name }}</div>
      }
    } @else if (users.status() === 'error' && !isOnline()) {
      <div>Offline - Showing cached data</div>
    }
  `
})
export class UserList {
  // Track online status
  isOnline = signal(navigator.onLine);
  
  users = resource({
    loader: async () => {
      if (!this.isOnline()) {
        // Return cached data during offline
        return localStorage.getItem('users') ? JSON.parse(localStorage.getItem('users')!) : [];
      }
      
      // Network request with retry
      const response = await fetch('/api/users');
      if (!response.ok) throw new Error('Failed');
      
      const data = await response.json();
      localStorage.setItem('users', JSON.stringify(data));
      return data;
    },
    refetchOn: [this.isOnline] // Auto-refetch when online changes
  });

  constructor() {
    // Listen for online/offline events
    window.addEventListener('online', () => this.isOnline.set(true));
    window.addEventListener('offline', () => this.isOnline.set(false));
    
    // Auto-retry when back online
    effect(() => {
      if (this.isOnline()) {
        this.users.refetch();
      }
    });
  }
}
      
Hire Now!

Need Help with Angular Development ?

Work with our skilled angular developers to accelerate your project and boost its performance.
**Hire now**Hire Now**Hire Now**Hire now**Hire now

How do I implement retry + offline support with Resource API?

Resource API retry + offline support uses refetchOn with connectivity signals and custom loaders that detect network state. It automatically retries failed requests when connectivity restores and supports exponential backoff via loader configuration.

Code

import { Component, signal, resource, effect } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  template: `
    @if (users.status() === 'loading') { Loading... }
    @else if (users.hasValue()) {
      @for (user of users.value(); track user.id) {
        <div>{{ user.name }}</div>
      }
    } @else if (users.status() === 'error' && !isOnline()) {
      <div>Offline - Showing cached data</div>
    }
  `
})
export class UserList {
  // Track online status
  isOnline = signal(navigator.onLine);
  
  users = resource({
    loader: async () => {
      if (!this.isOnline()) {
        // Return cached data during offline
        return localStorage.getItem('users') ? JSON.parse(localStorage.getItem('users')!) : [];
      }
      
      // Network request with retry
      const response = await fetch('/api/users');
      if (!response.ok) throw new Error('Failed');
      
      const data = await response.json();
      localStorage.setItem('users', JSON.stringify(data));
      return data;
    },
    refetchOn: [this.isOnline] // Auto-refetch when online changes
  });

  constructor() {
    // Listen for online/offline events
    window.addEventListener('online', () => this.isOnline.set(true));
    window.addEventListener('offline', () => this.isOnline.set(false));
    
    // Auto-retry when back online
    effect(() => {
      if (this.isOnline()) {
        this.users.refetch();
      }
    });
  }
}