Repository navigation
Refresh markdown preview html before a hidden panel is rebuilt - #334681
Henning Dieterichs (hediet) merged 6 commits into
Conversation
|
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
🟡 Changes recommended
The asynchronous hide race can still leave stored preview HTML stale, and test cleanup must be corrected.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Refreshes stale Markdown preview HTML before hidden webviews are rebuilt.
Changes:
- Tracks outdated stored webview HTML.
- Preserves forced refreshes during throttling.
- Adds regression coverage and exports a shared test fixture.
File summaries
| File | Summary | Findings |
|---|---|---|
extensions/markdown-language-features/src/test/preview.test.ts |
Tests preview refresh after hiding. | Moderate (1 vote): Save the edited document for cleanup. Nit (2 votes): Dispose the TopmostLineMonitor. |
extensions/markdown-language-features/src/test/engine.ts |
Exports the empty contribution provider. | No issues. |
extensions/markdown-language-features/src/preview/preview.ts |
Adds stale-HTML tracking and forced refresh scheduling. | Moderate (2 votes): Handle panels hidden during asynchronous rendering. Nit (2 votes): Add coverage for forced refresh coalescing. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 4
- Review effort level: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
🟡 Changes recommended
The render-while-hiding race branch lacks deterministic regression coverage.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Balanced
There was a problem hiding this comment.
🟢 Approval recommended
The fix is well covered, with only a non-blocking code-style nit.
Review details
Suppressed comments (1)
extensions/markdown-language-features/src/test/preview.test.ts:58
- This uses
applysolely to restore the method receiver, contrary to the project guideline to call through the object or use a closure. SincerenderBodywas restored immediately above, invoke it throughrendererdirectly.
return original.apply(renderer, args);
- Files reviewed: 3/3 changed files
- Comments generated: 0 new
- Review effort level: Balanced
Updates made while the preview is visible are applied by posting an updateContent message and leave webview.html untouched. VS Code discards a hidden panel's webview and rebuilds it from webview.html on the next reveal, so the preview came back showing the content from before the update. Request a full refresh when the panel is hidden while its html is behind, and keep a forced refresh forced when one is already pending. Part of microsoft#265277
A hide that arrives while the body is still rendering finds the html up to date and schedules nothing, and the update message then goes to a webview that is already gone. Request the forced refresh from the update itself in that case. The test saves the document so it does not stay dirty, disposes the line monitor, and covers the forced refresh surviving a pending regular one.
Holds the in-place render of a change, hides the panel while it is in flight, releases it and asserts that webview.html still catches up. The test fails without the branch in #updateWebviewContent that requests the forced refresh from the finished update.
a1390d0 to
adc8f28
Compare
Fixes #271233
Part of #265277
Several reports in those issues describe the same sequence: the preview updates correctly while it is visible, the user switches to another tab and back, and the preview shows the content from before the update. This applies to the dynamic preview (
markdown.showPreview) and to thevscode.markdown.preview.editorcustom editor, with edits made in VS Code as well as with files overwritten on disk.Root cause
MarkdownPreviewapplies updates made while the panel is visible by posting anupdateContentmessage to the webview, and leaveswebview.htmluntouched. Neither preview usesretainContextWhenHidden, so VS Code discards the webview when the panel is hidden and rebuilds it fromwebview.htmlwhen the panel is revealed. That html is whatever the last full render produced, so every update applied throughupdateContentsince then is lost on reveal. Nothing in the preview reacts to the panel being hidden, and the version check in#updatePreviewkeeps a later refresh from re-rendering unchanged content.Fix
webview.htmlis behind what the webview shows (#webviewHtmlOutOfDate).webview.htmlup to date before the panel can be revealed. Panels that never received an in-place update, and panels updated while hidden (which already re-render fully), are unaffected.refresh(true)now keeps its force flag when an update is already scheduled. Before, a forced refresh requested while the 300 ms throttle timer was pending was silently downgraded to a regular update.Verification
Measured by driving the extension from source in Insiders 1.137.0-insider (de8cc55) with
--extensionDevelopmentPath, reading the rendered.markdown-bodytext of the preview iframe over the remote debugging port after each step. "hidden" reads as no preview iframe at all, which is the webview being discarded.vscode.markdown.preview.editor, file overwritten on disk while visible, tab switchThe new tests in
src/test/preview.test.tsrevive aDynamicMarkdownPreviewon a real webview panel. The first applies an edit while the panel is visible, hides the panel behind a text editor and asserts thatwebview.htmlcatches up; it fails on main at that last assertion and passes with this change. The second holds the in-place render of an edit, hides the panel while it is in flight, releases the render and asserts the same; it fails when the branch in#updateWebviewContentis removed. The third makes a further edit so a regular refresh is pending, hides and re-reveals the panel, and asserts the reveal still gets a full reload. The full markdown-language-features suite passes (99 tests, 1 skipped).Related issues
Relation to the other open PRs
refresh()when the panel becomes visible, claims to fix Markdown preview show cached file when switching to another tab and then switching back #271233) calls the non-forcedrefresh()of the base class. The document version has not changed since the in-place update, so#updatePreviewreturns at the version check andwebview.htmlstays stale. Measured with that diff applied on top of 1.128.0: step 4 of the first scenario above still shows D1-A.refresh()when the dynamic preview becomes inactive, 2022) goes throughDynamicMarkdownPreview.refresh(), which is forced, so it does fix the dynamic preview (steps 4 above render D1-B and D1-C with it). It reloads the whole page every time the preview loses focus, whether or not anything changed, which is the concern raised in its review, and it does not covervscode.markdown.preview.editor(step 4 of the last scenario stays D1-A). This change reloads only when an in-place update has actually happened and covers both previews.PreviewDocumentVersion.equals) and Fix stale markdown preview after reopening document #332081 (token cache identity) address refreshes being skipped while the preview is visible; no overlap. Markdown preview: refresh on file-system change even when document is open #316756 removes the watcher guard for documents VS Code already tracks; it does not change what a hidden panel is rebuilt from, so the tab-switch case above stays stale with it.