Hive
perf(server): speed up LiveView dashboard cold start
GitHub issue · Closed
Speed up the cold start of dashboard LiveViews by skipping work that the HTTP dead render does not need and by removing a duplicate project lookup.
Profiling first-page-load behaviour after mise run dev showed that every authenticated dashboard request runs three synchronous Postgres queries before the HTML is returned, even though the data only populates the breadcrumb dropdown menus (which are not interactive until the LiveView WebSocket joins). The same code path also re-queries the project that TuistWeb.Authorization.require_user_can_read_project/1 had just fetched, so each /<account>/<project> mount paid for the project lookup twice.
This PR:
- Gates
get_projects/2andget_user_organization_accounts/1behindconnected?(socket)inLayoutLive.on_mount(:project, ...)andLayoutLive.on_mount(:account, ...). The dead render now skips them entirely; they run once on the connected mount and populate the dropdowns when the LiveView joins. - Returns the authorized project from
Authorization.require_user_can_read_project/1and adds a%{user, project}variant that just authorizes a pre-fetched project.LayoutLive.on_mount(:project, ...)reuses that result instead of issuing its ownProjects.get_project_by_account_and_project_handles/3call, andTuistWeb.App.on_mount(:mount_app, ...)does the same for the public app shell. - Updates the four
LayoutLiveTestcases that assert breadcrumb dropdown contents to mount with a connected socket (%LiveView.Socket{transport_pid: self()}) so they exercise the connected branch where the dropdown queries run.
Net effect per project dashboard request: two fewer Postgres round-trips on the dead render path and one fewer on the connected mount; per account dashboard request: one fewer on the dead render. Behaviour is unchanged from the user’s perspective beyond the dropdowns now populating a tick later (after the WebSocket join), which they already had to wait for to be interactive.
How to test locally
mise run devfromserver/.- Open
http://localhost:8080, sign in astuistrocks@tuist.dev/tuistrocks, and load any project dashboard page. Initial paint should land sooner; breadcrumbs render with their current labels immediately, dropdowns populate when the LiveView connects. mix test test/tuist_web/authorization_test.exs test/tuist_web/live/layout_live_test.exs test/tuist_web/controllers/build_controller_test.exs test/tuist_web/controllers/runs_controller_test.exs— all 28 tests pass.