Skip to content

chore: reduce dashboard requests from seeded data #13034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
kylecarbs committed Apr 22, 2024
commit fd977e22bc41cde5cb671ad1d5964a01886fc5c6
14 changes: 12 additions & 2 deletions site/src/api/queries/appearance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ import { getMetadataAsJSON } from "utils/metadata";
const initialAppearanceData = getMetadataAsJSON<AppearanceConfig>("appearance");
const appearanceConfigKey = ["appearance"] as const;

export const appearance = (): UseQueryOptions<AppearanceConfig> => {
return {
export const appearance = () => {
const opts: UseQueryOptions<AppearanceConfig> = {
queryKey: ["appearance"],
initialData: initialAppearanceData,
queryFn: () => API.getAppearance(),
};
// If we have initial appearance data, we don't want to fetch
// the user again. We already have it!
if (initialAppearanceData) {
opts.cacheTime = Infinity;
opts.staleTime = Infinity;
opts.refetchOnMount = false;
opts.refetchOnReconnect = false;
opts.refetchOnWindowFocus = false;
}
return opts;
};

export const updateAppearance = (queryClient: QueryClient) => {
Expand Down
14 changes: 12 additions & 2 deletions site/src/api/queries/buildInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import { getMetadataAsJSON } from "utils/metadata";
const initialBuildInfoData = getMetadataAsJSON<BuildInfoResponse>("build-info");
const buildInfoKey = ["buildInfo"] as const;

export const buildInfo = (): UseQueryOptions<BuildInfoResponse> => {
return {
export const buildInfo = () => {
const opts: UseQueryOptions<BuildInfoResponse> = {
queryKey: buildInfoKey,
initialData: initialBuildInfoData,
queryFn: () => API.getBuildInfo(),
};
// If we have initial build info data, we don't want to fetch
// the user again. We already have it!
if (initialBuildInfoData) {
opts.cacheTime = Infinity;
opts.staleTime = Infinity;
opts.refetchOnMount = false;
opts.refetchOnReconnect = false;
opts.refetchOnWindowFocus = false;
}
return opts;
};
30 changes: 23 additions & 7 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export const updateRoles = (queryClient: QueryClient) => {
};
};

const initialUserData = getMetadataAsJSON<User>("user");

export const authMethods = () => {
return {
// Even the endpoint being /users/authmethods we don't want to revalidate it
Expand All @@ -121,18 +123,26 @@ export const authMethods = () => {
};
};

const initialUserData = getMetadataAsJSON<User>("user");

const meKey = ["me"];

export const me = (): UseQueryOptions<User> & {
queryKey: QueryKey;
} => {
return {
export const me = () => {
const opts: UseQueryOptions<User> & {
queryKey: QueryKey;
} = {
queryKey: meKey,
initialData: initialUserData,
queryFn: API.getAuthenticatedUser,
};
// If we have initial user data, we don't want to fetch
// the user again. We already have it!
if (initialUserData) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

opts.cacheTime = Infinity;
opts.staleTime = Infinity;
opts.refetchOnMount = false;
opts.refetchOnReconnect = false;
opts.refetchOnWindowFocus = false;
}
return opts;
};

export function apiKey(): UseQueryOptions<GenerateAPIKeyResponse> {
Expand All @@ -142,8 +152,14 @@ export function apiKey(): UseQueryOptions<GenerateAPIKeyResponse> {
};
}

export const hasFirstUser = () => {
export const hasFirstUser = (): UseQueryOptions<boolean> => {
return {
// If there is initial user data, we don't want to make
// this request. It's a waste!
cacheTime: Infinity,
staleTime: Infinity,
initialData: Boolean(initialUserData),

queryKey: ["hasFirstUser"],
queryFn: API.hasFirstUser,
};
Expand Down
7 changes: 7 additions & 0 deletions site/src/modules/dashboard/Navbar/NavbarView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ const ProxyMenu: FC<ProxyMenuProps> = ({ proxyContextValue }) => {
return proxy.healthy && latency !== undefined && latency.at < refetchDate;
};

// This endpoint returns a 404 when not using enterprise.
// If we don't return null, then it looks like this is
// loading forever!
if (proxyContextValue.error) {
return null;
}

if (isLoading) {
return (
<Skeleton
Expand Down
Loading