-
Notifications
You must be signed in to change notification settings - Fork 580
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
feat: Prefetch all links as they enter the viewport #11293
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Text } from "@artsy/palette-mobile" | ||
import { fireEvent, screen } from "@testing-library/react-native" | ||
import { RouterLink } from "app/Components/RouterLink" | ||
import { navigate } from "app/system/navigation/navigate" | ||
import { renderWithWrappers } from "app/utils/tests/renderWithWrappers" | ||
import { useEffect } from "react" | ||
import { View } from "react-native" | ||
|
||
const mockPrefetch = jest.fn() | ||
const MockedVisibleSentinel: React.FC<any> = ({ children, onVisible }) => { | ||
useEffect(() => onVisible(), []) | ||
|
||
return <View>{children}</View> | ||
} | ||
|
||
jest.mock("app/utils/queryPrefetching", () => ({ | ||
usePrefetch: () => mockPrefetch, | ||
})) | ||
jest.mock("app/utils/ElementInView", () => ({ | ||
ElementInView: (props: any) => <MockedVisibleSentinel {...props} />, | ||
})) | ||
|
||
describe("RouterLink", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
const TestComponent = (props: any) => ( | ||
<RouterLink to="/test-route" {...props}> | ||
<Text>Test Link</Text> | ||
</RouterLink> | ||
) | ||
|
||
it("renders", () => { | ||
renderWithWrappers(<TestComponent />) | ||
|
||
expect(screen.getByText("Test Link")).toBeDefined() | ||
}) | ||
|
||
it("navigates to route on press", () => { | ||
renderWithWrappers(<TestComponent />) | ||
|
||
fireEvent.press(screen.getByText("Test Link")) | ||
|
||
expect(navigate).toHaveBeenCalledWith("/test-route", { passProps: undefined }) | ||
}) | ||
|
||
describe("prefetching", () => { | ||
it("prefetches ", () => { | ||
renderWithWrappers(<TestComponent />) | ||
|
||
expect(mockPrefetch).toHaveBeenCalledWith("/test-route") | ||
}) | ||
|
||
describe("when disablePrefetch is true", () => { | ||
renderWithWrappers(<TestComponent disablePrefetch />) | ||
|
||
it("does not prefetch", () => { | ||
expect(mockPrefetch).not.toHaveBeenCalledWith("/test-route") | ||
}) | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Touchable, TouchableProps } from "@artsy/palette-mobile" | ||
import { navigate } from "app/system/navigation/navigate" | ||
import { ElementInView } from "app/utils/ElementInView" | ||
import { useFeatureFlag } from "app/utils/hooks/useFeatureFlag" | ||
import { usePrefetch } from "app/utils/queryPrefetching" | ||
import { GestureResponderEvent } from "react-native" | ||
|
||
interface RouterLinkProps { | ||
disablePrefetch?: boolean | ||
passProps?: Object | ||
to: string | null | undefined | ||
} | ||
|
||
export const RouterLink: React.FC<RouterLinkProps & TouchableProps> = ({ | ||
disablePrefetch, | ||
to, | ||
onPress, | ||
passProps, | ||
...restProps | ||
}) => { | ||
const prefetchUrl = usePrefetch() | ||
const enableViewPortPrefetching = useFeatureFlag("AREnableViewPortPrefetching") | ||
|
||
const handlePress = (event: GestureResponderEvent) => { | ||
onPress?.(event) | ||
|
||
if (to) { | ||
navigate(to, { passProps }) | ||
} | ||
} | ||
|
||
const handleVisible = () => { | ||
if (!disablePrefetch && enableViewPortPrefetching && to) { | ||
prefetchUrl(to) | ||
} | ||
} | ||
|
||
return ( | ||
<ElementInView onVisible={handleVisible}> | ||
<Touchable {...restProps} onPress={handlePress} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder here if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this is a call to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used the Yeah, |
||
</ElementInView> | ||
) | ||
} |
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.
Can we place all system-level things in the System folder? https://github.com/artsy/eigen/tree/main/src/app/system/navigation
(in general, we should leverage this folder more for framework-y things -- cc @MounirDhahri)
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.
Good idea! I'll move the component to the systems folder 👍