Skip to main content
Version: 3.2.0

Configuration

ForesightJS provides two levels of configuration:

  1. Global Configuration: Applied to the entire ForesightManager through initialization
  2. Element-Specific Configuration: Applied when registering individual elements

Global Configuration

Global settings are specified when initializing the ForesightManager. This should be done once at your application's entry point.

If you want the default global options you dont need to initialize the ForesightManager.

import { ForesightManager } from "foresightjs"

// Initialize the manager once at the top of your app if you want custom global settings
// ALL SETTINGS ARE OPTIONAL
ForesightManager.initialize({
enableMousePrediction: true,
positionHistorySize: 8,
trajectoryPredictionTime: 80,
defaultHitSlop: 10,
enableTabPrediction: true,
tabOffset: 3,
enableScrollPrediction: true,
scrollMargin: 150,
})

Available Global Settings

Typescript Type: ForesightManagerSettings

note

All numeric settings are clamped to their specified Min/Max values to prevent invalid configurations.

SettingTypeDefaultMin/MaxDescription
enableMousePredictionbooleantrue-Toggles whether trajectory prediction is active. If false, only direct hovers will trigger the callback for mouse users.
positionHistorySizenumber80/30Number of mouse positions to keep in history for velocity calculations
trajectoryPredictionTimenumber12010/200How far ahead (in milliseconds) to predict the mouse trajectory
defaultHitSlopnumber | Rect{top: 0, left: 0, right: 0, bottom: 0}0/2000Default fully invisible "slop" around elements for all registered elements. Basically increases the hover hitbox
enableTabPredictionbooleantrue-Toggles whether keyboard prediction is on
tabOffsetnumber20/20Tab stops away from an element to trigger callback
enableScrollPredictionbooleantrue-Toggles whether scroll prediction is on on
scrollMarginnumber15030/300Sets the pixel distance to check from the mouse position in the scroll direction callback
Development Tools

Visual development tools are now available as a separate package. See the development tools documentation for details on installing and configuring the js.foresight-devtools package.

Element-Specific Configuration

When registering elements with the ForesightManager, you can provide configuration specific to each element:

const myElement = document.getElementById("my-element")

const { isTouchDevice } = ForesightManager.instance.register({
element: myElement, // The element to monitor
callback: () => {
console.log("prefetching")
}, // Function that executes when interaction is predicted or occurs
hitSlop: { top: 10, left: 50, right: 50, bottom: 100 }, // Fully invisible "slop" around the element. Basically increases the hover hitbox
name: "My button name", // A descriptive name, useful for development tools
meta: { route: "/about" }, // Additional info
reactivateAfter: 60 * 1000 * 5, // 5 minutes
})

Element Registration Parameters

Typescript Type: ForesightRegisterOptions or ForesightRegisterOptionsWithoutElement if you want to omit the element

ParameterTypeRequiredDescriptionDefault
elementHTMLElementYesThe DOM element to monitor
callbackfunctionYesFunction that executes when interaction is predicted or occurs
hitSlopnumber | RectNoFully invisible "slop" around the element. Basically increases the hover hitbox0 or defaultHitSlop from initialize
namestringNoA descriptive name for the element, useful for development tools.element.id or "" if there is no id
metaRecord<string, unknown>NoStores additional information about the registered element (e.g. The path). Visible in all element related events and in the devtools{}
reactivateAfternumberNoTime in milliseconds after which the callback can be fired again, match with staleTime in tanstack query for exampleinfinity

Return Value of register()

Typescript Type: ForesightRegisterResult

PropertyTypeDescription
isTouchDevicebooleanIndicates whether the current device is a touch device. Elements will not be registered on touch devices. See
isLimitedConnectionbooleanIs true when the user is on a 2g connection or has data-saver enabled. Elements will not be registered when connection is limited.
isRegisteredbooleanIf either isTouchDevice or isLimitedConnection is true this will become false. Usefull for implementing alternative prefetching logic.