-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sveltekit:prefetch not working #2929
Comments
I think I found the issue in #2769. The pull request updates function find_anchor(event) {
const node = event
.composedPath()
.find((e) => e instanceof Node && e.nodeName.toUpperCase() === "A"); // SVG <a> elements have a lowercase name
return /** @type {HTMLAnchorElement | SVGAElement | undefined} */ (node);
} I tried logging const handle_mousemove = (event) => {
- clearTimeout(mousemove_timeout);
- mousemove_timeout = setTimeout(() => {
trigger_prefetch(event);
- }, 20);
}; I'm not suggesting we should, though. Just trying to help and isolate the issue. The same question suggests saving a reference to the composed path and it'd be possible at the price of updating const handle_mousemove = (event) => {
clearTimeout(mousemove_timeout);
const composedPath = event.composedPath();
mousemove_timeout = setTimeout(() => {
trigger_prefetch(comosedPath);
}, 20);
}; Let me know if I can help some more :) Thanks to @Billyzou0741326 for highlighting the change introduced by c7c25c0 (199) 🙌 Update: I created a small demo on CodePen to describe the issue with a trivial example. The |
Thanks for digging into this @borntofrappe. Looks like this one's tricky to handle. Calling |
Early apology for my inexperience with custom events @bluwy I updated the pen and by creating a custom event I was able to access the array even with a delay. I tried the same approach in the router as follows: const handle_mousemove = (event) => {
clearTimeout(mousemove_timeout);
mousemove_timeout = setTimeout(() => {
dispatchEvent(new CustomEvent('trigger'))
}, 20);
};
dispatchEvent('trigger', trigger_prefetch); But in this instance the event refers to the window object. I got it working listening to the event on the target of the event itself, but I'm not confident in the solution. const handle_mousemove = (event) => {
clearTimeout(mousemove_timeout);
mousemove_timeout = setTimeout(() => {
event.target.addEventListener('trigger', trigger_prefetch);
event.target.dispatchEvent(new CustomEvent('trigger'));
event.target.removeEventListener('trigger', trigger_prefetch);
}, 20);
}; I hope you'll find a better way 🤞 |
No worries @borntofrappe. Looks like you're on the right track, we would need to call With all these said, looks like you've already implemented exactly that in your updated codepen 😄 Feel free to send a PR to fix this. |
Understood @bluwy In the end it's almost frustrating it only takes two lines (took me a while to figure it out). const handle_mousemove = (event) => {
clearTimeout(mousemove_timeout);
mousemove_timeout = setTimeout(() => {
- trigger_prefetch(event);
+ event.target.dispatchEvent(new CustomEvent('trigger_prefetch', { bubbles: true }))
}, 20);
};
addEventListener('touchstart', trigger_prefetch);
addEventListener('mousemove', handle_mousemove);
+ addEventListener('trigger_prefetch', trigger_prefetch); |
* 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>
Describe the bug
When I move mouse pointer over a
<a sveltekit:prefetch href="/page2">
, no chunks are preloaded.Reproduction
I created a simple repository to reproduce the issue: https://github.com/ww9/sveltekit-prefetch-test
It was created with
npm init svelte@next test
and:Then added some links with
sveltekit:prefetch
but it doesn't work.Logs
No response
System Info
Severity
annoyance
Additional Information
Svelte is amazing ❤
The text was updated successfully, but these errors were encountered: