-
Notifications
You must be signed in to change notification settings - Fork 3.8k
perf(workspace): stop the sidebar fetching palette data on every route #6670
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
391169f
refactor(prefetch): give the workspace-file seed its own module and t…
waleedlatif1 b6bb89f
perf(sidebar): let the palette fetch its own lists, so closed routes …
waleedlatif1 7cd9d23
perf(prefetch): seed the file list on the pages that render it, not e…
waleedlatif1 c43c934
fix(chat): guard the chat page prefetch behind the chat flag
waleedlatif1 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import type { QueryClient } from '@tanstack/react-query' | ||
| import { getWorkspaceHostContextForViewer } from '@/lib/workspaces/host-context' | ||
| import { seedWorkspaceFiles } from '@/app/workspace/[workspaceId]/lib/seed-workspace-files' | ||
|
|
||
| /** | ||
| * Prefetches what the Home surface needs on top of the workspace layout's own prefetch. | ||
| * | ||
| * Home reads the workspace file list on mount (resource tabs, mentions, the resource picker), so | ||
| * the list is seeded by the routes that render Home rather than by the layout: seeding it in the | ||
| * layout would pay for it on every workspace route, including the ones that never read it. | ||
| * | ||
| * The seed carries no authorization of its own, so the viewer is proved first. This reuses the | ||
| * layout's `cache`d host-context lookup rather than re-deriving the permission, so it costs no | ||
| * additional queries; a viewer without access caches nothing and the client fetch reaches the | ||
| * route for the real 403. | ||
| */ | ||
| export async function prefetchHomeSurface( | ||
| queryClient: QueryClient, | ||
| workspaceId: string, | ||
| userId: string | undefined | ||
| ): Promise<void> { | ||
| if (!userId) return | ||
| const hostContext = await getWorkspaceHostContextForViewer(workspaceId, userId) | ||
| if (!hostContext) return | ||
|
|
||
| await seedWorkspaceFiles(queryClient, workspaceId) | ||
| } |
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
80 changes: 80 additions & 0 deletions
80
apps/sim/app/workspace/[workspaceId]/lib/seed-workspace-files.test.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,80 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockListWorkspaceFilesWithShares } = vi.hoisted(() => ({ | ||
| mockListWorkspaceFilesWithShares: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/workspace-files/queries', () => ({ | ||
| listWorkspaceFilesWithShares: mockListWorkspaceFilesWithShares, | ||
| })) | ||
|
|
||
| /** The key factory lives in a `'use client'` module that pulls emcn's CSS at import. */ | ||
| vi.mock('@sim/emcn', () => ({ | ||
| toast: { success: vi.fn(), error: vi.fn() }, | ||
| })) | ||
|
|
||
| import { | ||
| seedWorkspaceFiles, | ||
| WORKSPACE_FILE_SEED_MAX, | ||
| } from '@/app/workspace/[workspaceId]/lib/seed-workspace-files' | ||
| import { workspaceFilesKeys } from '@/hooks/queries/workspace-files' | ||
|
|
||
| const WORKSPACE_ID = 'ws-123' | ||
|
|
||
| function makeClient() { | ||
| const store = new Map<string, unknown>() | ||
| return { | ||
| setQueryData: (key: readonly unknown[], value: unknown) => | ||
| store.set(JSON.stringify(key), value), | ||
| getQueryData: (key: readonly unknown[]) => store.get(JSON.stringify(key)), | ||
| } as never as import('@tanstack/react-query').QueryClient & { | ||
| getQueryData: (key: readonly unknown[]) => unknown | ||
| } | ||
| } | ||
|
|
||
| describe('seedWorkspaceFiles', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('seeds the file list, bounded by the document payload budget', async () => { | ||
| const files = [{ id: 'file-1', name: 'a.txt' }] | ||
| mockListWorkspaceFilesWithShares.mockResolvedValue(files) | ||
| const client = makeClient() | ||
|
|
||
| await seedWorkspaceFiles(client, WORKSPACE_ID) | ||
|
|
||
| expect(mockListWorkspaceFilesWithShares).toHaveBeenCalledWith(WORKSPACE_ID, 'active', { | ||
| maxRows: WORKSPACE_FILE_SEED_MAX, | ||
| /** A failed read must reach the catch, not degrade to a cached empty list. */ | ||
| throwOnError: true, | ||
| }) | ||
| expect(client.getQueryData(workspaceFilesKeys.list(WORKSPACE_ID, 'active'))).toEqual(files) | ||
| }) | ||
|
|
||
| /** | ||
| * A workspace over the budget seeds NOTHING rather than the prefix that was read: the | ||
| * Files browser renders this list as the workspace's files, so a truncated seed would | ||
| * silently hide some. The client fetch reaches the route for the complete list instead. | ||
| */ | ||
| it('seeds nothing when the workspace exceeds the budget', async () => { | ||
| mockListWorkspaceFilesWithShares.mockResolvedValue(null) | ||
| const client = makeClient() | ||
|
|
||
| await seedWorkspaceFiles(client, WORKSPACE_ID) | ||
|
|
||
| expect(client.getQueryData(workspaceFilesKeys.list(WORKSPACE_ID, 'active'))).toBeUndefined() | ||
| }) | ||
|
|
||
| /** A failed read is an optimization loss, not a render failure. */ | ||
| it('does not throw when the read rejects, and seeds nothing', async () => { | ||
| mockListWorkspaceFilesWithShares.mockRejectedValue(new Error('500')) | ||
| const client = makeClient() | ||
|
|
||
| await expect(seedWorkspaceFiles(client, WORKSPACE_ID)).resolves.toBeUndefined() | ||
| expect(client.getQueryData(workspaceFilesKeys.list(WORKSPACE_ID, 'active'))).toBeUndefined() | ||
| }) | ||
| }) |
48 changes: 48 additions & 0 deletions
48
apps/sim/app/workspace/[workspaceId]/lib/seed-workspace-files.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,48 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
| import type { QueryClient } from '@tanstack/react-query' | ||
| import { listWorkspaceFilesWithShares } from '@/lib/workspace-files/queries' | ||
| import { workspaceFilesKeys } from '@/hooks/queries/workspace-files' | ||
|
|
||
| const logger = createLogger('SeedWorkspaceFiles') | ||
|
|
||
| /** | ||
| * How many files a page is willing to inline into its document. At ~500 bytes of JSON per | ||
| * file this budgets the entry at ~150 KB. | ||
| * | ||
| * A workspace above the budget seeds NOTHING rather than a prefix: the Files browser | ||
| * renders this list as the workspace's files, so a truncated seed would silently hide some. | ||
| */ | ||
| export const WORKSPACE_FILE_SEED_MAX = 300 | ||
|
|
||
| /** | ||
| * Seeds the workspace's file list for the pages that render it. | ||
| * | ||
| * Seeded rather than prefetched so it can decline to create an entry at all above | ||
| * {@link WORKSPACE_FILE_SEED_MAX} — `prefetchQuery` always creates one, and a partial | ||
| * entry would be read as the whole list. Parsed through the route's response contract, so | ||
| * a seeded entry matches what a client fetch caches. | ||
| */ | ||
| export async function seedWorkspaceFiles( | ||
| queryClient: QueryClient, | ||
| workspaceId: string | ||
| ): Promise<void> { | ||
| try { | ||
| const files = await listWorkspaceFilesWithShares(workspaceId, 'active', { | ||
| maxRows: WORKSPACE_FILE_SEED_MAX, | ||
| /** | ||
| * A failed read must reach the catch below, not degrade to an empty list: seeding | ||
| * `[]` would cache "this workspace has no files" as authoritative for the entry's | ||
| * lifetime, which is worse than seeding nothing and letting the client fetch. | ||
| */ | ||
| throwOnError: true, | ||
| }) | ||
| if (!files) return | ||
| queryClient.setQueryData(workspaceFilesKeys.list(workspaceId, 'active'), files) | ||
| } catch (error) { | ||
| /** Optimization only: the client fetch reaches the route instead. */ | ||
| logger.warn('Workspace file list seed failed; client will fetch', { | ||
| error: getErrorMessage(error), | ||
| }) | ||
| } | ||
| } |
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.