Compile only the sources a dev request needs

Open
#735 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
fastapi, python

Research direction

Start by measuring eager startup and single-route compilation, then trace compile_dependencies() and Compiler.compile_with_deps() to build the route dependency graph. Add dev-mode first-request compilation with source fingerprints and reverse dependency invalidation; keep / prewarmed. Use temporary-project tests to verify route-specific compilation, warm requests, and shared-component invalidation, and add separate cold and warm route budgets under tests/perf/.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
11
Forks
6
Avg merge
2h 14m
Merged PRs (30d)
265

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NexyPy/nexy

All issues in NexyPy/nexy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.