Skip to content

Compile only the sources a dev request needs #735

Backlog

Description

@Espoir-Loem

Problem

nx dev scans src and compiles every discovered file at startup. With many routes this puts work into startup and fills the VFS with routes nobody opened. The request path is already cached, but the trade is fixed: everything is compiled before the first request.

The proposal is to compile only what a request needs, with a cache and incremental invalidation, while keeping the request path free of parsing.

Change to make

  • Measure the current eager startup and the cost of compiling one route before changing the behaviour.
  • In dev, build a route to sources graph from .nexy imports and the layout chain, reusing compile_dependencies() and Compiler.compile_with_deps().
  • On the first request to a route, compile only the missing or changed sources of that route into the VFS. Keep Python modules in sys.modules and Jinja templates in Environment.cache.
  • Keep a per-source fingerprint cache. An unchanged source is not read, parsed or generated at request time.
  • Keep a reverse dependency graph. When one source changes, invalidate only the routes that depend on it and compile the changed part on the next request to one of those routes.
  • Keep / prewarmed at startup so the first page budget stays unchanged. Other routes are compiled on first use.

Out of scope

The production build, which stays eager and keeps writing generated files to disk. Framework routes, MDX and the Vite pipeline also stay as they are.

Checks

A new budget in tests/perf/ measures a cold route and a warm route separately. Unit tests with a temporary project assert that /about compiles only its imports and layout, that a second request to the same route compiles nothing, and that editing a shared component invalidates only the routes that use it. The startup, first page, reload and request budgets do not regress. Every added branch is covered by a test.

Relation

Follow-up to #711 and #710. The VFS and the source mapping are already delivered; this issue changes when compilation is triggered.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions