HTML viewer reloads its document every time its tab is shown
Description
The HTML viewer (@jupyterlab/htmlviewer, the "HTML Viewer" widget factory) reloads the file into its iframe every time its tab is brought back into view, even though the file has not changed. The page goes back to the top and anything the page itself was holding, such as an open <details> element or state set by the page's own scripts, is lost.
This makes the viewer hard to use for comparing two HTML files, which means switching back and forth between two tabs: every switch resets both pages.
I ran into this while developing a JupyterLab extension, but the extension is not involved. It reproduces in a clean install with only jupyterlab installed and no third-party extensions, by opening files through the file browser.
Reproduce
Put two HTML files in the working directory. They should be taller than the window, and something that holds state makes the effect obvious, for example:
<!doctype html> <html><body> <h1>one</h1> <details><summary>Open me</summary>If this is still open after a tab switch, no reload happened.</details> <p>line 1</p> ... a few hundred more paragraphs ... </body></html>Right-click each file in the file browser, Open With, HTML Viewer, so both are tabs in the same area. Trusting the HTML is not needed; it happens either way.
In the first tab, scroll a long way down and open the
<details>element.Click the second tab, then click the first tab again.
The first page has been loaded again from scratch: it is back at the top and the
<details>element is closed.
I have not diagnosed this myself beyond reproducing it as above. I used an AI assistant to investigate, and it ran a scripted version of the same steps in headless Chromium against JupyterLab 4.6.3. What it reported from that:
After the tab switch the viewer's iframe had a new
blob:URL and had firedloadonce more, and a timestamp written by the page's own script had changed, so it was a fresh page load, not a scroll reset.A plain iframe on the same file in another tab (opened through
help:openat the file's/files/URL) kept its URL and did not fireloadacross the same switch.With a message log attached to the viewer widget, the switch delivered
before-show,after-show,update-request, and the reload happened on theupdate-request, with no content change involved.
Its explanation of the cause, at a high level, was:
The HTML viewer does its rendering in its
onUpdateRequesthandler, and rendering means building a newBlobfrom the document text, creating a new object URL for it and pointing the iframe at that URL, which is a full page load. The viewer requests an update in the places where a render is genuinely needed (the file finishing loading, the content changing, the trust setting changing, the reload button).update-requestis a general Lumino message, though, and other code sends it too. Every Lumino layout, including theBoxLayoutinsideMainAreaWidget, callsparent.update()from itsonBeforeShowso that it can re-measure after being hidden. So showing a tab posts the viewer an update request, and the viewer treats that layout message as a reason to render the document again.Other iframes in the dock panel are not affected. A hidden tab is
display: none, not detached, so a plainIFramewidget on the same file keeps its state across the same switch. The reload is the viewer's own doing.The same design was in the PDF viewer and reported as #5714 ("PDF scrolls to top everytime when you click the tab"); that was fixed by not re-rendering when nothing had changed. The HTML viewer never got an equivalent change.
I am passing this on as the assistant's analysis rather than my own; it is consistent with what I see by hand, but I have not verified the explanation against the source. The assistant also proposed a fix. I am not in a position to judge whether it is the right one, so I have not included it here, but I am happy to share it if that would be useful.
Expected behavior
The first page is as it was left: scrolled to the same place, with the <details> element open. Other document widgets behave this way, for example a notebook keeps its scroll position and the PDF viewer keeps its page when their tabs are hidden and shown. The viewer's toolbar already has a reload button for when a fresh load is wanted.
Context
- Operating System and version: macOS
- Browser and version: ; the assistant's scripted check used headless Chromium through Playwright
- JupyterLab version: 4.6.3 and 4.6.4, with jupyter_server 2.21.0
Nothing is printed to the console when it happens.
Source: jupyterlab/jupyterlab