Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd977e2

Browse files
committed
chore: reduce requests the dashboard makes from seeded data
We already inject all of this content in `index.html`. There was also a bug with displaying a loading indicator when the workspace proxies endpoint 404s.
1 parent 7bd1b3b commit fd977e2

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

site/src/api/queries/appearance.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ import { getMetadataAsJSON } from "utils/metadata";
66
const initialAppearanceData = getMetadataAsJSON<AppearanceConfig>("appearance");
77
const appearanceConfigKey = ["appearance"] as const;
88

9-
export const appearance = (): UseQueryOptions<AppearanceConfig> => {
10-
return {
9+
export const appearance = () => {
10+
const opts: UseQueryOptions<AppearanceConfig> = {
1111
queryKey: ["appearance"],
1212
initialData: initialAppearanceData,
1313
queryFn: () => API.getAppearance(),
1414
};
15+
// If we have initial appearance data, we don't want to fetch
16+
// the user again. We already have it!
17+
if (initialAppearanceData) {
18+
opts.cacheTime = Infinity;
19+
opts.staleTime = Infinity;
20+
opts.refetchOnMount = false;
21+
opts.refetchOnReconnect = false;
22+
opts.refetchOnWindowFocus = false;
23+
}
24+
return opts;
1525
};
1626

1727
export const updateAppearance = (queryClient: QueryClient) => {

site/src/api/queries/buildInfo.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ import { getMetadataAsJSON } from "utils/metadata";
66
const initialBuildInfoData = getMetadataAsJSON<BuildInfoResponse>("build-info");
77
const buildInfoKey = ["buildInfo"] as const;
88

9-
export const buildInfo = (): UseQueryOptions<BuildInfoResponse> => {
10-
return {
9+
export const buildInfo = () => {
10+
const opts: UseQueryOptions<BuildInfoResponse> = {
1111
queryKey: buildInfoKey,
1212
initialData: initialBuildInfoData,
1313
queryFn: () => API.getBuildInfo(),
1414
};
15+
// If we have initial build info data, we don't want to fetch
16+
// the user again. We already have it!
17+
if (initialBuildInfoData) {
18+
opts.cacheTime = Infinity;
19+
opts.staleTime = Infinity;
20+
opts.refetchOnMount = false;
21+
opts.refetchOnReconnect = false;
22+
opts.refetchOnWindowFocus = false;
23+
}
24+
return opts;
1525
};

site/src/api/queries/users.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ export const updateRoles = (queryClient: QueryClient) => {
112112
};
113113
};
114114

115+
const initialUserData = getMetadataAsJSON<User>("user");
116+
115117
export const authMethods = () => {
116118
return {
117119
// Even the endpoint being /users/authmethods we don't want to revalidate it
@@ -121,18 +123,26 @@ export const authMethods = () => {
121123
};
122124
};
123125

124-
const initialUserData = getMetadataAsJSON<User>("user");
125-
126126
const meKey = ["me"];
127127

128-
export const me = (): UseQueryOptions<User> & {
129-
queryKey: QueryKey;
130-
} => {
131-
return {
128+
export const me = () => {
129+
const opts: UseQueryOptions<User> & {
130+
queryKey: QueryKey;
131+
} = {
132132
queryKey: meKey,
133133
initialData: initialUserData,
134134
queryFn: API.getAuthenticatedUser,
135135
};
136+
// If we have initial user data, we don't want to fetch
137+
// the user again. We already have it!
138+
if (initialUserData) {
139+
opts.cacheTime = Infinity;
140+
opts.staleTime = Infinity;
141+
opts.refetchOnMount = false;
142+
opts.refetchOnReconnect = false;
143+
opts.refetchOnWindowFocus = false;
144+
}
145+
return opts;
136146
};
137147

138148
export function apiKey(): UseQueryOptions<GenerateAPIKeyResponse> {
@@ -142,8 +152,14 @@ export function apiKey(): UseQueryOptions<GenerateAPIKeyResponse> {
142152
};
143153
}
144154

145-
export const hasFirstUser = () => {
155+
export const hasFirstUser = (): UseQueryOptions<boolean> => {
146156
return {
157+
// If there is initial user data, we don't want to make
158+
// this request. It's a waste!
159+
cacheTime: Infinity,
160+
staleTime: Infinity,
161+
initialData: Boolean(initialUserData),
162+
147163
queryKey: ["hasFirstUser"],
148164
queryFn: API.hasFirstUser,
149165
};

site/src/modules/dashboard/Navbar/NavbarView.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ const ProxyMenu: FC<ProxyMenuProps> = ({ proxyContextValue }) => {
235235
return proxy.healthy && latency !== undefined && latency.at < refetchDate;
236236
};
237237

238+
// This endpoint returns a 404 when not using enterprise.
239+
// If we don't return null, then it looks like this is
240+
// loading forever!
241+
if (proxyContextValue.error) {
242+
return null;
243+
}
244+
238245
if (isLoading) {
239246
return (
240247
<Skeleton

0 commit comments

Comments
 (0)