-
Notifications
You must be signed in to change notification settings - Fork 3.7k
perf(workspace): server-prefetch home, knowledge, tables, and files list pages #5196
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import type { QueryClient } from '@tanstack/react-query' | ||
| import type { WorkspaceFileFolderApi } from '@/lib/api/contracts/workspace-file-folders' | ||
| import type { ListWorkspaceFilesResponse } from '@/lib/api/contracts/workspace-files' | ||
| import { prefetchInternalJson } from '@/app/workspace/[workspaceId]/lib/prefetch-internal-fetch' | ||
| import { workspaceFileFolderKeys } from '@/hooks/queries/workspace-file-folders' | ||
| import { workspaceFilesKeys } from '@/hooks/queries/workspace-files' | ||
|
|
||
| /** | ||
| * Prefetches the Files browser's two lists — workspace files and file folders — | ||
| * under the same query keys their client hooks (`useWorkspaceFiles`, | ||
| * `useWorkspaceFileFolders`) use (scope `active`), so the browser paints | ||
| * populated on first render. | ||
| * | ||
| * Both payloads carry `Date` fields, so they go through their routes and cache | ||
| * the serialized wire shape — see {@link prefetchInternalJson}. | ||
| */ | ||
| export async function prefetchFilesBrowser( | ||
| queryClient: QueryClient, | ||
| workspaceId: string | ||
| ): Promise<void> { | ||
| await Promise.all([ | ||
| queryClient.prefetchQuery({ | ||
| queryKey: workspaceFilesKeys.list(workspaceId, 'active'), | ||
| queryFn: async () => { | ||
| const data = await prefetchInternalJson<ListWorkspaceFilesResponse>( | ||
| `/api/workspaces/${workspaceId}/files?scope=active` | ||
| ) | ||
| return data.success ? data.files : [] | ||
| }, | ||
| staleTime: 30 * 1000, | ||
| }), | ||
| queryClient.prefetchQuery({ | ||
| queryKey: workspaceFileFolderKeys.list(workspaceId, 'active'), | ||
| queryFn: async () => { | ||
| const data = await prefetchInternalJson<{ folders?: WorkspaceFileFolderApi[] }>( | ||
| `/api/workspaces/${workspaceId}/files/folders?scope=active` | ||
| ) | ||
| return data.folders ?? [] | ||
| }, | ||
| staleTime: 30 * 1000, | ||
| }), | ||
| ]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,30 @@ | ||
| import { Suspense } from 'react' | ||
| import { dehydrate, HydrationBoundary } from '@tanstack/react-query' | ||
| import type { Metadata } from 'next' | ||
| import { getSession } from '@/lib/auth' | ||
| import { getQueryClient } from '@/app/_shell/providers/get-query-client' | ||
| import { prefetchHomeLists } from '@/app/workspace/[workspaceId]/home/prefetch' | ||
| import { Home } from './home' | ||
| import { HomeFallback } from './home-fallback' | ||
|
|
||
| export const metadata: Metadata = { | ||
| title: 'New chat', | ||
| } | ||
|
|
||
| export default async function HomePage() { | ||
| export default async function HomePage({ params }: { params: Promise<{ workspaceId: string }> }) { | ||
| const { workspaceId } = await params | ||
|
|
||
| const queryClient = getQueryClient() | ||
| const listsPrefetch = prefetchHomeLists(queryClient, workspaceId) | ||
|
|
||
| const session = await getSession() | ||
| await listsPrefetch | ||
|
|
||
| return ( | ||
| <Suspense fallback={<HomeFallback />}> | ||
| <Home userName={session?.user?.name} userId={session?.user?.id} /> | ||
| </Suspense> | ||
| <HydrationBoundary state={dehydrate(queryClient)}> | ||
| <Suspense fallback={<HomeFallback />}> | ||
| <Home userName={session?.user?.name} userId={session?.user?.id} /> | ||
| </Suspense> | ||
| </HydrationBoundary> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { QueryClient } from '@tanstack/react-query' | ||
| import type { FolderApi } from '@/lib/api/contracts' | ||
| import type { ListWorkspaceFilesResponse } from '@/lib/api/contracts/workspace-files' | ||
| import { prefetchInternalJson } from '@/app/workspace/[workspaceId]/lib/prefetch-internal-fetch' | ||
| import { FOLDER_LIST_STALE_TIME, mapFolder } from '@/hooks/queries/folders' | ||
| import { folderKeys } from '@/hooks/queries/utils/folder-keys' | ||
| import { workspaceFilesKeys } from '@/hooks/queries/workspace-files' | ||
|
|
||
| /** | ||
| * Prefetches the home page's secondary lists — folders and workspace files — | ||
| * under the same query keys their client hooks (`useFolders`, | ||
| * `useWorkspaceFiles`) use, so the home view paints populated on first render. | ||
| * | ||
| * The workflow list (`workflowKeys.list(ws, 'active')`) is already hydrated by | ||
| * the workspace sidebar prefetch and is intentionally not repeated here. | ||
| * | ||
| * Folders are fetched through the route and mapped with the same `mapFolder` | ||
| * the hook applies, matching its cached shape (string dates → `Date`). Files | ||
| * carry `Date` fields, so they go through the route and cache the serialized | ||
| * wire shape — see {@link prefetchInternalJson}. | ||
| */ | ||
| export async function prefetchHomeLists( | ||
| queryClient: QueryClient, | ||
| workspaceId: string | ||
| ): Promise<void> { | ||
| await Promise.all([ | ||
| queryClient.prefetchQuery({ | ||
| queryKey: folderKeys.list(workspaceId, 'active'), | ||
| queryFn: async () => { | ||
| const { folders } = await prefetchInternalJson<{ folders?: FolderApi[] }>( | ||
| `/api/folders?workspaceId=${workspaceId}&scope=active` | ||
| ) | ||
| return (folders ?? []).map(mapFolder) | ||
| }, | ||
| staleTime: FOLDER_LIST_STALE_TIME, | ||
| }), | ||
| queryClient.prefetchQuery({ | ||
| queryKey: workspaceFilesKeys.list(workspaceId, 'active'), | ||
| queryFn: async () => { | ||
| const data = await prefetchInternalJson<ListWorkspaceFilesResponse>( | ||
| `/api/workspaces/${workspaceId}/files?scope=active` | ||
| ) | ||
| return data.success ? data.files : [] | ||
| }, | ||
| staleTime: 30 * 1000, | ||
| }), | ||
| ]) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,26 @@ | ||
| import { dehydrate, HydrationBoundary } from '@tanstack/react-query' | ||
| import type { Metadata } from 'next' | ||
| import { getQueryClient } from '@/app/_shell/providers/get-query-client' | ||
| import { prefetchKnowledgeBases } from '@/app/workspace/[workspaceId]/knowledge/prefetch' | ||
| import { Knowledge } from './knowledge' | ||
|
|
||
| export const metadata: Metadata = { | ||
| title: 'Knowledge Base', | ||
| } | ||
|
|
||
| export default Knowledge | ||
| export default async function KnowledgePage({ | ||
| params, | ||
| }: { | ||
| params: Promise<{ workspaceId: string }> | ||
| }) { | ||
| const { workspaceId } = await params | ||
|
|
||
| const queryClient = getQueryClient() | ||
| await prefetchKnowledgeBases(queryClient, workspaceId) | ||
|
|
||
| return ( | ||
| <HydrationBoundary state={dehydrate(queryClient)}> | ||
| <Knowledge /> | ||
| </HydrationBoundary> | ||
| ) | ||
| } |
28 changes: 28 additions & 0 deletions
28
apps/sim/app/workspace/[workspaceId]/knowledge/prefetch.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { QueryClient } from '@tanstack/react-query' | ||
| import type { KnowledgeBaseData } from '@/lib/api/contracts/knowledge' | ||
| import { prefetchInternalJson } from '@/app/workspace/[workspaceId]/lib/prefetch-internal-fetch' | ||
| import { knowledgeKeys } from '@/hooks/queries/kb/knowledge' | ||
|
|
||
| /** | ||
| * Prefetches the workspace's knowledge-bases list under the same query key the | ||
| * client `useKnowledgeBasesQuery` hook uses (scope `active`), so the list paints | ||
| * populated on first render. | ||
| * | ||
| * The list carries `Date` fields, so it goes through the `/api/knowledge` route | ||
| * and caches the serialized wire shape — see {@link prefetchInternalJson}. | ||
| */ | ||
| export async function prefetchKnowledgeBases( | ||
| queryClient: QueryClient, | ||
| workspaceId: string | ||
| ): Promise<void> { | ||
| await queryClient.prefetchQuery({ | ||
| queryKey: knowledgeKeys.list(workspaceId, 'active'), | ||
| queryFn: async () => { | ||
| const result = await prefetchInternalJson<{ data: KnowledgeBaseData[] }>( | ||
| `/api/knowledge?workspaceId=${workspaceId}&scope=active` | ||
| ) | ||
| return result.data | ||
| }, | ||
| staleTime: 60 * 1000, | ||
| }) | ||
| } |
25 changes: 25 additions & 0 deletions
25
apps/sim/app/workspace/[workspaceId]/lib/prefetch-internal-fetch.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { headers } from 'next/headers' | ||
| import { getInternalApiBaseUrl } from '@/lib/core/utils/urls' | ||
|
|
||
| /** | ||
| * Server-side GET against an internal `/api` route, forwarding the incoming | ||
| * request's cookie so the route authenticates as the current user. | ||
| * | ||
| * List prefetches go through the route (rather than the data layer) when the | ||
| * payload carries `Date` fields: `NextResponse.json` serializes them to the | ||
| * string wire shape the client caches via `requestJson`, so the | ||
| * server-hydrated entry byte-matches the client-fetched one through | ||
| * dehydration. Calling the data layer directly would cache raw `Date` objects | ||
| * and drift from that wire shape. Mirrors the settings/subscription prefetch. | ||
| */ | ||
| export async function prefetchInternalJson<T>(path: string): Promise<T> { | ||
| const cookie = (await headers()).get('cookie') | ||
| // boundary-raw-fetch: server-side RSC prefetch forwarding the session cookie to an internal API route; requestJson is client-only and cannot run here | ||
| const response = await fetch(`${getInternalApiBaseUrl()}${path}`, { | ||
| headers: cookie ? { cookie } : {}, | ||
| }) | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| if (!response.ok) { | ||
| throw new Error(`Prefetch failed for ${path}: ${response.status}`) | ||
| } | ||
| return response.json() as Promise<T> | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.