fix: resolve @file virtual-file URLs to absolute in HTML renderer#9440
Open
VishakBaddur wants to merge 4 commits intomarimo-team:mainfrom
Open
fix: resolve @file virtual-file URLs to absolute in HTML renderer#9440VishakBaddur wants to merge 4 commits intomarimo-team:mainfrom
VishakBaddur wants to merge 4 commits intomarimo-team:mainfrom
Conversation
When mo.image() (or mo.audio(), mo.pdf()) renders output, the backend generates a relative URL like ./@file/SIZE-filename. On molab in edit mode, the page URL is /notebooks/nb_xxx (no trailing slash), so the browser resolves ./@file/... to /notebooks/@file/... — dropping the notebook ID — causing a 404 and broken image. App mode works because the URL ends with /app, giving an extra path segment for relative resolution. Fix: add resolveVirtualFileUrl() helper that gets the runtime base URL and guarantees a trailing slash before resolving the @file path to absolute. Apply it in replaceVirtualFileSrc (img/audio/video/source) and replaceValidIframes (iframe, for mo.pdf()). Fixes marimo-team#9432
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
1 issue found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="frontend/src/plugins/core/RenderHTML.tsx">
<violation number="1" location="frontend/src/plugins/core/RenderHTML.tsx:64">
P2: `replaceVirtualFileSrc` recreates `<audio>/<video>` without children, which drops nested `<source>/<track>` and fallback content.</violation>
</file>
Architecture diagram
sequenceDiagram
participant BE as Marimo Backend
participant RHTML as RenderHTML Component
participant RM as RuntimeManager
participant Browser as Browser DOM
Note over BE, Browser: User loads notebook (e.g., /notebooks/nb_123)
BE->>RHTML: Send HTML (mo.image, mo.pdf, etc.)
Note right of BE: contains src="./@file/..."
RHTML->>RHTML: parseHtml()
loop For each img, audio, video, source, iframe
RHTML->>RHTML: NEW: Check if src contains "/@file/"
opt Match Found
RHTML->>RM: getRuntimeManager().httpURL
RM-->>RHTML: Return base URL
RHTML->>RHTML: NEW: resolveVirtualFileUrl(src)
Note right of RHTML: 1. Ensure base URL has trailing slash "/"<br/>2. Resolve relative src against base<br/>3. Convert to Absolute URL
end
end
alt CHANGED: Tag is media (img/audio/video/source)
RHTML->>RHTML: NEW: replaceVirtualFileSrc()
RHTML-->>Browser: Render element with absolute src
else CHANGED: Tag is iframe
RHTML->>RHTML: CHANGED: replaceValidIframes() (absolute src injection)
RHTML-->>Browser: Render iframe with absolute src
else Default (External URL or Data URI)
RHTML-->>Browser: Render element with original src
end
Note over Browser, BE: Network Request Resolution
alt Original Behavior (Buggy in Edit Mode)
Browser-xBE: GET /notebooks/@file/... (404: nb_123 segment lost)
else NEW: Fixed Behavior
Browser->>BE: GET /notebooks/nb_123/@file/...
BE-->>Browser: 200 OK (Asset data)
end
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
mo.image()(andmo.audio(),mo.pdf()) fails to render in edit mode on molab but works in app mode. Closes #9432.Root cause: The backend intentionally generates relative virtual file URLs like
./@file/SIZE-filename(it can't know the mount path at creation time). When the page URL has no trailing slash — e.g., molab edit mode at/notebooks/nb_xxx— the browser resolves./@file/...to/notebooks/@file/..., dropping the notebook ID and causing a 404.App mode works because the URL ends with
/app, providing an extra path segment that makes relative resolution correct.Verified empirically:
Fix
Add
resolveVirtualFileUrl()helper inRenderHTML.tsxthat resolves@file/URLs against the runtime base URL with a guaranteed trailing slash, making the URL absolute and unambiguous regardless of the page URL's trailing slash.Apply it in:
replaceVirtualFileSrc— handles<img>,<audio>,<video>,<source>(coversmo.image(),mo.audio())replaceValidIframes— handles<iframe>(coversmo.pdf())Data URLs and external
https://URLs are untouched.Testing
./@file/rewriting,@file/without./, non-@fileURLs (unchanged),data:URLs (unchanged)