What is ForesightJS
ForesightJS is a lightweight JavaScript library that predicts user intent to prefetch content before it's needed. It starts earlier than hover prefetching, loads less than viewport prefetching, and works for keyboard users. It works completely out of the box without configuration, supporting both desktop and mobile devices with different prediction strategies.
Understanding ForesightJS's Role
When you over simplify prefetching it exists of three parts:
- What resource or data to load
- How the loading method and caching strategy is
- When the optimal moment to start fetching is
ForesightJS takes care of the When by predicting user intent with mouse trajectory, tab navigation and scroll direction.
You supply the What and How inside your callback when you register an element.
In Vue you use ForesightJS through the official @foresightjs/vue package: the v-foresight directive registers your elements, and composables expose their reactive prediction state.
Installation
npm install @foresightjs/vue js.foresight
Continue with Installation and the Quick Start.
Prediction Strategies
ForesightJS uses different prediction strategies depending on the device type. For limited connections (slower than the minimum connection type - default: 3G - or data-saver mode), we respect the user's preference to minimize data usage: the element is still registered but stays inactive, so it is never predicted and never prefetches. Foresight automatically detects mouse, pen, or touch input, and updates its strategy if the input method changes.
Keyboard/Mouse Users
Pick and choose multiple prediction strategies. All are enabled by default, but you can disable any of them when initializing the ForesightManager.
- Mouse Trajectory default - Analyzes cursor movement patterns to predict which links users are heading towards and prefetches content before they arrive
- Keyboard Navigation default - Tracks tab key usage to prefetch when the user is N tab stops away from your registered element
- Scroll default - Prefetches content when users scroll towards registered elements, predicting which elements will be reached based on scroll direction
Touch Devices
While desktop systems use multiple concurrent prediction strategies, touch devices operate with a single active strategy that can be configured while initializing the ForesightManager.
- onTouchStart default - Captures the initial touch event to begin prefetching when users start interacting with registered elements
- Viewport Enter - Detects when registered elements enter the viewport and prefetches their content based on scroll behavior and visibility
- None - Disables ForesightJS on touch devices (previous behavior)
Why ForesightJS?
ForesightJS is a specialized performance tool, not something to drop into every project.
Which problems does ForesightJS solve?
Problem 1: Hover-Based Prefetching Starts Late
Hover-based prefetching only triggers once the cursor is already on the link. The 100-200ms between the user starting to move toward a link and the hover event firing goes unused. ForesightJS predicts the target from the mouse trajectory and starts prefetching before the cursor arrives.
Problem 2: Viewport-Based Prefetching Loads What Users Never Click
Viewport prefetching loads routes for links as they become visible, while users usually click only a few of them. Some frameworks now make this cheaper. Next.js has an opt-in Partial Prefetching mode that fetches one shared shell per route instead of one request per link. ForesightJS only prefetches the links users are predicted to interact with.
Problem 3: Keyboard Users Get Less
Hover-only prefetching never fires for keyboard users. Strategies that also listen to focus only start once the link is focused. ForesightJS tracks Tab navigation and prefetches when the user is a few tab stops away from a link.
Lightweight
The full library is ~32 KB minified, and code splitting means you only ship the parts you actually use. See Behind the Scenes for the per-chunk breakdown.