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
Prev Previous commit
Next Next commit
Fix first user fetch
  • Loading branch information
kylecarbs committed Apr 22, 2024
commit 792aa637a56e1bd2f6b6fcbf3ea6119920d0243b
21 changes: 9 additions & 12 deletions site/src/api/queries/appearance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ import { getMetadataAsJSON } from "utils/metadata";
const initialAppearanceData = getMetadataAsJSON<AppearanceConfig>("appearance");
const appearanceConfigKey = ["appearance"] as const;

export const appearance = () => {
const opts: UseQueryOptions<AppearanceConfig> = {
export const appearance = (): UseQueryOptions<AppearanceConfig> => {
return {
// We either have our initial data or should immediately
// fetch and never again!
cacheTime: Infinity,
staleTime: Infinity,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
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
21 changes: 9 additions & 12 deletions site/src/api/queries/buildInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@ import { getMetadataAsJSON } from "utils/metadata";
const initialBuildInfoData = getMetadataAsJSON<BuildInfoResponse>("build-info");
const buildInfoKey = ["buildInfo"] as const;

export const buildInfo = () => {
const opts: UseQueryOptions<BuildInfoResponse> = {
export const buildInfo = (): UseQueryOptions<BuildInfoResponse> => {
return {
// We either have our initial data or should immediately
// fetch and never again!
cacheTime: Infinity,
staleTime: Infinity,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
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;
};
32 changes: 16 additions & 16 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,21 @@ export const authMethods = () => {

const meKey = ["me"];

export const me = () => {
const opts: UseQueryOptions<User> & {
queryKey: QueryKey;
} = {
export const me = (): UseQueryOptions<User> & {
queryKey: QueryKey;
} => {
return {
// We either have our initial data or should immediately
// fetch and never again!
cacheTime: Infinity,
staleTime: Infinity,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
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) {
opts.cacheTime = Infinity;
opts.staleTime = Infinity;
opts.refetchOnMount = false;
opts.refetchOnReconnect = false;
opts.refetchOnWindowFocus = false;
}
return opts;
};

export function apiKey(): UseQueryOptions<GenerateAPIKeyResponse> {
Expand All @@ -158,8 +155,11 @@ export const hasFirstUser = (): UseQueryOptions<boolean> => {
// this request. It's a waste!
cacheTime: Infinity,
staleTime: Infinity,
initialData: Boolean(initialUserData),

refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
// This cannot be false otherwise it will not fetch!
initialData: typeof initialUserData !== "undefined" ? true : undefined,
queryKey: ["hasFirstUser"],
queryFn: API.hasFirstUser,
};
Expand Down
Loading