Skip to content

Commit

Permalink
fix: update sveltekit:prefetch mouse detection (#2995)
Browse files Browse the repository at this point in the history
* dispatch custom event - fixes #2929

* changeset

* missing semicolon

* clarify pull request

* pnpm check

* update typescript check

* Update packages/kit/src/runtime/client/router.js

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
  • Loading branch information
borntofrappe and benmccann authored Dec 22, 2021
1 parent 3fb176a commit 3acdf93
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-ways-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix `sveltekit:prefetch` mouse detection
9 changes: 7 additions & 2 deletions packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Router {
}, 200);
});

/** @param {MouseEvent|TouchEvent} event */
/** @param {Event} event */
const trigger_prefetch = (event) => {
const a = find_anchor(event);
if (a && a.href && a.hasAttribute('sveltekit:prefetch')) {
Expand All @@ -109,12 +109,17 @@ export class Router {
const handle_mousemove = (event) => {
clearTimeout(mousemove_timeout);
mousemove_timeout = setTimeout(() => {
trigger_prefetch(event);
// event.composedPath(), which is used in find_anchor, will be empty if the event is read in a timeout
// add a layer of indirection to address that
event.target?.dispatchEvent(
new CustomEvent('sveltekit:trigger_prefetch', { bubbles: true })
);
}, 20);
};

addEventListener('touchstart', trigger_prefetch);
addEventListener('mousemove', handle_mousemove);
addEventListener('sveltekit:trigger_prefetch', trigger_prefetch);

/** @param {MouseEvent} event */
addEventListener('click', (event) => {
Expand Down

0 comments on commit 3acdf93

Please sign in to comment.