For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/angular-lynx/resource-prefetch.md.

LynxResourcePrefetch

Download images and videos into the native cache before they're shown, so they appear without a delay. import { LynxResourcePrefetch, type PrefetchRequest, type PrefetchPriority, type PrefetchCacheTarget, type PrefetchResultDetail, type ResourcePrefetchResult, type ResourcePrefetchData } from '@blotch/angular-lynx';

Usage

src/app/gallery.ts
import { Component, inject } from '@angular/core';
import { LYNX_ELEMENTS, LynxResourcePrefetch } from '@blotch/angular-lynx';

@Component({
  selector: 'app-gallery',
  imports: [LYNX_ELEMENTS],
  template: `<image src="https://example.com/hero.jpg" />`,
})
export class Gallery {
  readonly #prefetch = inject(LynxResourcePrefetch);

  constructor() {
    this.#prefetch
      .request([
        {
          uri: 'https://example.com/next.jpg',
          type: 'image',
          priority: 'high',
        },
        {
          uri: 'https://example.com/intro.mp4',
          type: 'video',
          priority: 'low',
        },
      ])
      .catch(() => {
        // Not available on this thread or platform.
      });
  }
}

API

LynxResourcePrefetch

@Injectable({ providedIn: 'root' })
class LynxResourcePrefetch {}

Asks Lynx to fetch resources into its image and video caches.

Runs on the background thread

Lynx provides prefetching only on the background thread, and your app runs on the main thread. Both methods send their call there for you, and the result comes back as JSON. See Threads.

LynxResourcePrefetch.request()

request(resources: PrefetchRequest[]): Promise<ResourcePrefetchResult>;

Starts prefetching. Resolves when Lynx accepts the request, which can be before the downloads finish. Rejects when prefetching isn't available.

LynxResourcePrefetch.cancel()

cancel(resources: PrefetchRequest[]): Promise<ResourcePrefetchResult>;

Cancels prefetches that are still running, for example when the user leaves the screen. Rejects when prefetching isn't available.

Types

PrefetchRequest

type PrefetchRequest = {
  uri: string;
  type: 'image' | 'video';
  priority?: PrefetchPriority;
  cacheTarget?: PrefetchCacheTarget;
  preloadKey?: string;
  size?: number;
};
FieldDescription
uriThe resource's URL.
type'image' or 'video'. Lynx handles each type differently.
priorityHow soon to fetch it. Lynx picks one when you leave it out.
cacheTargetWhere to keep it. See PrefetchCacheTarget.
preloadKeyYour own cache key, for managing the cache yourself.
sizeThe expected size in bytes, which helps Lynx allocate memory.

PrefetchPriority

type PrefetchPriority = 'high' | 'medium' | 'low';

PrefetchCacheTarget

type PrefetchCacheTarget = 'disk' | 'bitmap';
ValueMeaning
'disk'Saved to disk and kept across app sessions. For larger resources.
'bitmap'Decoded into memory, ready to show at once. For images.

PrefetchResultDetail

type PrefetchResultDetail = {
  code: number;
  msg: string;
  uri: string;
  type: 'image' | 'video';
};

The result for one resource.

ResourcePrefetchResult

// Re-exported from @lynx-js/types.
interface ResourcePrefetchResult {
  code: number;
  msg: string;
  details: PrefetchResultDetail[];
}

The overall result, with one details entry for each resource.

ResourcePrefetchData

// Re-exported from @lynx-js/types.
interface ResourcePrefetchData {
  data: {
    uri: string;
    type: 'image' | 'video' | 'font';
    param?: {
      priority?: PrefetchPriority;
      cacheTarget?: PrefetchCacheTarget;
      preloadKey?: string;
      size: number;
    };
  }[];
}

The request in Lynx's own format, which request() and cancel() build from your PrefetchRequest list. You only need it to call lynx.requestResourcePrefetch() yourself, in a backgroundFn().