cachingPageLoaderFor method

PageLoader cachingPageLoaderFor(
  1. Client client,
  2. Map<String, Map<String, dynamic>> cache, {
  3. Map<String, dynamic> transform(
    1. Map<String, dynamic>
    )?,
})

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, {
  Map<String, dynamic> Function(Map<String, dynamic>)? transform,
}) {
  final base = pageLoaderFor(client, transform: transform);
  return (String uri) async {
    final cached = cache[uri];
    if (cached != null) {
      _logger.debug('Loading page from cache', {'uri': uri});
      // The cache holds the entry page as it came off the wire, so it needs
      // the same transform the live path gets.
      return transform == null ? cached : transform(cached);
    }
    return base(uri);
  };
}