Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
improvement(sidebar): prefetch workspace list server-side so the switcher never flashes loading#5706
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
improvement(sidebar): prefetch workspace list server-side so the switcher never flashes loading #5706
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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
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,9 +1,10 @@ | ||
| import type { QueryClient } from '@tanstack/react-query' | ||
| import type { WorkspaceHostContext } from '@/lib/api/contracts/workspaces' | ||
| import { listWorkspacesContract, type WorkspaceHostContext } from '@/lib/api/contracts/workspaces' | ||
| import { listMothershipChats } from '@/lib/copilot/chat/list-mothership-chats' | ||
| import { listFoldersForWorkspace } from '@/lib/folders/queries' | ||
| import { listWorkflowsForUser } from '@/lib/workflows/queries' | ||
| import { getWorkspaceHostContextForViewer } from '@/lib/workspaces/host-context' | ||
| import { listWorkspacesForViewer } from '@/lib/workspaces/list' | ||
| import { getWorkspacePermissionsForAuthorizedViewer } from '@/lib/workspaces/permissions/utils' | ||
| import { FOLDER_LIST_STALE_TIME, mapFolder } from '@/hooks/queries/folders' | ||
| import { | ||
| @@ -14,6 +15,10 @@ import { | ||
| import { folderKeys } from '@/hooks/queries/utils/folder-keys' | ||
| import { workflowKeys } from '@/hooks/queries/utils/workflow-keys' | ||
| import { mapWorkflow, WORKFLOW_LIST_STALE_TIME } from '@/hooks/queries/utils/workflow-list-query' | ||
| import { | ||
| normalizeWorkspacesResponse, | ||
| WORKSPACE_LIST_STALE_TIME, | ||
| } from '@/hooks/queries/utils/workspace-list-query' | ||
| import { WORKSPACE_PERMISSIONS_STALE_TIME, workspaceKeys } from '@/hooks/queries/workspace' | ||
| import { | ||
| WORKSPACE_HOST_CONTEXT_STALE_TIME, | ||
| @@ -37,22 +42,27 @@ export function prefetchWorkspaceHostContext( | ||
| } | ||
| /** | ||
| * Prefetches the sidebar's workflow, chat, folder, and workspace-permissions lists for | ||
| * a workspace and stores them under the same query keys + mappers the client hooks use, | ||
| * so the persistent sidebar paints populated on the first server render instead of | ||
| * flashing skeletons on a cold load (e.g. after the browser discards an idle tab). Calls | ||
| * the data layer directly — the same functions the API routes use — with no internal | ||
| * HTTP hop. | ||
| * Prefetches the sidebar's workflow, chat, folder, workspace-permissions, and | ||
| * workspace lists for a workspace and stores them under the same query keys + | ||
| * mappers the client hooks use, so the persistent sidebar (including the | ||
| * workspace switcher header) paints populated on the first server render | ||
| * instead of flashing skeletons on a cold load (e.g. after the browser | ||
| * discards an idle tab). Calls the data layer directly — the same functions | ||
| * the API routes use — with no internal HTTP hop. | ||
| * | ||
| * The host context is the authorization proof for this server-render pass, so | ||
| * permission prefetch can reuse its effective permission without repeating | ||
| * workspace and membership reads. | ||
| * workspace and membership reads. It also proves the viewer has at least one | ||
| * accessible workspace, which is why the workspace-list prefetch can safely | ||
| * skip the route's empty-list default-workspace creation path — and the | ||
| * route's orphaned-workflow repair, which still runs on client refetches. | ||
| */ | ||
| export async function prefetchWorkspaceSidebar( | ||
| queryClient: QueryClient, | ||
| workspaceId: string, | ||
| userId: string, | ||
| hostContext: WorkspaceHostContext | ||
| hostContext: WorkspaceHostContext, | ||
| activeOrganizationId: string | null | ||
| ): Promise<void> { | ||
| if (hostContext.workspace.id !== workspaceId) return | ||
| await Promise.all([ | ||
| @@ -80,6 +90,27 @@ export async function prefetchWorkspaceSidebar( | ||
| }, | ||
| staleTime: FOLDER_LIST_STALE_TIME, | ||
| }), | ||
| queryClient.prefetchQuery({ | ||
| queryKey: workspaceKeys.list('active'), | ||
| queryFn: async () => { | ||
| const payload = await listWorkspacesForViewer({ | ||
| userId, | ||
| activeOrganizationId, | ||
| scope: 'active', | ||
| }) | ||
| // An empty list means GET /api/workspaces' default-workspace creation | ||
| // path must run — throw so prefetchQuery caches nothing and the client | ||
| // fetch reaches the route. | ||
| if (payload.workspaces.length === 0) { | ||
| throw new Error('Empty workspace list requires the route creation path') | ||
| } | ||
| // Parsing through the route contract's response schema strips the same | ||
| // server-only fields `requestJson` strips on the client, guaranteeing the | ||
| // cached shape is identical to a client fetch. | ||
| return normalizeWorkspacesResponse(listWorkspacesContract.response.schema.parse(payload)) | ||
| }, | ||
| staleTime: WORKSPACE_LIST_STALE_TIME, | ||
| }), | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| queryClient.prefetchQuery({ | ||
| queryKey: workspaceKeys.permissions(workspaceId), | ||
| queryFn: () => | ||
18 changes: 11 additions & 7 deletions
18 ...space/[workspaceId]/w/components/sidebar/components/workspace-header/workspace-header.tsx
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.