Skip to content

Angular PWA and offline server #51337

Closed as not planned
Closed as not planned

Description

@MutazNawas

Which @angular/* package(s) are the source of the bug?

service-worker

Is this a regression?

Yes

Description

I utilize the PWA functionality to enable offline capabilities for my application. This involves executing the command ng add @angular/pwa. Initially, everything functions as intended. However, upon testing a scenario where the server experiences an outage or is offline, I observed that the cache storage is being cleared and the service worker is removed.

It's worth noting that there are no problems when the client is in an offline state. After investigating the ngsw-worker.js file, I identified a specific code segment responsible for triggering this cache clearance. The code is within the fetchLatestManifest function:

async fetchLatestManifest(ignoreOfflineError = false) {
  const res = await this.safeFetch(this.adapter.newRequest("ngsw.json?ngsw-cache-bust=" + Math.random()));
  if (!res.ok) {
    if (res.status === 404) {
      await this.deleteAllCaches();
      await this.scope.registration.unregister();
    } else if ((res.status === 503 || res.status === 504) && ignoreOfflineError) {
      return null;
    }
    throw new Error(`Manifest fetch failed! (status: ${res.status})`);
  }
  this.lastUpdateCheck = this.adapter.time;
  return res.json();
}

This behavior seems counterintuitive, as cache deletion shouldn't occur when the server is offline. Ideally, the service worker should cache the ngsw.json and rely on it, rather than deleting caches when the server experiences downtime. I'm seeking advice on potential solutions to this issue. Should I modify the code to resolve this problem? If so, what changes are recommended to ensure caches are retained even when the server is offline?

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

No response

Anything else?

No response

Activity

  1. JeanMeche commented on Aug 11, 2023

    @JeanMeche
    Member

    An offline server shouldn't return a 404 right ?

    A 404 would mean that the app doesn't exist anymore at its original location.

  2. MutazNawas commented on Aug 11, 2023

    @MutazNawas
    Author

    @JeanMeche
    In my situation, if my server is not online or is inaccessible for a certain period, I would like my client to function as if it's in an offline mode.

  3. added this to the needsTriage milestone on Aug 13, 2023
  4. JeanMeche commented on Aug 29, 2025

    @JeanMeche
    Member

    With #63364 we've announced that the service-worker package in now in maintenance mode and will not receive feature PRs and feature requests.

    The message is also visible here: https://angular.dev/ecosystem/service-workers

  5. angular-automatic-lock-bot commented on Sep 29, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  6. locked and limited conversation to collaborators on Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: service-workerIssues related to the @angular/service-worker package

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions