fix(assets): link shared assets in subdirectories, cache content hashes and accept versioned stylesheets

BE
1d ago
+295-14

Four small gaps in rx.asset that we had worked around in an app-level wrapper:

  • Shared assets in a subdirectory: rx.asset("icons/logo.svg", shared=True) failed, because only the module's folder below assets/external/ was created, not the directories of the path itself.
  • Frame lookup: a shared asset found its calling module with inspect.stack(), which builds every frame on the stack and reads its source context on every call. sys._getframe(_stack_level) and the frame's __name__ give the same file and module.
  • Hash caching: every call read the file twice for its hash, with nothing cached. The hash is now cached per file under its stat signature (device, inode, size, mtime, ctime) and the double read only runs when that changed. A file changed within the last 2 s is hashed but not cached, since a second write in the same timestamp tick could leave the stat unchanged.
  • Versioned stylesheets: since #6749, an rx.asset url passed to rx.App(stylesheets=...) carries ?v=<hash>, and the stylesheet lookup failed with FileNotFoundError. The query is now dropped there; the bundler versions the compiled stylesheet anyway.

With a settled 400 KB asset, a repeated local rx.asset call goes from about 330 to 9 µs, and a shared one at a stack depth of about 70 from about 990 to 210 µs.

This touches the same lines in asset() and _compile_root_stylesheet as #8009 and #8007; whichever merges second resolves a small conflict.