-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathclient-data-test.ts
1546 lines (1461 loc) · 59.8 KB
/
client-data-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { test, expect } from "@playwright/test";
import { UNSAFE_ServerMode as ServerMode } from "react-router";
import {
createAppFixture,
createFixture,
js,
} from "./helpers/create-fixture.js";
import type { AppFixture } from "./helpers/create-fixture.js";
import { PlaywrightFixture } from "./helpers/playwright-fixture.js";
import { reactRouterConfig } from "./helpers/vite.js";
function getFiles({
splitRouteModules,
parentClientLoader,
parentClientLoaderHydrate,
parentAdditions,
childClientLoader,
childClientLoaderHydrate,
childAdditions,
}: {
splitRouteModules: boolean;
parentClientLoader: boolean;
parentClientLoaderHydrate: boolean;
parentAdditions?: string;
childClientLoader: boolean;
childClientLoaderHydrate: boolean;
childAdditions?: string;
}) {
return {
"react-router.config.ts": reactRouterConfig({ splitRouteModules }),
"app/root.tsx": js`
import { Outlet, Scripts } from "react-router"
export default function Root() {
return (
<html>
<head></head>
<body>
<main>
<Outlet />
</main>
<Scripts />
</body>
</html>
);
}
`,
"app/routes/_index.tsx": js`
import { Link } from "react-router"
export default function Component() {
return <Link to="/parent/child">Go to /parent/child</Link>
}
`,
"app/routes/parent.tsx": js`
import { Outlet, useLoaderData } from "react-router"
export function loader() {
return { message: 'Parent Server Loader' };
}
${
parentClientLoader
? js`
export async function clientLoader({ serverLoader }) {
// Need a small delay to ensure we capture the server-rendered
// fallbacks for assertions
await new Promise(r => setTimeout(r, 100))
let data = await serverLoader();
return { message: data.message + " (mutated by client)" };
}
`
: ""
}
${
parentClientLoaderHydrate
? js`
clientLoader.hydrate = true;
export function HydrateFallback() {
return <p>Parent Fallback</p>
}
`
: ""
}
${parentAdditions || ""}
export default function Component() {
let data = useLoaderData();
return (
<>
<p id="parent-data">{data.message}</p>
<Outlet/>
</>
);
}
`,
"app/routes/parent.child.tsx": js`
import { Form, Outlet, useActionData, useLoaderData } from "react-router"
export function loader() {
return { message: 'Child Server Loader' };
}
export function action() {
return { message: 'Child Server Action' };
}
${
childClientLoader
? js`
export async function clientLoader({ serverLoader }) {
// Need a small delay to ensure we capture the server-rendered
// fallbacks for assertions
await new Promise(r => setTimeout(r, 100))
let data = await serverLoader();
return { message: data.message + " (mutated by client)" };
}
`
: ""
}
${
childClientLoaderHydrate
? js`
clientLoader.hydrate = true;
export function HydrateFallback() {
return <p>Child Fallback</p>
}
`
: ""
}
${childAdditions || ""}
export default function Component() {
let data = useLoaderData();
let actionData = useActionData();
return (
<>
<p id="child-data">{data.message}</p>
<Form method="post">
<button type="submit">Submit</button>
{actionData ? <p id="child-action-data">{actionData.message}</p> : null}
</Form>
</>
);
}
`,
};
}
test.describe("Client Data", () => {
let appFixture: AppFixture;
test.afterAll(() => {
appFixture.close();
});
[true, false].forEach((splitRouteModules) => {
test.describe(`splitRouteModules: ${splitRouteModules}`, () => {
test.describe("clientLoader - critical route module", () => {
test("no client loaders or fallbacks", async ({ page }) => {
appFixture = await createAppFixture(
await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
})
);
let app = new PlaywrightFixture(appFixture, page);
// Full SSR - normal Remix behavior due to lack of clientLoader
await app.goto("/parent/child");
let html = await app.getHtml("main");
expect(html).toMatch("Parent Server Loader");
expect(html).toMatch("Child Server Loader");
});
test("parent.clientLoader/child.clientLoader", async ({ page }) => {
appFixture = await createAppFixture(
await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: true,
parentClientLoaderHydrate: false,
childClientLoader: true,
childClientLoaderHydrate: false,
}),
})
);
let app = new PlaywrightFixture(appFixture, page);
// Full SSR - normal Remix behavior due to lack of HydrateFallback components
await app.goto("/parent/child");
let html = await app.getHtml("main");
expect(html).toMatch("Parent Server Loader");
expect(html).toMatch("Child Server Loader");
});
test("parent.clientLoader.hydrate/child.clientLoader", async ({
page,
}) => {
appFixture = await createAppFixture(
await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: true,
parentClientLoaderHydrate: true,
childClientLoader: true,
childClientLoaderHydrate: false,
}),
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
let html = await app.getHtml("main");
expect(html).toMatch("Parent Fallback");
expect(html).not.toMatch("Parent Server Loader");
expect(html).not.toMatch("Child Server Loader");
await page.waitForSelector("#child-data");
html = await app.getHtml("main");
expect(html).not.toMatch("Parent Fallback");
expect(html).toMatch("Parent Server Loader (mutated by client)");
expect(html).toMatch("Child Server Loader");
});
test("parent.clientLoader/child.clientLoader.hydrate", async ({
page,
}) => {
appFixture = await createAppFixture(
await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: true,
parentClientLoaderHydrate: false,
childClientLoader: true,
childClientLoaderHydrate: true,
}),
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
let html = await app.getHtml("main");
expect(html).toMatch("Parent Server Loader");
expect(html).toMatch("Child Fallback");
expect(html).not.toMatch("Child Server Loader");
await page.waitForSelector("#child-data");
html = await app.getHtml("main");
expect(html).not.toMatch("Child Fallback");
expect(html).toMatch("Parent Server Loader");
expect(html).toMatch("Child Server Loader (mutated by client)");
});
test("parent.clientLoader.hydrate/child.clientLoader.hydrate", async ({
page,
}) => {
appFixture = await createAppFixture(
await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: true,
parentClientLoaderHydrate: true,
childClientLoader: true,
childClientLoaderHydrate: true,
}),
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
let html = await app.getHtml("main");
expect(html).toMatch("Parent Fallback");
expect(html).not.toMatch("Parent Server Loader");
expect(html).not.toMatch("Child Fallback");
expect(html).not.toMatch("Child Server Loader");
await page.waitForSelector("#child-data");
html = await app.getHtml("main");
expect(html).not.toMatch("Parent Fallback");
expect(html).not.toMatch("Child Fallback");
expect(html).toMatch("Parent Server Loader (mutated by client)");
expect(html).toMatch("Child Server Loader (mutated by client)");
});
test("handles synchronous client loaders", async ({ page }) => {
let fixture = await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
parentAdditions: js`
export function clientLoader() {
return { message: "Parent Client Loader" };
}
clientLoader.hydrate=true
export function HydrateFallback() {
return <p>Parent Fallback</p>
}
`,
childAdditions: js`
export function clientLoader() {
return { message: "Child Client Loader" };
}
clientLoader.hydrate=true
`,
}),
});
// Ensure we SSR the fallbacks
let doc = await fixture.requestDocument("/parent/child");
let html = await doc.text();
expect(html).toMatch("Parent Fallback");
appFixture = await createAppFixture(fixture);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
await page.waitForSelector("#child-data");
html = await app.getHtml("main");
expect(html).toMatch("Parent Client Loader");
expect(html).toMatch("Child Client Loader");
});
test("handles deferred data through client loaders", async ({
page,
}) => {
let fixture = await createFixture({
files: {
...getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/parent.child.tsx": js`
import * as React from 'react';
import { json } from "react-router"
import { Await, useLoaderData } from "react-router"
export function loader() {
return {
message: 'Child Server Loader',
lazy: new Promise(r => setTimeout(() => r("Child Deferred Data"), 1000)),
};
}
export async function clientLoader({ serverLoader }) {
let data = await serverLoader();
return {
...data,
message: data.message + " (mutated by client)",
};
}
clientLoader.hydrate = true;
export function HydrateFallback() {
return <p>Child Fallback</p>
}
export default function Component() {
let data = useLoaderData();
return (
<>
<p id="child-data">{data.message}</p>
<React.Suspense fallback={<p>Loading Deferred Data...</p>}>
<Await resolve={data.lazy}>
{(value) => <p id="child-deferred-data">{value}</p>}
</Await>
</React.Suspense>
</>
);
}
`,
},
});
// Ensure initial document request contains the child fallback _and_ the
// subsequent streamed/resolved deferred data
let doc = await fixture.requestDocument("/parent/child");
let html = await doc.text();
expect(html).toMatch("Parent Server Loader");
expect(html).toMatch("Child Fallback");
expect(html).toMatch("Child Deferred Data");
appFixture = await createAppFixture(fixture);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
await page.waitForSelector("#child-deferred-data");
html = await app.getHtml("main");
expect(html).toMatch("Parent Server Loader");
// app.goto() doesn't resolve until the document finishes loading so by
// then the HTML has updated via the streamed suspense updates
expect(html).toMatch("Child Server Loader (mutated by client)");
expect(html).toMatch("Child Deferred Data");
});
test("allows hydration execution without rendering a fallback", async ({
page,
}) => {
let fixture = await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
childAdditions: js`
export async function clientLoader() {
await new Promise(r => setTimeout(r, 100));
return { message: "Child Client Loader" };
}
clientLoader.hydrate=true
`,
}),
});
appFixture = await createAppFixture(fixture);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
let html = await app.getHtml("main");
expect(html).toMatch("Child Server Loader");
await page.waitForSelector(':has-text("Child Client Loader")');
html = await app.getHtml("main");
expect(html).toMatch("Child Client Loader");
});
test("HydrateFallback is not rendered if clientLoader.hydrate is not set (w/server loader)", async ({
page,
}) => {
let fixture = await createFixture({
files: {
...getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/parent.child.tsx": js`
import * as React from 'react';
import { useLoaderData } from "react-router";
export function loader() {
return { message: "Child Server Loader Data" };
}
export async function clientLoader({ serverLoader }) {
await new Promise(r => setTimeout(r, 100));
return { message: "Child Client Loader Data" };
}
export function HydrateFallback() {
return <p>SHOULD NOT SEE ME</p>
}
export default function Component() {
let data = useLoaderData();
return <p id="child-data">{data.message}</p>;
}
`,
},
});
appFixture = await createAppFixture(fixture);
// Ensure initial document request contains the child fallback _and_ the
// subsequent streamed/resolved deferred data
let doc = await fixture.requestDocument("/parent/child");
let html = await doc.text();
expect(html).toMatch("Child Server Loader Data");
expect(html).not.toMatch("SHOULD NOT SEE ME");
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
await page.waitForSelector("#child-data");
html = await app.getHtml("main");
expect(html).toMatch("Child Server Loader Data");
});
test("clientLoader.hydrate is automatically implied when no server loader exists (w HydrateFallback)", async ({
page,
}) => {
appFixture = await createAppFixture(
await createFixture({
files: {
...getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/parent.child.tsx": js`
import * as React from 'react';
import { useLoaderData } from "react-router";
// Even without setting hydrate=true, this should run on hydration
export async function clientLoader({ serverLoader }) {
await new Promise(r => setTimeout(r, 100));
return {
message: "Loader Data (clientLoader only)",
};
}
export function HydrateFallback() {
return <p>Child Fallback</p>
}
export default function Component() {
let data = useLoaderData();
return <p id="child-data">{data.message}</p>;
}
`,
},
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
let html = await app.getHtml("main");
expect(html).toMatch("Child Fallback");
await page.waitForSelector("#child-data");
html = await app.getHtml("main");
expect(html).toMatch("Loader Data (clientLoader only)");
});
test("clientLoader.hydrate is automatically implied when no server loader exists (w/o HydrateFallback)", async ({
page,
}) => {
appFixture = await createAppFixture(
await createFixture({
files: {
...getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/parent.child.tsx": js`
import * as React from 'react';
import { useLoaderData } from "react-router";
// Even without setting hydrate=true, this should run on hydration
export async function clientLoader({ serverLoader }) {
await new Promise(r => setTimeout(r, 100));
return {
message: "Loader Data (clientLoader only)",
};
}
export default function Component() {
let data = useLoaderData();
return <p id="child-data">{data.message}</p>;
}
`,
},
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
let html = await app.getHtml();
expect(html).toMatch(
"💿 Hey developer 👋. You can provide a way better UX than this"
);
expect(html).not.toMatch("child-data");
await page.waitForSelector("#child-data");
html = await app.getHtml("main");
expect(html).toMatch("Loader Data (clientLoader only)");
});
test("throws a 400 if you call serverLoader without a server loader", async ({
page,
}) => {
appFixture = await createAppFixture(
await createFixture({
files: {
...getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/parent.child.tsx": js`
import * as React from 'react';
import { useLoaderData, useRouteError } from "react-router";
export async function clientLoader({ serverLoader }) {
return await serverLoader();
}
export default function Component() {
return <p>Child</p>;
}
export function HydrateFallback() {
return <p>Loading...</p>;
}
export function ErrorBoundary() {
let error = useRouteError();
return <p id="child-error">{error.status} {error.data}</p>;
}
`,
},
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
await page.waitForSelector("#child-error");
let html = await app.getHtml("#child-error");
expect(html.replace(/\n/g, " ").replace(/ +/g, " ")).toMatch(
"400 Error: You are trying to call serverLoader() on a route that does " +
'not have a server loader (routeId: "routes/parent.child")'
);
});
test("initial hydration data check functions properly", async ({
page,
}) => {
appFixture = await createAppFixture(
await createFixture({
files: {
...getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/parent.child.tsx": js`
import * as React from 'react';
import { useLoaderData, useRevalidator } from "react-router";
let isFirstCall = true;
export async function loader({ serverLoader }) {
if (isFirstCall) {
isFirstCall = false
return { message: "Child Server Loader Data (1)" };
}
return { message: "Child Server Loader Data (2+)" };
}
export async function clientLoader({ serverLoader }) {
await new Promise(r => setTimeout(r, 100));
let serverData = await serverLoader();
return {
message: serverData.message + " (mutated by client)",
};
}
clientLoader.hydrate=true;
export default function Component() {
let data = useLoaderData();
let revalidator = useRevalidator();
return (
<>
<p id="child-data">{data.message}</p>
<button onClick={() => revalidator.revalidate()}>Revalidate</button>
</>
);
}
export function HydrateFallback() {
return <p>Loading...</p>
}
`,
},
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
await page.waitForSelector("#child-data");
let html = await app.getHtml();
expect(html).toMatch(
"Child Server Loader Data (1) (mutated by client)"
);
app.clickElement("button");
await page.waitForSelector(
':has-text("Child Server Loader Data (2+)")'
);
html = await app.getHtml("main");
expect(html).toMatch(
"Child Server Loader Data (2+) (mutated by client)"
);
});
test("initial hydration data check functions properly even if serverLoader isn't called on hydration", async ({
page,
}) => {
appFixture = await createAppFixture(
await createFixture({
files: {
...getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/parent.child.tsx": js`
import * as React from 'react';
import { json } from "react-router";
import { useLoaderData, useRevalidator } from "react-router";
let isFirstCall = true;
export async function loader({ serverLoader }) {
if (isFirstCall) {
isFirstCall = false
return { message: "Child Server Loader Data (1)" };
}
return { message: "Child Server Loader Data (2+)" };
}
let isFirstClientCall = true;
export async function clientLoader({ serverLoader }) {
await new Promise(r => setTimeout(r, 100));
if (isFirstClientCall) {
isFirstClientCall = false;
// First time through - don't even call serverLoader
return {
message: "Child Client Loader Data",
};
}
// Only call the serverLoader on subsequent calls and this
// should *not* return us the initialData any longer
let serverData = await serverLoader();
return {
message: serverData.message + " (mutated by client)",
};
}
clientLoader.hydrate=true;
export default function Component() {
let data = useLoaderData();
let revalidator = useRevalidator();
return (
<>
<p id="child-data">{data.message}</p>
<button onClick={() => revalidator.revalidate()}>Revalidate</button>
</>
);
}
export function HydrateFallback() {
return <p>Loading...</p>
}
`,
},
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
await page.waitForSelector("#child-data");
let html = await app.getHtml();
expect(html).toMatch("Child Client Loader Data");
app.clickElement("button");
await page.waitForSelector(
':has-text("Child Server Loader Data (2+)")'
);
html = await app.getHtml("main");
expect(html).toMatch(
"Child Server Loader Data (2+) (mutated by client)"
);
});
test("server loader errors are re-thrown from serverLoader()", async ({
page,
}) => {
let _consoleError = console.error;
console.error = () => {};
appFixture = await createAppFixture(
await createFixture(
{
files: {
...getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/parent.child.tsx": js`
import { ClientLoaderFunctionArgs, useRouteError } from "react-router";
export function loader() {
throw new Error("Broken!")
}
export async function clientLoader({ serverLoader }) {
return await serverLoader();
}
clientLoader.hydrate = true;
export default function Index() {
return <h1>Should not see me</h1>;
}
export function ErrorBoundary() {
let error = useRouteError();
return <p id="child-error">{error.message}</p>;
}
`,
},
},
ServerMode.Development // Avoid error sanitization
),
ServerMode.Development // Avoid error sanitization
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent/child");
let html = await app.getHtml("main");
expect(html).toMatch("Broken!");
// Ensure we hydrate and remain on the boundary
await new Promise((r) => setTimeout(r, 100));
html = await app.getHtml("main");
expect(html).toMatch("Broken!");
expect(html).not.toMatch("Should not see me");
console.error = _consoleError;
});
test("bubbled server loader errors are persisted for hydrating routes", async ({
page,
}) => {
let _consoleError = console.error;
console.error = () => {};
appFixture = await createAppFixture(
await createFixture(
{
files: {
...getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
"app/routes/parent.tsx": js`
import { Outlet, useLoaderData, useRouteLoaderData, useRouteError } from 'react-router'
export function loader() {
return { message: 'Parent Server Loader' };
}
export async function clientLoader({ serverLoader }) {
console.log('running parent client loader')
// Need a small delay to ensure we capture the server-rendered
// fallbacks for assertions
await new Promise(r => setTimeout(r, 100));
let data = await serverLoader();
return { message: data.message + " (mutated by client)" };
}
clientLoader.hydrate = true;
export default function Component() {
let data = useLoaderData();
return (
<>
<p id="parent-data">{data.message}</p>
<Outlet/>
</>
);
}
export function ErrorBoundary() {
let data = useRouteLoaderData("routes/parent")
let error = useRouteError();
return (
<>
<h1>Parent Error</h1>
<p id="parent-data">{data?.message}</p>
<p id="parent-error">{error?.message}</p>
</>
);
}
`,
"app/routes/parent.child.tsx": js`
import { useRouteError, useLoaderData } from 'react-router'
export function loader() {
throw new Error('Child Server Error');
}
export function clientLoader() {
console.log('running child client loader')
return "Should not see me";
}
clientLoader.hydrate = true;
export default function Component() {
let data = useLoaderData()
return (
<>
<p>Should not see me</p>
<p>{data}</p>;
</>
);
}
`,
},
},
ServerMode.Development // Avoid error sanitization
),
ServerMode.Development // Avoid error sanitization
);
let app = new PlaywrightFixture(appFixture, page);
let logs: string[] = [];
page.on("console", (msg) => {
let text = msg.text();
if (
// Chrome logs the 500 as a console error, so skip that since it's not
// what we are asserting against here
/500 \(Internal Server Error\)/.test(text) ||
// Ignore any dev tools messages. This may only happen locally when dev
// tools is installed and not in CI but either way we don't care
/Download the React DevTools/.test(text)
) {
return;
}
logs.push(text);
});
await app.goto("/parent/child", false);
let html = await app.getHtml("main");
expect(html).toMatch("Parent Server Loader</p>");
expect(html).toMatch("Child Server Error");
expect(html).not.toMatch("Should not see me");
// Ensure we hydrate and remain on the boundary
await page.waitForSelector(
":has-text('Parent Server Loader (mutated by client)')"
);
html = await app.getHtml("main");
expect(html).toMatch("Parent Server Loader (mutated by client)</p>");
expect(html).toMatch("Child Server Error");
expect(html).not.toMatch("Should not see me");
expect(logs).toEqual(["running parent client loader"]);
console.error = _consoleError;
});
});
test.describe("clientLoader - lazy route module", () => {
test("no client loaders or fallbacks", async ({ page }) => {
appFixture = await createAppFixture(
await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink("/parent/child");
await page.waitForSelector("#child-data");
// Normal Remix behavior due to lack of clientLoader
let html = await app.getHtml("main");
expect(html).toMatch("Parent Server Loader");
expect(html).toMatch("Child Server Loader");
});
test("parent.clientLoader", async ({ page }) => {
appFixture = await createAppFixture(
await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: true,
parentClientLoaderHydrate: false,
childClientLoader: false,
childClientLoaderHydrate: false,
}),
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink("/parent/child");
await page.waitForSelector("#child-data");
let html = await app.getHtml("main");
expect(html).toMatch("Parent Server Loader (mutated by client)");
expect(html).toMatch("Child Server Loader");
});
test("child.clientLoader", async ({ page }) => {
appFixture = await createAppFixture(
await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: false,
parentClientLoaderHydrate: false,
childClientLoader: true,
childClientLoaderHydrate: false,
}),
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink("/parent/child");
await page.waitForSelector("#child-data");
let html = await app.getHtml("main");
expect(html).toMatch("Parent Server Loader");
expect(html).toMatch("Child Server Loader (mutated by client)");
});
test("parent.clientLoader/child.clientLoader", async ({ page }) => {
appFixture = await createAppFixture(
await createFixture({
files: getFiles({
splitRouteModules,
parentClientLoader: true,
parentClientLoaderHydrate: false,
childClientLoader: true,
childClientLoaderHydrate: false,
}),
})
);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink("/parent/child");
await page.waitForSelector("#child-data");