Skip to content

Commit f41bdc9

Browse files
authored
feat: revalidate loaders only when a change to one is detected (#6135)
1 parent 78cb8df commit f41bdc9

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

integration/hmr-test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ let fixture = (options: {
155155

156156
"app/routes/_index.tsx": js`
157157
import { useLoaderData } from "@remix-run/react";
158+
export function shouldRevalidate(args) {
159+
return args.defaultShouldRevalidate;
160+
}
158161
export default function Index() {
159162
const t = useLoaderData();
160163
return (
@@ -219,6 +222,13 @@ test("HMR", async ({ page }) => {
219222
// uncomment for debugging
220223
// page.on("console", (msg) => console.log(msg.text()));
221224
page.on("pageerror", (err) => console.log(err.message));
225+
let dataRequests = 0;
226+
page.on("request", (request) => {
227+
let url = new URL(request.url());
228+
if (url.searchParams.has("_data")) {
229+
dataRequests++;
230+
}
231+
});
222232

223233
let portRange = makeRange(3080, 3099);
224234
let appServerPort = await getPort({ port: portRange });
@@ -276,6 +286,9 @@ test("HMR", async ({ page }) => {
276286
let newIndex = `
277287
import { useLoaderData } from "@remix-run/react";
278288
import styles from "~/styles.module.css";
289+
export function shouldRevalidate(args) {
290+
return args.defaultShouldRevalidate;
291+
}
279292
export default function Index() {
280293
const t = useLoaderData();
281294
return (
@@ -289,6 +302,7 @@ test("HMR", async ({ page }) => {
289302

290303
// detect HMR'd content and style changes
291304
await page.waitForLoadState("networkidle");
305+
292306
let h1 = page.getByText("Changed");
293307
await h1.waitFor({ timeout: HMR_TIMEOUT_MS });
294308
expect(h1).toHaveCSS("color", "rgb(255, 255, 255)");
@@ -305,13 +319,19 @@ test("HMR", async ({ page }) => {
305319
expect(await page.getByLabel("Root Input").inputValue()).toBe("asdfasdf");
306320
await page.waitForSelector(`#root-counter:has-text("inc 1")`);
307321

322+
// We should not have done any revalidation yet as only UI has changed
323+
expect(dataRequests).toBe(0);
324+
308325
// add loader
309326
let withLoader1 = `
310327
import { json } from "@remix-run/node";
311328
import { useLoaderData } from "@remix-run/react";
312329
313-
export let loader = () => json({ hello: "world" })
330+
export let loader = () => json({ hello: "world" });
314331
332+
export function shouldRevalidate(args) {
333+
return args.defaultShouldRevalidate;
334+
}
315335
export default function Index() {
316336
let { hello } = useLoaderData<typeof loader>();
317337
return (
@@ -322,10 +342,14 @@ test("HMR", async ({ page }) => {
322342
}
323343
`;
324344
fs.writeFileSync(indexPath, withLoader1);
345+
await page.waitForLoadState("networkidle");
346+
325347
await page.getByText("Hello, world").waitFor({ timeout: HMR_TIMEOUT_MS });
326348
expect(await page.getByLabel("Root Input").inputValue()).toBe("asdfasdf");
327349
await page.waitForSelector(`#root-counter:has-text("inc 1")`);
328350

351+
expect(dataRequests).toBe(1);
352+
329353
let withLoader2 = `
330354
import { json } from "@remix-run/node";
331355
import { useLoaderData } from "@remix-run/react";
@@ -334,6 +358,9 @@ test("HMR", async ({ page }) => {
334358
return json({ hello: "planet" })
335359
}
336360
361+
export function shouldRevalidate(args) {
362+
return args.defaultShouldRevalidate;
363+
}
337364
export default function Index() {
338365
let { hello } = useLoaderData<typeof loader>();
339366
return (
@@ -344,10 +371,14 @@ test("HMR", async ({ page }) => {
344371
}
345372
`;
346373
fs.writeFileSync(indexPath, withLoader2);
374+
await page.waitForLoadState("networkidle");
375+
347376
await page.getByText("Hello, planet").waitFor({ timeout: HMR_TIMEOUT_MS });
348377
expect(await page.getByLabel("Root Input").inputValue()).toBe("asdfasdf");
349378
await page.waitForSelector(`#root-counter:has-text("inc 1")`);
350379

380+
expect(dataRequests).toBe(2);
381+
351382
// change shared component
352383
let updatedCounter = `
353384
import * as React from "react";
@@ -388,6 +419,10 @@ test("HMR", async ({ page }) => {
388419
aboutCounter = await page.waitForSelector(
389420
`#about-counter:has-text("inc 0")`
390421
);
422+
423+
// This should not have triggered any revalidation but our detection is
424+
// failing for x-module changes for route module imports
425+
// expect(dataRequests).toBe(2);
391426
} catch (e) {
392427
console.log("stdout begin -----------------------");
393428
console.log(devStdout());

0 commit comments

Comments
 (0)