Prefetch on demand

The Prefetch on Demand component enables intelligent, user-driven page prefetching to dramatically improve navigation performance in your Progressive Web App. Instead of relying solely on static <link rel="prefetch"> tags, this component allows you to dynamically prefetch pages based on user interactions like hovering over links, scrolling into view, or any custom trigger. Pages are cached by the service worker via Workbox, making subsequent navigation instantaneous.

This component is particularly useful for:

  • Speeding up navigation in content-heavy websites (blogs, news sites, documentation)

  • Preloading likely next pages in multi-step forms or checkout flows

  • Improving perceived performance in single-page applications

  • Caching related articles, author pages, or recommended content

  • Prefetching resources when users hover over navigation menus

  • Loading content as users scroll down infinite-scroll pages

  • Preparing next chapters in e-learning platforms

  • Optimizing user experience in catalog browsing and e-commerce

Browser Support

The Prefetch on Demand component relies on service workers and the Cache API, which are widely supported across modern browsers.

Support level: Excellent - Works on all modern browsers that support service workers (Chrome, Firefox, Safari, Edge).

circle-info

This component requires an active service worker with Workbox configured. Ensure your PWA Bundle service worker is properly registered before using this component.

circle-check

Traditional Prefetching vs. On-Demand

Static Prefetching

Traditional HTML-based prefetching uses link tags:

Limitations:

  • Prefetching starts immediately on page load

  • No control over timing or conditions

  • Browser may ignore hints based on connection type or battery

  • Cannot respond to user behavior

Dynamic On-Demand Prefetching

The Prefetch on Demand component offers more flexibility:

  • Trigger prefetching based on user interactions (hover, scroll, click)

  • Programmatically control when and what to prefetch

  • Cache resources through service worker for reliable offline access

  • Respond to user behavior and intent

Usage

Basic Hover-Based Prefetching

When users hover over the aside section, the specified URLs are prefetched and cached.

Intersection Observer - Prefetch on Scroll

Best Practices

Be Selective with Prefetching

circle-exclamation

Use Hover Intent, Not Immediate Hover

circle-check

Prioritize High-Value Pages

circle-info

Prefetch pages that provide the most value when loaded instantly:

  • Next steps in user workflows

  • High-traffic destination pages

  • Content directly related to current page

  • Pages that require authentication or API calls

Monitor Cache Size

circle-exclamation

Respect User Preferences

Common Use Cases

1. Multi-Step Form/Checkout Flow

2. Blog Article Navigation

3. E-Commerce Product Listing

4. Documentation Navigation

5. Infinite Scroll Feed

Example with custom event:

Targets

None

Events

prefetched

Fired when the service worker successfully receives and processes the prefetch request. Note that this indicates the command was received, not necessarily that all URLs are fully cached.

Event detail:

  • params.urls (array): The URLs that were requested to be prefetched

error

Fired when the prefetch operation fails (e.g., service worker not available, Workbox not configured).

Event detail:

  • params.urls (array): The URLs that failed to prefetch

How It Works

  1. User Interaction: User triggers an event (hover, scroll, click)

  2. Controller Action: The prefetch action is called with an array of URLs

  3. Service Worker Message: Controller sends a CACHE_URLS message to the service worker via Workbox

  4. Caching: Service worker fetches and caches the specified URLs

  5. Event Dispatch: Component emits prefetched or error event

  6. Navigation: When user navigates to prefetched URL, it loads instantly from cache

Configuration

Ensure your service worker is configured to handle the CACHE_URLS message. The PWA Bundle automatically configures this when Workbox is enabled.

If you need custom cache configuration:

Resources

Last updated

Was this helpful?