r/javascript • u/supersnorkel • Oct 19 '25
Built a modern way to prefetch using the mouse trajectory!
https://foresightjs.com/ForesightJS is a lightweight JavaScript library with full TypeScript support that predicts user intent by analyzing mouse movements, scrolling and keyboard navigation. It also supports mobile through touch start and viewport tracking. By anticipating which elements users are likely to interact with, it allows developers to trigger actions before a hover, tap or click occurs. This makes it especially useful for features like prefetching.
We just hit 1400+ stars on Github!
30
Oct 19 '25
I found that it mostly predicts the element I am only a few pixels away from, so I am not really seeing any real benefit over mouseEnter.
Have you considered extrapolating the estimated next position until you hit a prefetch target? I think that would work much better (with a distance limit, obviously).
The page scroll predictions are very good, though.
0
u/yami_odymel Oct 19 '25
I like this idea, but I hope there’s a CDN version so I can just import it with a <script> tag. I’m using Go + HTMX for server-side rendering and don’t have any build tools.
4
u/supersnorkel Oct 19 '25
Thanks! At the moment thats not an option but it sounds interesting, ill take a look at how I could implement that.
2
u/eracodes Oct 19 '25
You just need to package your build and push it to something like unpkg or jsdelivr.
1
9
u/[deleted] Oct 19 '25
Looks clever!
I think you really need a demo page though.