cachingPageLoaderFor method

PageLoader cachingPageLoaderFor(
  1. Client client,
  2. Map<String, Map<String, dynamic>> cache
)

Page loader that serves cache entries from memory and falls back to the live client for any other uri.

The server-open path already reads the entry page (ui://app) once to detect its type before promoting it to an application (wrapAsApplication). Routing that page through the client again — a second read over a possibly dead link — is what surfaced "Failed to load page / Client is not initialized" on a screen with no way out. Feeding the already-parsed content back through the cache means the app's first frame renders from memory, so its AppBar (and its close/exit affordance) is always present, even if the connection dropped right after the initial load.

Implementation

PageLoader cachingPageLoaderFor(
  Client client,
  Map<String, Map<String, dynamic>> cache,
) {
  final base = pageLoaderFor(client);
  return (String uri) async {
    final cached = cache[uri];
    if (cached != null) {
      _logger.debug('Loading page from cache', {'uri': uri});
      return cached;
    }
    return base(uri);
  };
}