Skip to content

Commit

Permalink
Dont prefetch server loader when client loader exists
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jun 6, 2024
1 parent 8a997a6 commit f2bdb58
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/heavy-ways-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

Don't prefetch server loader data when clientLoader exists
86 changes: 86 additions & 0 deletions integration/client-data-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,48 @@ test.describe("Client Data", () => {
'not have a server loader (routeId: "routes/parent.child")'
);
});

test("does not prefetch server loader if a client loader is present", async ({
page,
}) => {
appFixture = await createAppFixture(
await createTestFixture({
files: {
...getFiles({
parentClientLoader: true,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/_index.tsx": js`
import { Link } from '@remix-run/react'
export default function Component() {
return (
<>
<Link prefetch="render" to="/parent">Go to /parent</Link>
<Link prefetch="render" to="/parent/child">Go to /parent/child</Link>
</>
);
}
`,
},
})
);

let dataUrls: string[] = [];
page.on("request", (request) => {
if (request.url().includes("_data")) {
dataUrls.push(request.url());
}
});

let app = new PlaywrightFixture(appFixture, page);
await app.goto("/", true);
// Only prefetch child server loader since parent has a `clientLoader`
expect(dataUrls).toEqual([
expect.stringMatching(/parent\/child\?_data=routes%2Fparent\.child/),
]);
});
});

test.describe("clientAction - critical route module", () => {
Expand Down Expand Up @@ -2212,6 +2254,50 @@ test.describe("single fetch", () => {
'not have a server loader (routeId: "routes/parent.child")'
);
});

test("does not prefetch server loader if a client loader is present", async ({
page,
}) => {
appFixture = await createAppFixture(
await createTestFixture({
files: {
...getFiles({
parentClientLoader: true,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/_index.tsx": js`
import { Link } from '@remix-run/react'
export default function Component() {
return (
<>
<Link prefetch="render" to="/parent">Go to /parent</Link>
<Link prefetch="render" to="/parent/child">Go to /parent/child</Link>
</>
);
}
`,
},
})
);

let dataUrls: string[] = [];
page.on("request", (request) => {
if (request.url().includes(".data")) {
dataUrls.push(request.url());
}
});

let app = new PlaywrightFixture(appFixture, page);
await app.goto("/", true);
// Only prefetch child server loader since parent has a `clientLoader`
expect(dataUrls).toEqual([
expect.stringMatching(
/parent\/child\.data\?_routes=routes%2Fparent\.child/
),
]);
});
});

test.describe("clientAction - critical route module", () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/remix-react/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,11 @@ export function getDataLinkHrefs(
let path = parsePathPatch(page);
return dedupeHrefs(
matches
.filter((match) => manifest.routes[match.route.id].hasLoader)
.filter(
(match) =>
manifest.routes[match.route.id].hasLoader &&
!manifest.routes[match.route.id].hasClientLoader
)
.map((match) => {
let { pathname, search } = path;
let searchParams = new URLSearchParams(search);
Expand Down

0 comments on commit f2bdb58

Please sign in to comment.