Skip to content

Refresh markdown preview html before a hidden panel is rebuilt - #334681

Merged
Henning Dieterichs (hediet) merged 6 commits into
microsoft:mainfrom
bompus:markdown-preview-stale-html-on-reveal
Oct 31, 2026
Merged

Henning Dieterichs (hediet) merged 6 commits into
microsoft:mainfrom
bompus:markdown-preview-stale-html-on-reveal

Conversation

@bompus

@bompus Aaron Queen (bompus) commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

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 the vscode.markdown.preview.editor custom editor, with edits made in VS Code as well as with files overwritten on disk.

Root cause

MarkdownPreview applies updates made while the panel is visible by posting an updateContent message to the webview, and leaves webview.html untouched. Neither preview uses retainContextWhenHidden, so VS Code discards the webview when the panel is hidden and rebuilds it from webview.html when the panel is revealed. That html is whatever the last full render produced, so every update applied through updateContent since then is lost on reveal. Nothing in the preview reacts to the panel being hidden, and the version check in #updatePreview keeps a later refresh from re-rendering unchanged content.

Fix

  • Track whether webview.html is behind what the webview shows (#webviewHtmlOutOfDate).
  • When the panel is hidden while the html is behind, request a forced refresh, which goes through the existing full render path and brings webview.html up 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.
  • If the panel is hidden while an in-place update is still rendering, the update requests the forced refresh itself: the hide event found the html up to date and scheduled nothing, and the webview the message was meant for is already gone.
  • 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-body text 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.

Scenario Step main (42b37b7) with this change
Dynamic preview, file overwritten on disk while visible, tab switch 1 preview opened D1-A D1-A
2 after external write D1-B D1-B
3 preview hidden behind another tab hidden hidden
4 preview tab shown again D1-A (stale) D1-B
Dynamic preview, edit + save in a text editor, tab switch 1 preview opened D1-A D1-A
2 after edit and save D1-C D1-C
3 preview hidden behind another tab hidden hidden
4 preview tab shown again D1-A (stale) D1-C
Dynamic preview, file overwritten while hidden (control) 1 preview opened D1-A D1-A
2 preview tab shown after the hidden write D1-D D1-D
vscode.markdown.preview.editor, file overwritten on disk while visible, tab switch 1 editor opened D1-A D1-A
2 after external write D1-E D1-E
3 editor hidden behind another tab hidden hidden
4 editor tab shown again D1-A (stale) D1-E

The new tests in src/test/preview.test.ts revive a DynamicMarkdownPreview on a real webview panel. The first applies an edit while the panel is visible, hides the panel behind a text editor and asserts that webview.html catches 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 #updateWebviewContent is 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

Copilot AI balanced review requested due to automatic review settings September 5, 2026 01:52
@bompus

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 apply solely to restore the method receiver, contrary to the project guideline to call through the object or use a closure. Since renderBody was restored immediately above, invoke it through renderer directly.
		return original.apply(renderer, args);
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The fix and regression coverage were fully reviewed with no unresolved issues.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The shared preview lifecycle fix is coherent and covers the identified races with focused regression tests.

Review effort: Balanced
Findings: None

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The lifecycle fix is focused and its key race conditions have deterministic regression coverage.

Review effort: Balanced
Findings: None

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The lifecycle fix is focused and its key race conditions have regression coverage.

Review effort: Balanced
Findings: None

@hediet
Henning Dieterichs (hediet) merged commit 4e8c102 into microsoft:main Oct 31, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown preview show cached file when switching to another tab and then switching back

5 participants