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).
This component requires an active service worker with Workbox configured. Ensure your PWA Bundle service worker is properly registered before using this component.
Unlike native <link rel="prefetch">, which may be ignored by browsers under certain conditions (like when on cellular data), service worker-based prefetching gives you more control over caching behavior.
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.
Individual Link Prefetching
Intersection Observer - Prefetch on Scroll
Menu Navigation Prefetching
Best Practices
Be Selective with Prefetching
Don't prefetch everything! Prefetching consumes bandwidth and cache storage. Only prefetch pages users are likely to visit based on:
User behavior patterns
Navigation flow analysis
Content relationships
User intent signals (hovering, scrolling direction)
Use Hover Intent, Not Immediate Hover
Consider using mouseenter instead of mouseover, or add a small delay before prefetching to avoid unnecessary prefetches when users move their mouse across the page.
Prioritize High-Value Pages
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
Service worker caches have storage limits. Implement cache management strategies to prevent filling up user storage:
Set cache expiration policies in Workbox
Limit the number of cached entries
Clear old prefetched pages periodically
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
prefetchedFired 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
errorFired 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
User Interaction: User triggers an event (hover, scroll, click)
Controller Action: The
prefetchaction is called with an array of URLsService Worker Message: Controller sends a
CACHE_URLSmessage to the service worker via WorkboxCaching: Service worker fetches and caches the specified URLs
Event Dispatch: Component emits
prefetchedorerroreventNavigation: 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:
Related Components
Service Worker - Required for prefetching to work
Background Fetch - For downloading large files in background
Connection Status - Check connection before prefetching
Network Information - Adapt prefetching based on connection type
Resources
Last updated
Was this helpful?