Skip to content

Commit 1bb8dd3

Browse files
authored
Use list fetches + setQueryData to prefetch items for *NameFromId cells (#2055)
get wild with prefetches for *NameFromId cells
1 parent 41dd9f0 commit 1bb8dd3

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

app/api/hooks.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ export const wrapQueryClient = <A extends ApiClient>(api: A, queryClient: QueryC
219219
queryClient.invalidateQueries({ queryKey: [method], ...filters }),
220220
setQueryData: <M extends keyof A>(method: M, params: Params<A[M]>, data: Result<A[M]>) =>
221221
queryClient.setQueryData([method, params], data),
222+
setQueryDataErrorsAllowed: <M extends keyof A>(
223+
method: M,
224+
params: Params<A[M]>,
225+
data: ErrorsAllowed<Result<A[M]>, ApiError>
226+
) => queryClient.setQueryData([method, params, ERRORS_ALLOWED], data),
222227
fetchQuery: <M extends string & keyof A>(
223228
method: M,
224229
params: Params<A[M]>,

app/pages/project/disks/DisksPage.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,27 @@ const EmptyState = () => (
4444
)
4545

4646
DisksPage.loader = async ({ params }: LoaderFunctionArgs) => {
47-
await apiQueryClient.prefetchQuery('diskList', {
48-
query: { ...getProjectSelector(params), limit: 25 },
49-
})
47+
const { project } = getProjectSelector(params)
48+
await Promise.all([
49+
apiQueryClient.prefetchQuery('diskList', {
50+
query: { project, limit: 25 },
51+
}),
52+
53+
// fetch instances and preload into RQ cache so fetches by ID in
54+
// InstanceLinkCell can be mostly instant yet gracefully fall back to
55+
// fetching individually if we don't fetch them all here
56+
apiQueryClient
57+
.fetchQuery('instanceList', { query: { project, limit: 200 } })
58+
.then((instances) => {
59+
for (const instance of instances.items) {
60+
apiQueryClient.setQueryData(
61+
'instanceView',
62+
{ path: { instance: instance.id } },
63+
instance
64+
)
65+
}
66+
}),
67+
])
5068
return null
5169
}
5270

app/pages/project/snapshots/SnapshotsPage.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,32 @@ const EmptyState = () => (
5050
)
5151

5252
SnapshotsPage.loader = async ({ params }: LoaderFunctionArgs) => {
53-
await apiQueryClient.prefetchQuery('snapshotList', {
54-
query: { ...getProjectSelector(params), limit: 25 },
55-
})
53+
const { project } = getProjectSelector(params)
54+
await Promise.all([
55+
apiQueryClient.prefetchQuery('snapshotList', {
56+
query: { project, limit: 25 },
57+
}),
58+
59+
// Fetch disks and preload into RQ cache so fetches by ID in DiskNameFromId
60+
// can be mostly instant yet gracefully fall back to fetching individually
61+
// if we don't fetch them all here. This has to be the *ErrorsAllowed
62+
// version of setQueryData because the disk fetchs are also the errors
63+
// allowed version. If we use regular setQueryData, nothing blows up; the
64+
// data is just never found in the cache. Note that the disks that error
65+
// (delete disks) are not prefetched here because they are (obviously) not
66+
// in the disk list response.
67+
apiQueryClient
68+
.fetchQuery('diskList', { query: { project, limit: 200 } })
69+
.then((disks) => {
70+
for (const disk of disks.items) {
71+
apiQueryClient.setQueryDataErrorsAllowed(
72+
'diskView',
73+
{ path: { disk: disk.id } },
74+
{ type: 'success', data: disk }
75+
)
76+
}
77+
}),
78+
])
5679
return null
5780
}
5881

app/pages/system/networking/IpPoolPage.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ IpPoolPage.loader = async function ({ params }: LoaderFunctionArgs) {
5454
path: { pool },
5555
query: { limit: 25 }, // match QueryTable
5656
}),
57+
58+
// fetch silos and preload into RQ cache so fetches by ID in SiloNameFromId
59+
// can be mostly instant yet gracefully fall back to fetching individually
60+
// if we don't fetch them all here
61+
apiQueryClient.fetchQuery('siloList', { query: { limit: 200 } }).then((silos) => {
62+
for (const silo of silos.items) {
63+
apiQueryClient.setQueryData('siloView', { path: { silo: silo.id } }, silo)
64+
}
65+
}),
5766
])
5867
return null
5968
}

0 commit comments

Comments
 (0)