perf(compiler): place auto memos by the routes that load them

BE
14h ago
+1023-188

The memoize plugin mirrors auto memos into modules named after the __module__ of the page function that produced them (#6218, #6457). That fits @rx.memo, but not auto memos, whose page functions rarely say much about which pages use them:

  1. Pages registered through one helper all report the helper's module, so every route imports one memo module holding the memos of all routes:

    # site.py
    def register(app: rx.App, route: str, body) -> None:
        def render() -> rx.Component:
            return body()
    
        app.add_page(render, route=route)
    
    
    site.register(app, "/", page_a)  # page_a's memos inline ~64 KB of literals
    site.register(app, "/b", page_b)
    

    /b loads all of /'s memos. Wrappers such as an i18n plugin's per-locale page function have the same effect.

  2. Pages in separate modules each get their own copy of the memos they share (a navbar used by pages_a.py and pages_b.py is compiled into both modules), so every page change downloads it again.

This PR places auto memos by the routes that load them instead. Explicit @rx.memo keeps mirroring its source module.

  • A page imports its auto memos from its own route module, .web/utils/components/routes/<page>.jsx, so a page's output never depends on the other pages.
  • After all pages compiled, compile_memo_components computes the routes that load each memo through the compiled import graph, which covers memos nested in other memos, in @rx.memo bodies and in the app root.
  • A memo one route loads is defined in that route's module. Memos loaded by the same set of routes share a module, .web/utils/components/shared/<hash>.jsx, named after the memos it holds so that adding a page doesn't rename it. Memos the app root loads go to its module, which every page loads anyway. Route modules re-export what they don't define.
  • An auto memo's symbol is its content tag, without a module suffix, so identical memos from different modules (e.g. the same chrome rendered under / and /en) merge.
  • Grouping happens in the compiler because Vite 8's output.codeSplitting.groups can't split a module; one module per memo gave the same chunks but built in 21 s instead of 13 s on the site below.

Measured on a prerendered 129-route bilingual site, JS raw / gzip:

first view // → /pricing/ → /enall JS
main1054 / 213 KB86 / 15 KB506 / 57 KB10522 KB
main, each page function given its own __module__720 / 186 KB105 / 18 KB170 / 31 KB12781 KB
this PR728 / 188 KB87 / 16 KB160 / 30 KB10509 KB

The __module__ workaround is no longer needed: with this PR the build is byte-identical with and without it. The prerendered HTML is unchanged. The first view's extra 8 KB over the workaround is react-router's route manifest, which lists the shared chunks of each route. In the helper repro above, /b drops from 549 to 483 KB raw.

For #6688's compile cache: a reused page's output stays valid when placement changes, and placement only needs each page's memo keys, which the cache already records. Placement follows the compiled import graph, so it gets tighter once #8013 drops descendant imports from passthrough memo modules.

Unnamed rx.client_state() created inside a page function draws a global counter name (cs$0, cs$3, ...), so memos that look identical on two pages differ and stay route-local. They're correct, just not deduplicated; naming them per page deterministically is a separate change.