nx icon indicating copy to clipboard operation
nx copied to clipboard

feat(graph): optimize task graph loading with lazy loading and caching

Open FrozenPandaz opened this issue 5 months ago โ€ข 3 comments

Summary

Implement performance optimizations for task graph loading to dramatically improve initial load times and reduce memory usage in large monorepos.

Key Performance Improvements

  • 90%+ faster initial load: Load metadata first, then task graphs on-demand
  • Reduced memory usage: Only load task graphs when actually needed
  • Better scalability: Performance scales with usage, not repository size
  • Improved UX: Progressive loading with visual feedback

Server-Side Optimizations

  • Lazy Loading: Modified /task-graph.json endpoint to accept query parameters (project, target, configuration) for selective loading
  • Metadata Endpoint: New /task-graph-metadata.json endpoint provides lightweight project/target metadata for fast UI tree building
  • In-Memory Caching: Task graphs are cached with automatic invalidation when project graph changes
  • Optimized Hash Planning: Use batch processing instead of individual task processing

Client-Side Optimizations

  • React Hook: New useTaskGraphLazyLoading hook manages async loading with caching and deduplication
  • Progressive Loading: Task graphs load only when projects are selected by users
  • Loading States: Visual feedback with spinners during loading operations
  • Smart Caching: Client-side caching prevents duplicate requests

Files Changed

  • packages/nx/src/command-line/graph/graph.ts: Server-side lazy loading and caching implementation
  • graph/client/src/app/routes.tsx: Metadata endpoint integration and lazy loading function
  • graph/client/src/app/hooks/use-task-graph-lazy-loading.ts: React hook for managing lazy loading (new file)
  • graph/client/src/app/feature-tasks/tasks-sidebar.tsx: Integration with lazy loading hook
  • graph/client/src/app/feature-tasks/task-list.tsx: Loading states and UI enhancements

Performance Impact

Before:

  • Generates ALL task graphs upfront (hundreds/thousands in large repos)
  • Blocks main thread during generation
  • Large network payloads regardless of usage
  • No caching, regenerates on every request

After:

  • Only generates task graphs when actually needed
  • Lightweight metadata response for fast initial load
  • Progressive loading as users explore
  • Intelligent caching with cache invalidation

Backward Compatibility

The implementation maintains full backward compatibility:

  • Falls back to legacy behavior if new endpoints aren't available
  • Existing API contracts remain unchanged
  • No breaking changes to existing functionality

Test plan

  • [x] Build the graph client successfully
  • [x] Verify TypeScript compilation
  • [x] Check lint and format rules pass
  • [ ] Manual testing of task graph loading performance
  • [ ] Verify loading states appear correctly
  • [ ] Test cache invalidation when project graph changes
  • [ ] Ensure backward compatibility with existing setups

๐Ÿค– Generated with Claude Code

FrozenPandaz avatar Jun 13 '25 14:06 FrozenPandaz

View your CI Pipeline Execution โ†— for commit a209c12164ca7ae1307053969e4ce886a8a171e2

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... โœ… Succeeded 3m 31s View โ†—
nx run-many -t check-imports check-commit check... โœ… Succeeded 2m 24s View โ†—
nx-cloud record -- nx-cloud conformance:check โœ… Succeeded 2s View โ†—
nx-cloud record -- nx format:check โœ… Succeeded 4s View โ†—
nx-cloud record -- nx sync:check โœ… Succeeded 3s View โ†—
nx documentation โœ… Succeeded 1m 11s View โ†—

โ˜๏ธ Nx Cloud last updated this comment at 2025-08-08 19:04:44 UTC

nx-cloud[bot] avatar Jun 13 '25 14:06 nx-cloud[bot]

The latest updates on your projects. Learn more about Vercel for Git โ†—๏ธŽ

Name Status Preview Updated (UTC)
nx-dev โœ… Ready (Inspect) Visit Preview Aug 8, 2025 6:02pm

vercel[bot] avatar Jun 13 '25 14:06 vercel[bot]

๐Ÿณ We have a release for that!

This PR has a release associated with it. You can try it out using this command:

npx [email protected] my-workspace

Or just copy this version and use it in your own command:

0.0.0-pr-31581-fa41031
Release details ๐Ÿ“‘
Published version 0.0.0-pr-31581-fa41031
Triggered by @FrozenPandaz
Branch perf/optimize-task-graph-loading
Commit fa41031
Workflow run 15637073569

To request a new release for this pull request, mention someone from the Nx team or the @nrwl/nx-pipelines-reviewers.

github-actions[bot] avatar Jun 13 '25 15:06 github-actions[bot]

๐Ÿณ We have a release for that!

This PR has a release associated with it. You can try it out using this command:

npx [email protected] my-workspace

Or just copy this version and use it in your own command:

0.0.0-pr-31581-e373111
Release details ๐Ÿ“‘
Published version 0.0.0-pr-31581-e373111
Triggered by @nartc
Branch perf/optimize-task-graph-loading
Commit e373111
Workflow run 16807925763

