Skip to content

Conversation

@m5r
Copy link

@m5r m5r commented Dec 13, 2024

Add support for touchstart, focus, and blur events in link prefetch hover behavior.

This is hugely inspired from React Router's link prefetching code. Their event handlers composition is pretty neat too but I don't think it's necessary here.

@m5r m5r marked this pull request as ready for review December 13, 2024 20:04

const regularEvents = {
onClick: (event) => {
onClick: (event: Parameters<InertiaLinkProps['onClick']>[0]) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit ugly but it's basically reusing the type of the first parameter of the onClick property defined in InertiaLinkProps above

blur: regularEvents.mouseleave,
click: regularEvents.click,
}
} satisfies ActionEventHandlers
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced the type assignment with the satisfies operator to make autocomplete smarter. Now typescript knows which properties from ActionEventHandlers are defined and which aren't while still validating the types are correct.

await page.getByRole('link', { name: 'On Mount' }).click()
await isPrefetchPage(page, 2)
await expect(requests.requests.length).toBe(0)
expect(requests.requests.length).toBe(0)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awaits here were not necessary, expect().toBe() doesn't return a promise, it's all synchronous

@m5r m5r force-pushed the expand-prefetch-behavior branch from 4d726f2 to c9e0aa2 Compare December 13, 2024 20:14
Copy link
Contributor

@joetannenbaum joetannenbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks cool, I just have a couple of questions here that I need clarification on. Thanks!

@@ -1,9 +1,9 @@
export default function shouldIntercept(event: MouseEvent | KeyboardEvent): boolean {
export default function shouldIntercept(event: MouseEvent | KeyboardEvent | import('react').MouseEvent<Element>): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a silly question, but doesn't this make react a dependency in the core library now?

}

const prefetchHoverEvents = {
onMouseEnter: () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move these events into the regularEvents? These shouldn't be fired if they don't intend to prefetch, correct?

@pascalbaljet
Copy link
Member

@m5r Thanks for this, looks like a great PR! Could you maybe check out Joe's comments about the React import and the mouseenter/mouseleave in the regularEvents object? Thanks!

@pascalbaljet pascalbaljet added the needs more info/work Needs more info from the author or additional work to get merged label Jun 17, 2025
@pascalbaljet
Copy link
Member

I'm closing this pull request due to lack of activity. If you're still working on this or have updates to share, feel free to open a new PR. We'd be happy to take another look. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs more info/work Needs more info from the author or additional work to get merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants