-
-
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
fix: update sveltekit:prefetch
mouse detection
#2995
fix: update sveltekit:prefetch
mouse detection
#2995
Conversation
🦋 Changeset detectedLatest commit: f06924d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
I didn't review the PR yet, but please note that the Lint CI job is failing and would need to be fixed |
Apologies, but I don't know how to consider the message "No overload matches this call". Is this connected to TypeScript? Unfortunately I need some guidance on this point. |
For "No overload matches this call", TypeScript isn't happy that |
I see. Using
I've managed to remove the message by returning the function early if if (!event.target) return;
event.target.dispatchEvent(
new CustomEvent('sveltekit:trigger_prefetch', { bubbles: true })
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. The failing tests seems to be caused by an issue with amphtml-validator
(unrelated), I've made #3022 to fix this.
7a7ccec
to
26d6bcd
Compare
trigger_prefetch(event); | ||
// ensure event.composedPath() in find_anchor(event) returns an array of EventTarget objects | ||
event.target?.dispatchEvent( | ||
new CustomEvent('sveltekit:trigger_prefetch', { bubbles: true }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if something like sveltekit:mousestop
might be a better name for this? What do you guys think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either works for me, but it does looks nicer when used in the addEventListener
call below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I lean toward sveltekit:trigger_prefetch
as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My instinct is to look for an alternative solution altogether — it seems weird to dispatch a custom event just to indirectly call our own function, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I realised there's a deeper problem here. The composedPath
stuff was introduced because recursively checking parentNode
to find an <a>
element doesn't work if the <a>
in question is in shadow DOM.
The solution here dispatches a custom event on e.target
, but the composedPath
of the custom event won't include the shadow <a>
in certain cases. Demo here — basically, if you have a custom element with an <a>
in the shadow DOM like this...
customElements.define(
'fancy-a',
class extends HTMLElement {
constructor() {
super();
let a = document.createElement('a');
a.href = this.getAttribute('href');
a.innerHTML = '<slot></slot>';
let shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.append(a);
}
}
);
...then whether or not the <a>
will be included in the composedPath
depends on whether the original event happened to happen within an element in light DOM inside the custom element.
I'm not exactly sure how to solve this, or if it's even possible. We might have to just say that prefetching won't work with shadow DOM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That does look a bit risky and potentially more resource intensive. I wonder if it would be fine to not support sveltekit:prefetch
in web components at all, the usecase would be rare. And also that normal link clicks should already work in web components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be totally fine dropping support for prefetch with web components
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on the pragmatic side here. I don't think that dispatching a custom element with an explanation why this is needed is bad nor does it clutter the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem dispatching event is that Rich showed it doesn't work all the times with custom elements. I share the sentiment from @bluwy that the issue is becoming rather cumbersome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very well, if we're okay only having partial support for shadow DOM then we can merge this — thanks. Very annoying that the existence of shadow DOM forces us to jump through these ridiculous hoops in the first place!
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
sveltekit:prefetch
mouse detection
dispatches a custom event so that
event.composedPath
returns an array of objects - fixes #2929Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpx changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0