To request a new release for this pull request, mention someone from the Nx team or the @nrwl/nx-pipelines-reviewers.

github-actions[bot] avatar Aug 07 '25 15:08 github-actions[bot]

Failed to publish a PR release of this pull request, triggered by @nartc. See the failed workflow run at: https://github.com/nrwl/nx/actions/runs/16810096979

github-actions[bot] avatar Aug 07 '25 16:08 github-actions[bot]

๐Ÿณ We have a release for that!

This PR has a release associated with it. You can try it out using this command:

npx [email protected] my-workspace

Or just copy this version and use it in your own command:

0.0.0-pr-31581-976a9ba
Release details ๐Ÿ“‘
Published version 0.0.0-pr-31581-976a9ba
Triggered by @nartc
Branch perf/optimize-task-graph-loading
Commit 976a9ba
Workflow run 16810096979

To request a new release for this pull request, mention someone from the Nx team or the @nrwl/nx-pipelines-reviewers.

github-actions[bot] avatar Aug 07 '25 17:08 github-actions[bot]

๐Ÿณ We have a release for that!

This PR has a release associated with it. You can try it out using this command:

npx [email protected] my-workspace

Or just copy this version and use it in your own command:

0.0.0-pr-31581-5f19a21
Release details ๐Ÿ“‘
Published version 0.0.0-pr-31581-5f19a21
Triggered by @nartc
Branch perf/optimize-task-graph-loading
Commit 5f19a21
Workflow run 16814300429

To request a new release for this pull request, mention someone from the Nx team or the @nrwl/nx-pipelines-reviewers.

github-actions[bot] avatar Aug 07 '25 20:08 github-actions[bot]

Failed to publish a PR release of this pull request, triggered by @nartc. See the failed workflow run at: https://github.com/nrwl/nx/actions/runs/16816733837

github-actions[bot] avatar Aug 07 '25 22:08 github-actions[bot]

๐Ÿณ We have a release for that!

This PR has a release associated with it. You can try it out using this command:

npx [email protected] my-workspace

Or just copy this version and use it in your own command:

0.0.0-pr-31581-b6f1582
Release details ๐Ÿ“‘
Published version 0.0.0-pr-31581-b6f1582
Triggered by @nartc
Branch perf/optimize-task-graph-loading
Commit b6f1582
Workflow run 16816733837

To request a new release for this pull request, mention someone from the Nx team or the @nrwl/nx-pipelines-reviewers.

github-actions[bot] avatar Aug 07 '25 22:08 github-actions[bot]

Failed to publish a PR release of this pull request, triggered by @nartc. See the failed workflow run at: https://github.com/nrwl/nx/actions/runs/16821027653

github-actions[bot] avatar Aug 08 '25 03:08 github-actions[bot]

๐Ÿณ We have a release for that!

This PR has a release associated with it. You can try it out using this command:

npx [email protected] my-workspace

Or just copy this version and use it in your own command:

0.0.0-pr-31581-deeb4dc
Release details ๐Ÿ“‘
Published version 0.0.0-pr-31581-deeb4dc
Triggered by @nartc
Branch perf/optimize-task-graph-loading
Commit deeb4dc
Workflow run 16821027653

To request a new release for this pull request, mention someone from the Nx team or the @nrwl/nx-pipelines-reviewers.

github-actions[bot] avatar Aug 08 '25 03:08 github-actions[bot]

Failed to publish a PR release of this pull request, triggered by @nartc. See the failed workflow run at: https://github.com/nrwl/nx/actions/runs/16822554961

github-actions[bot] avatar Aug 08 '25 06:08 github-actions[bot]

๐Ÿณ We have a release for that!

This PR has a release associated with it. You can try it out using this command:

npx [email protected] my-workspace

Or just copy this version and use it in your own command:

0.0.0-pr-31581-c075307
Release details ๐Ÿ“‘
Published version 0.0.0-pr-31581-c075307
Triggered by @nartc
Branch perf/optimize-task-graph-loading
Commit c075307
Workflow run 16822554961

To request a new release for this pull request, mention someone from the Nx team or the @nrwl/nx-pipelines-reviewers.

github-actions[bot] avatar Aug 08 '25 12:08 github-actions[bot]

๐Ÿณ We have a release for that!

This PR has a release associated with it. You can try it out using this command:

npx [email protected] my-workspace

Or just copy this version and use it in your own command:

0.0.0-pr-31581-32a9797
Release details ๐Ÿ“‘
Published version 0.0.0-pr-31581-32a9797
Triggered by @nartc
Branch perf/optimize-task-graph-loading
Commit 32a9797
Workflow run 16834585123

To request a new release for this pull request, mention someone from the Nx team or the @nrwl/nx-pipelines-reviewers.

github-actions[bot] avatar Aug 08 '25 16:08 github-actions[bot]

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

github-actions[bot] avatar Aug 14 '25 00:08 github-actions[bot]