Hone logo
Hone
Problems

Implementing Background Sync for Offline Data Persistence in an Angular Application

Background sync allows an Angular application to continue processing data and synchronizing with a server even when the user is offline or has a poor network connection. This challenge focuses on implementing a basic background sync mechanism using the browser's Background Sync API to ensure data persistence and eventual synchronization when connectivity is restored. This is crucial for providing a robust and user-friendly experience in applications that rely on data persistence.

Problem Description

You are tasked with implementing a background sync feature for a simple Angular application that manages a list of "tasks." The application should allow users to add tasks, and these tasks should be persisted locally even when the user is offline. When the user regains connectivity, the application should automatically synchronize the local tasks with a simulated backend server.

What needs to be achieved:

  1. Local Storage: Implement a mechanism to store tasks in the browser's local storage.
  2. Background Sync Registration: Register a background sync event with the browser, specifying a tag (e.g., "task-sync").
  3. Task Addition: When a new task is added, store it locally and trigger a background sync registration.
  4. Synchronization: When the background sync event is triggered (simulating connectivity restoration), fetch the existing tasks from local storage and "send" them to a simulated backend (a simple console log will suffice for this challenge).
  5. Error Handling: Implement basic error handling for background sync registration and synchronization.

Key Requirements:

  • Use the navigator.serviceWorker API to register a service worker.
  • Utilize the navigator.registration.sync.register() method to initiate background sync.
  • Handle the sync event within the service worker to perform the synchronization logic.
  • The application should function correctly both online and offline.
  • The UI should provide feedback to the user about the sync status (e.g., "Syncing...", "Sync complete.").

Expected Behavior:

  • When a new task is added, it should be immediately stored in local storage.
  • A background sync registration request should be triggered.
  • When the browser detects connectivity, the background sync event should fire.
  • The service worker should retrieve tasks from local storage and log them to the console (simulating a server request).
  • The UI should reflect the successful synchronization.

Edge Cases to Consider:

  • User adds multiple tasks in rapid succession while offline.
  • Background sync registration fails (e.g., due to browser limitations).
  • Synchronization fails (e.g., due to simulated server errors).
  • User navigates away from the application while offline and returns later.

Examples

Example 1:

Input: User adds tasks "Buy groceries", "Walk the dog", "Pay bills" while offline.
Output: Tasks are stored in local storage. Background sync registration is attempted. When online, the service worker logs:
"Syncing tasks: [ { task: 'Buy groceries' }, { task: 'Walk the dog' }, { task: 'Pay bills' } ]" to the console.
Explanation: The tasks are persisted locally and synchronized when connectivity is restored.

Example 2:

Input: User adds a task "Call Mom" while online.
Output: Task is stored in local storage. Background sync registration is attempted. The service worker immediately logs:
"Syncing tasks: [ { task: 'Call Mom' } ]" to the console.
Explanation: Even when online, the background sync mechanism is triggered to ensure eventual synchronization.

Example 3: (Edge Case)

Input: User adds 5 tasks rapidly while offline. Background sync registration fails.
Output: Tasks are stored in local storage. No background sync registration occurs. When online, the tasks remain in local storage and are *not* synchronized.  An error message is logged to the console indicating the sync registration failure.
Explanation: Demonstrates handling of background sync registration failure.

Constraints

  • Angular Version: Angular 14 or higher.
  • Service Worker: The application must use a service worker for background sync to function.
  • Local Storage: Use localStorage for local data persistence.
  • Simulated Backend: For this challenge, the "backend" is simulated by logging the tasks to the console. No actual server interaction is required.
  • Performance: The synchronization process should be reasonably efficient. Avoid unnecessary loops or complex operations.
  • Task Object: Each task should be an object with a task property (string). Example: { task: "Do laundry" }

Notes

  • This challenge focuses on the core concepts of background sync. Error handling and UI feedback are important but can be kept relatively simple.
  • Consider using Angular's HttpClient for simulating the backend request, even though it's not strictly required.
  • The browser's Background Sync API has limitations (e.g., it may not be supported on all browsers). Handle these limitations gracefully.
  • Debugging service workers can be tricky. Use the browser's developer tools to inspect the service worker's lifecycle and logs.
  • Focus on the interaction between the Angular component, the service worker, and the browser's background sync API.
Loading editor...
typescript