Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: prefetch project homepage (#30317)

Alaister Youngc59be649a377
diff --git a/apps/studio/components/interfaces/Home/ProjectList/ProjectCard.tsx b/apps/studio/components/interfaces/Home/ProjectList/ProjectCard.tsx
index 1c6cbd01585..bab4d0c394f 100644
--- a/apps/studio/components/interfaces/Home/ProjectList/ProjectCard.tsx
+++ b/apps/studio/components/interfaces/Home/ProjectList/ProjectCard.tsx
@@ -3,6 +3,7 @@ import { GitBranch, Github } from 'lucide-react'
import CardButton from 'components/ui/CardButton'
import { ComputeBadgeWrapper } from 'components/ui/ComputeBadgeWrapper'
import type { IntegrationProjectConnection } from 'data/integrations/integrations.types'
+import { ProjectIndexPageLink } from 'data/prefetchers/project.$ref'
import type { ProjectInfo } from 'data/projects/projects-query'
import type { ResourceWarning } from 'data/usage/resource-warnings-query'
import { BASE_PATH } from 'lib/constants'
@@ -73,6 +74,7 @@ const ProjectCard = ({
footer={
<ProjectCardStatus projectStatus={projectStatus} resourceWarnings={resourceWarnings} />
}
+ containerElement={<ProjectIndexPageLink projectRef={projectRef} />}
/>
</li>
)
diff --git a/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.tsx b/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.tsx
index 8fd77400bda..0032c511051 100644
--- a/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.tsx
+++ b/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.tsx
@@ -12,6 +12,7 @@ import {
useIsDatabaseFunctionsAssistantEnabled,
} from 'components/interfaces/App/FeaturePreview/FeaturePreviewContext'
import { useProjectLintsQuery } from 'data/lint/lint-query'
+import { ProjectIndexPageLink } from 'data/prefetchers/project.$ref'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useLocalStorageQuery } from 'hooks/misc/useLocalStorage'
import { useFlag } from 'hooks/ui/useFlag'
@@ -263,6 +264,7 @@ const NavigationBar = () => {
label: 'Home',
icon: <Home size={ICON_SIZE} strokeWidth={ICON_STROKE_WIDTH} />,
link: `/project/${projectRef}`,
+ linkElement: <ProjectIndexPageLink projectRef={projectRef} />,
}}
onClick={onCloseNavigationIconLink}
/>
diff --git a/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.utils.tsx b/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.utils.tsx
index 3b92106f0d6..6d695d60116 100644
--- a/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.utils.tsx
+++ b/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.utils.tsx
@@ -26,7 +26,7 @@ export const generateToolRoutes = (ref?: string, project?: Project): Route[] =>
label: 'Table Editor',
icon: <TableEditor size={ICON_SIZE} strokeWidth={ICON_STROKE_WIDTH} />,
link: ref && (isProjectBuilding ? buildingUrl : `/project/${ref}/editor`),
- linkComponent: <EditorIndexPageLink projectRef={ref} />,
+ linkElement: <EditorIndexPageLink projectRef={ref} />,
},
{
key: 'sql',
diff --git a/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationIconLink.tsx b/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationIconLink.tsx
index 758deedc315..d8ff0949a26 100644
--- a/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationIconLink.tsx
+++ b/apps/studio/components/layouts/ProjectLayout/NavigationBar/NavigationIconLink.tsx
@@ -48,8 +48,8 @@ const NavigationIconLink = forwardRef<HTMLAnchorElement, NavigationIconButtonPro
const LinkComponent = forwardRef<HTMLAnchorElement, ComponentPropsWithoutRef<typeof Link>>(
function LinkComponent(props, ref) {
- if (route.linkComponent && isValidElement(route.linkComponent)) {
- return cloneElement<any>(route.linkComponent, { ...props, ref })
+ if (route.linkElement && isValidElement(route.linkElement)) {
+ return cloneElement<any>(route.linkElement, { ...props, ref })
}
return <Link ref={ref} {...props} />
diff --git a/apps/studio/components/ui/CardButton.tsx b/apps/studio/components/ui/CardButton.tsx
index ab15ba26513..ee56413b87d 100644
--- a/apps/studio/components/ui/CardButton.tsx
+++ b/apps/studio/components/ui/CardButton.tsx
@@ -1,6 +1,7 @@
-import { Loader, ChevronRight } from 'lucide-react'
+import { ChevronRight, Loader } from 'lucide-react'
import Link from 'next/link'
-import React, { PropsWithChildren } from 'react'
+import React, { cloneElement, PropsWithChildren } from 'react'
+
import { cn } from 'ui'
interface CardButtonProps {
@@ -18,6 +19,7 @@ interface CardButtonProps {
fixedHeight?: boolean
hideChevron?: boolean
titleClass?: string
+ containerElement?: React.ReactNode
}
// Define separate interfaces for each type of container
@@ -56,6 +58,7 @@ const CardButton = ({
fixedHeight = true,
hideChevron = false,
titleClass = '',
+ containerElement,
...props
}: PropsWithChildren<CardButtonProps & ContainerProps>) => {
const isLink = url || linkHref || props.onClick
@@ -63,23 +66,28 @@ const CardButton = ({
let Container: React.ElementType
let containerProps: ContainerProps = {}
+ const ContainerComponentOverride =
+ containerElement && React.isValidElement(containerElement)
+ ? (props: any) => cloneElement<any>(containerElement, { ...props })
+ : undefined
+
if (props.onClick) {
- Container = 'button'
+ Container = ContainerComponentOverride ?? 'button'
containerProps = props
} else if (linkHref) {
- Container = Link
+ Container = ContainerComponentOverride ?? Link
containerProps = {
href: linkHref,
...props,
}
} else if (url) {
- Container = 'a'
+ Container = ContainerComponentOverride ?? 'a'
containerProps = {
href: url,
...props,
}
} else {
- Container = 'div'
+ Container = ContainerComponentOverride ?? 'div'
containerProps = props
}
diff --git a/apps/studio/components/ui/ui.types.ts b/apps/studio/components/ui/ui.types.ts
index 2aee180817c..c1ef94670f2 100644
--- a/apps/studio/components/ui/ui.types.ts
+++ b/apps/studio/components/ui/ui.types.ts
@@ -6,5 +6,5 @@ export interface Route {
icon: ReactNode
link?: string
disabled?: boolean
- linkComponent?: ReactNode
+ linkElement?: ReactNode
}
diff --git a/apps/studio/data/analytics/project-log-requests-count-query.ts b/apps/studio/data/analytics/project-log-requests-count-query.ts
index 310ea107b65..0d6e5b5863d 100644
--- a/apps/studio/data/analytics/project-log-requests-count-query.ts
+++ b/apps/studio/data/analytics/project-log-requests-count-query.ts
@@ -1,4 +1,4 @@
-import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
@@ -48,3 +48,12 @@ export const useProjectLogRequestsCountQuery = <TData = ProjectLogRequestsCountD
...options,
}
)
+
+export function prefetchProjectLogRequestsCount(
+ client: QueryClient,
+ { projectRef }: ProjectLogRequestsCountVariables
+) {
+ return client.fetchQuery(analyticsKeys.usageApiRequestsCount(projectRef), ({ signal }) =>
+ getProjectLogRequestsCountStats({ projectRef }, signal)
+ )
+}
diff --git a/apps/studio/data/analytics/project-log-stats-query.ts b/apps/studio/data/analytics/project-log-stats-query.ts
index d29c1dce999..1374d3ba67e 100644
--- a/apps/studio/data/analytics/project-log-stats-query.ts
+++ b/apps/studio/data/analytics/project-log-stats-query.ts
@@ -1,4 +1,4 @@
-import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, isResponseOk } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { analyticsKeys } from './keys'
@@ -61,3 +61,12 @@ export const useProjectLogStatsQuery = <TData = ProjectLogStatsData>(
...options,
}
)
+
+export function prefetchProjectLogStats(
+ client: QueryClient,
+ { projectRef, interval }: ProjectLogStatsVariables
+) {
+ return client.fetchQuery(analyticsKeys.usageApiCounts(projectRef, interval), ({ signal }) =>
+ getProjectLogStats({ projectRef, interval }, signal)
+ )
+}
diff --git a/apps/studio/data/prefetchers/project.$ref.editor.$id.tsx b/apps/studio/data/prefetchers/project.$ref.editor.$id.tsx
index 74e2269059e..054f2c906f1 100644
--- a/apps/studio/data/prefetchers/project.$ref.editor.$id.tsx
+++ b/apps/studio/data/prefetchers/project.$ref.editor.$id.tsx
@@ -39,30 +39,34 @@ export function usePrefetchEditorTablePage() {
projectRef: project.ref,
connectionString: project.connectionString,
id,
- }).then((entity) => {
- if (entity) {
- const supaTable = parseSupaTable(entity)
+ })
+ .then((entity) => {
+ if (entity) {
+ const supaTable = parseSupaTable(entity)
- const { sorts: localSorts = [], filters: localFilters = [] } =
- loadTableEditorSortsAndFiltersFromLocalStorage(
- project.ref,
- entity.name,
- entity.schema
- ) ?? {}
+ const { sorts: localSorts = [], filters: localFilters = [] } =
+ loadTableEditorSortsAndFiltersFromLocalStorage(
+ project.ref,
+ entity.name,
+ entity.schema
+ ) ?? {}
- prefetchTableRows(queryClient, {
- queryKey: [supaTable.schema, supaTable.name],
- projectRef: project?.ref,
- connectionString: project?.connectionString,
- table: supaTable,
- sorts: sorts ?? formatSortURLParams(supaTable.name, localSorts),
- filters: filters ?? formatFilterURLParams(localFilters),
- page: 1,
- limit: TABLE_EDITOR_DEFAULT_ROWS_PER_PAGE,
- impersonatedRole: roleImpersonationState.role,
- })
- }
- })
+ prefetchTableRows(queryClient, {
+ queryKey: [supaTable.schema, supaTable.name],
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ table: supaTable,
+ sorts: sorts ?? formatSortURLParams(supaTable.name, localSorts),
+ filters: filters ?? formatFilterURLParams(localFilters),
+ page: 1,
+ limit: TABLE_EDITOR_DEFAULT_ROWS_PER_PAGE,
+ impersonatedRole: roleImpersonationState.role,
+ })
+ }
+ })
+ .catch(() => {
+ // eat prefetching errors as they are not critical
+ })
},
[project, queryClient, roleImpersonationState.role, router, tableEditorPrefetchingEnabled]
)
diff --git a/apps/studio/data/prefetchers/project.$ref.editor.tsx b/apps/studio/data/prefetchers/project.$ref.editor.tsx
index e21d6fed797..e460ca0d063 100644
--- a/apps/studio/data/prefetchers/project.$ref.editor.tsx
+++ b/apps/studio/data/prefetchers/project.$ref.editor.tsx
@@ -1,5 +1,6 @@
import { useQueryClient } from '@tanstack/react-query'
import Link from 'next/link'
+import { useRouter } from 'next/router'
import { ComponentProps, PropsWithChildren, useCallback } from 'react'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
@@ -8,7 +9,6 @@ import { ENTITY_TYPE } from 'data/entity-types/entity-type-constants'
import { prefetchEntityTypes } from 'data/entity-types/entity-types-infinite-query'
import { useLocalStorage } from 'hooks/misc/useLocalStorage'
import { useFlag } from 'hooks/ui/useFlag'
-import { useRouter } from 'next/router'
export function usePrefetchEditorIndexPage() {
const router = useRouter()
@@ -31,12 +31,16 @@ export function usePrefetchEditorIndexPage() {
prefetchSchemas(queryClient, {
projectRef: project.ref,
connectionString: project.connectionString,
+ }).catch(() => {
+ // eat prefetching errors as they are not critical
})
prefetchEntityTypes(queryClient, {
projectRef: project?.ref,
connectionString: project?.connectionString,
sort: entityTypesSort,
filterTypes: Object.values(ENTITY_TYPE),
+ }).catch(() => {
+ // eat prefetching errors as they are not critical
})
}, [entityTypesSort, project, queryClient, router])
}
diff --git a/apps/studio/data/prefetchers/project.$ref.tsx b/apps/studio/data/prefetchers/project.$ref.tsx
new file mode 100644
index 00000000000..9b826cc45fd
--- /dev/null
+++ b/apps/studio/data/prefetchers/project.$ref.tsx
@@ -0,0 +1,67 @@
+import { useQueryClient } from '@tanstack/react-query'
+import Link from 'next/link'
+import { useRouter } from 'next/router'
+import { ComponentProps, PropsWithChildren, useCallback } from 'react'
+
+import { prefetchProjectLogRequestsCount } from 'data/analytics/project-log-requests-count-query'
+import { prefetchProjectLogStats } from 'data/analytics/project-log-stats-query'
+import { prefetchProjectDetail } from 'data/projects/project-detail-query'
+
+export function usePrefetchProjectIndexPage() {
+ const router = useRouter()
+ const queryClient = useQueryClient()
+
+ return useCallback(
+ ({ projectRef }: { projectRef?: string }) => {
+ // Prefetch code
+ router.prefetch(`/project/${projectRef}`)
+
+ // Prefetch data
+ prefetchProjectDetail(queryClient, {
+ ref: projectRef,
+ }).catch(() => {
+ // eat prefetching errors as they are not critical
+ })
+
+ prefetchProjectLogRequestsCount(queryClient, {
+ projectRef,
+ }).catch(() => {
+ // eat prefetching errors as they are not critical
+ })
+
+ prefetchProjectLogStats(queryClient, {
+ projectRef,
+ interval: 'hourly',
+ }).catch(() => {
+ // eat prefetching errors as they are not critical
+ })
+ },
+ [queryClient, router]
+ )
+}
+
+type LinkProps = ComponentProps<typeof Link>
+
+interface ProjectIndexPageLinkProps extends Omit<LinkProps, 'href'> {
+ projectRef?: string
+ href?: LinkProps['href']
+}
+
+export function ProjectIndexPageLink({
+ href,
+ projectRef,
+ children,
+ ...props
+}: PropsWithChildren<ProjectIndexPageLinkProps>) {
+ const prefetch = usePrefetchProjectIndexPage()
+
+ return (
+ <Link
+ href={href || `/project/${projectRef}`}
+ onMouseEnter={() => prefetch({ projectRef })}
+ {...props}
+ >
+ {children}
+ </Link>
+ )
+}
diff --git a/apps/studio/data/projects/project-detail-query.ts b/apps/studio/data/projects/project-detail-query.ts
index 6f791b0c85b..dfee1c1dcfc 100644
--- a/apps/studio/data/projects/project-detail-query.ts
+++ b/apps/studio/data/projects/project-detail-query.ts
@@ -62,15 +62,8 @@ export function invalidateProjectDetailsQuery(client: QueryClient, ref: string)
return client.invalidateQueries(projectKeys.detail(ref))
}
-// get the cached value or fallback to fetching it
-export async function getCachedProjectDetail(
- client: QueryClient,
- ref: string | undefined
-): Promise<ProjectDetailData | undefined> {
- if (!ref) return undefined
-
- const cached = client.getQueryData<ProjectDetailData>(projectKeys.detail(ref))
- if (cached) return cached
-
- return await client.fetchQuery<ProjectDetailData, ProjectDetailError>(projectKeys.detail(ref))
+export function prefetchProjectDetail(client: QueryClient, { ref }: ProjectDetailVariables) {
+ return client.fetchQuery(projectKeys.detail(ref), ({ signal }) =>
+ getProjectDetail({ ref }, signal)
+ )
}