-
Notifications
You must be signed in to change notification settings - Fork 3
/
EntryPointRoute.tsx
98 lines (87 loc) · 3.12 KB
/
EntryPointRoute.tsx
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
import { type ComponentType, useEffect } from "react";
import type {
EntryPoint,
EntryPointProps,
JSResourceReference,
} from "react-relay";
import { useLoaderData } from "react-router-dom";
import type { OperationType } from "relay-runtime";
import type {
BaseEntryPointComponent,
SimpleEntryPoint,
} from "./entry-point.types";
import { InternalPreload } from "./internal-preload-symbol";
const preloadsToDispose = new Set();
export type PreloadableComponent = ComponentType & {
[InternalPreload]?: {
entryPoint: () => Promise<SimpleEntryPoint<BaseEntryPointComponent, any>>;
resource: () => Promise<unknown>;
};
};
export default function EntryPointRoute(
entryPoint:
| SimpleEntryPoint<BaseEntryPointComponent, any>
| JSResourceReference<SimpleEntryPoint<BaseEntryPointComponent, any>>,
): ComponentType {
const Hoc: PreloadableComponent = () => {
const data = useLoaderData() as EntryPointProps<
Record<string, OperationType>,
Record<string, EntryPoint<any, any> | undefined>,
Record<string, never>,
Record<string, never>
>;
// We need to dispose of preloaded queries when changing routes. React
// router doesn't provide a mechanism for actually accomplishing this so
// we have this effect which attempts to do a deferred cleanup. We use a
// timeout to delay the cleanup to avoid issues when unmounting and re-
// mounting the same component without a new call to the loader function.
useEffect(() => {
if (data.queries == null) {
return;
}
Object.values(data.queries).forEach((preloadedQuery) => {
preloadsToDispose.delete(preloadedQuery);
});
return () => {
Object.values(data.queries).forEach((preloadedQuery) => {
preloadsToDispose.add(preloadedQuery);
});
setTimeout(() => {
Object.values(data.queries).forEach((preloadedQuery) => {
if (preloadsToDispose.delete(preloadedQuery)) {
preloadedQuery.dispose();
}
});
}, 10);
};
}, [data.queries]);
const loadedEntryPoint = (() => {
if (!("load" in entryPoint)) return entryPoint;
const loadedEntryPoint = entryPoint.getModuleIfRequired();
if (!loadedEntryPoint) throw entryPoint.load();
return loadedEntryPoint;
})();
const resource = loadedEntryPoint.root;
const Component = resource.getModuleIfRequired();
if (!Component) throw resource.load();
return <Component {...data} />;
};
Hoc.displayName = `EntryPointRoute(${
"load" in entryPoint
? entryPoint.getModuleId()
: entryPoint.root.getModuleId()
})`;
// This would be much better if it injected a modulepreload link. Unfortunately
// we don't have a mechanism for getting the right bundle file name to put into
// the href. We might be able to do it by building a rollup plugin.
Hoc[InternalPreload] = {
async entryPoint() {
return "load" in entryPoint ? entryPoint.load() : entryPoint;
},
async resource() {
const entryPoint = await this.entryPoint();
return entryPoint.root.load();
},
};
return Hoc;
}