Skip to content
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(remix-react): avoid duplicate loader calls when using prefetch-intent #2938

Merged
merged 5 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions integration/prefetch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function fixtureFactory(mode: RemixLinkProps["prefetch"]) {

"app/routes/index.jsx": js`
export default function() {
return <h2>Index</h2>;
return <h2 className="index">Index</h2>;
}
`,

Expand All @@ -61,13 +61,13 @@ function fixtureFactory(mode: RemixLinkProps["prefetch"]) {
return { message: 'data from the loader' };
}
export default function() {
return <h2>With Loader</h2>;
return <h2 className="with-loader">With Loader</h2>;
}
`,

"app/routes/without-loader.jsx": js`
export default function() {
return <h2>Without Loader</h2>;
return <h2 className="without-loader">Without Loader</h2>;
}
`,
},
Expand Down Expand Up @@ -187,6 +187,28 @@ test.describe("prefetch=intent (hover)", () => {
);
expect(await page.locator("#nav link").count()).toBe(3);
});

test("removes prefetch tags after navigating to/from the page", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");

// Links added on hover
await page.hover("a[href='/with-loader']");
await page.waitForSelector("#nav link", { state: "attached" });
expect(await page.locator("#nav link").count()).toBe(2);

// Links removed upon navigating to the page
await page.click("a[href='/with-loader']");
await page.waitForSelector("h2.with-loader", { state: "attached" });
expect(await page.locator("#nav link").count()).toBe(0);

// Links stay removed upon navigating away from the page
await page.click("a[href='/without-loader']");
await page.waitForSelector("h2.without-loader", { state: "attached" });
expect(await page.locator("#nav link").count()).toBe(0);
});
});

test.describe("prefetch=intent (focus)", () => {
Expand Down
21 changes: 18 additions & 3 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,14 @@ interface PrefetchHandlers {

function usePrefetchBehavior(
prefetch: PrefetchBehavior,
theirElementProps: PrefetchHandlers
theirElementProps: PrefetchHandlers,
linkHref: string
) {
let [maybePrefetch, setMaybePrefetch] = React.useState(false);
let [shouldPrefetch, setShouldPrefetch] = React.useState(false);
let { onFocus, onBlur, onMouseEnter, onMouseLeave, onTouchStart } =
theirElementProps;
let currentHref = useHref(useLocation());

React.useEffect(() => {
if (prefetch === "render") {
Expand All @@ -423,6 +425,17 @@ function usePrefetchBehavior(
}
};

React.useEffect(() => {
// If we navigate to a route that we potentially prefetched for, unset
// shouldPrefetch so that when we navigate away from this route the links
// don't get re-inserted and cause a dup call to the loader. We should
// require another intent to re-insert
if (prefetch === "intent" && linkHref === currentHref) {
setMaybePrefetch(false);
setShouldPrefetch(false);
}
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved
}, [prefetch, linkHref, currentHref]);

React.useEffect(() => {
if (maybePrefetch) {
let id = setTimeout(() => {
Expand Down Expand Up @@ -456,7 +469,8 @@ let NavLink = React.forwardRef<HTMLAnchorElement, RemixNavLinkProps>(
let href = useHref(to);
let [shouldPrefetch, prefetchHandlers] = usePrefetchBehavior(
prefetch,
props
props,
href
);
return (
<>
Expand Down Expand Up @@ -484,7 +498,8 @@ let Link = React.forwardRef<HTMLAnchorElement, RemixLinkProps>(
let href = useHref(to);
let [shouldPrefetch, prefetchHandlers] = usePrefetchBehavior(
prefetch,
props
props,
href
);
return (
<>
Expand Down