Skip to content

Lazy-load the ELK layout engine in the flow builder - #4150

Closed
DonOmalVindula wants to merge 1 commit into
thunder-id:mainfrom
DonOmalVindula:perf-flow-builder-chunk
Closed

Lazy-load the ELK layout engine in the flow builder#4150
DonOmalVindula wants to merge 1 commit into
thunder-id:mainfrom
DonOmalVindula:perf-flow-builder-chunk

Conversation

@DonOmalVindula

@DonOmalVindula DonOmalVindula commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Purpose

Speed up opening the flow builder. Clicking a flow in the flows list showed a noticeable delay before the canvas appeared.

The builder page is code-split, so opening it downloads and parses a single large chunk before anything but the loading spinner renders. applyAutoLayout statically imported elkjs (the ELK layout engine, ~1.5MB) at module top level, so the entire engine was bundled into the builder's entry chunk and parsed on every open, even though auto-layout only runs on demand (the toolbar action, or opening a flow whose nodes have no stored positions). Saved flows carry node positions, so the common editing path never uses ELK at all.

Approach

Dynamically import ELK on first layout and cache the instance, instead of importing it eagerly at module load. The static import becomes type-only. No behavior change: applyAutoLayout is already async, so the layout call simply awaits the engine before running.

Result (production build of the console app):

Before After
Builder entry chunk (raw) 2,399 KB 985 KB
Builder entry chunk (gzipped) 727 KB 308 KB
ELK in entry chunk separate on-demand chunk

ELK now downloads only when a layout is actually requested.

Related Issues

Related PRs

  • N/A

Checklist

  • Followed the contribution guidelines.
  • Manual test round performed and verified.
  • Documentation provided. (Add links if there are any)
    • Ran Vale and fixed all errors and warnings
  • Tests provided. (Add links if there are any)
    • Unit Tests (existing applyAutoLayout suite, 25 tests, still passing)
    • Integration Tests
  • Breaking changes. (Fill if applicable)
    • Breaking changes section filled.
    • breaking change label added.

Security checks

  • Followed secure coding standards in WSO2 Secure Coding Guidelines
  • Confirmed that this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets.

Summary by CodeRabbit

  • Performance
    • Improved flow editor loading by loading auto-layout functionality only when needed.
    • Preserved existing layout behavior and fallback handling when layout operations fail.

The flow builder statically imported elkjs (~1.5MB) at module load through
applyAutoLayout, so it was bundled into the builder's entry chunk and parsed
on every flow open, even though auto-layout only runs on demand (toolbar
action, or opening a flow with no stored node positions). Saved flows carry
positions, so the common path never uses it.

Dynamically import ELK on first layout instead. The builder entry chunk drops
from ~2.4MB to ~1.0MB (727KB to 308KB gzipped), and ELK loads only when a
layout is actually requested.

Refs thunder-id#3871
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

ELK loading in the auto-layout utility now occurs dynamically and is cached for subsequent layout requests. Layout computation and existing failure fallback behavior remain unchanged.

Changes

Auto-layout initialization

Layer / File(s) Summary
Lazy ELK initialization
frontend/apps/console/src/features/flows/utils/applyAutoLayout.ts
ELK is imported as a type, dynamically instantiated through a cached getElk() helper, and awaited when layout execution begins.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: lazy-loading ELK in the flow builder.
Description check ✅ Passed The description follows the template well, covering Purpose, Approach, Related Issues, Checklist, and Security checks.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@frontend/apps/console/src/features/flows/utils/applyAutoLayout.ts`:
- Around line 58-61: Update getElk so a rejected dynamic-import promise clears
the cached elkPromise before propagating the error, allowing subsequent
auto-layout attempts to retry the import while preserving successful promise
caching.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 0ae82e97-7b59-4385-95cc-9c3b995974f4

📥 Commits

Reviewing files that changed from the base of the PR and between cc16eaa and 1c7bae9.

📒 Files selected for processing (1)
  • frontend/apps/console/src/features/flows/utils/applyAutoLayout.ts

Comment thread frontend/apps/console/src/features/flows/utils/applyAutoLayout.ts
@codecov

codecov Bot commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@DonOmalVindula

Copy link
Copy Markdown
Contributor Author

Folded into #4153 (same file, combined as one change) together with the CodeRabbit retry fix for the cached dynamic import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant