Skip to content

Force update on Markdown preview webview visible - #150150

Closed
Song Xie (aifreedom) wants to merge 2 commits into
microsoft:mainfrom
aifreedom:markdown-preview
Closed

Song Xie (aifreedom) wants to merge 2 commits into
microsoft:mainfrom
aifreedom:markdown-preview

Conversation

@aifreedom

Copy link
Copy Markdown
Contributor

This PR fixes #147718

The cause of the issue seems to be that when the preview webview becomes visible from invisible, the content is initialized using the webview's html. But when the webview's content is updated, it's updated using JavaScript in the webview, which doesn't update the html property.

The fix forces the webview to refresh when the webview becomes visible. Honestly, it's not the most elegant fix. I'm open to suggestions on how to fix the root cause better.

Test

test.md.-.aifreedom.-.Code.-.OSS.Dev.2022-05-22.21-24-24.mp4


this._register(this._webviewPanel.onDidChangeViewState(event => {
if (event.webviewPanel.visible) {
this.refresh(true);

@mjbvz Matt Bierner (mjbvz) May 23, 2022 •

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.

This fixes the problem but two issues:

  1. This changes means the preview gets updated every time it is revealed, even if the content has not changed (and the current change also incorrectly refreshes the preview when active state changes)

    Instead we should only update the preview if preview has become out of sync with the document content

  2. Updating the content on reveal will cause a flash where we see the old content before the new content is loaded.

    Not sure of the best approach to fix this but open to ideas

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Matt Bierner (@mjbvz) Thanks for the comments. I also agree that the fix is far from perfect.

I think I'll need to understand the webview better to find a better approach.

My understanding of the issue is that when the Markdown file is edited, the preview webview's content is updated by javascript. However, the HTML source is not updated in this process.

this._webviewPanel.webview.postMessage({
type: 'updateContent',
content: html,
source: this._resource.toString(),
});

But when the webview panel disappears and reappears, the changes made by JavaScript are not kept. It's like after a webpage's content gets modified by javascript, a refresh in the browser will bring back the original state.

One approach I have in mind is to have a flag to indicate that what's currently shown in the webview is different from the HTML. And the next time when it's loaded, it knows a hard refresh is needed. Or, if we do the hard refresh when the webview disappears and needs to be refreshed, the updated HTML can avoid the flash on reveal.

@aifreedom

Song Xie (aifreedom) commented May 23, 2022 •

Copy link
Copy Markdown
Contributor Author

Use retainContextWhenHidden to persist state when the webview disappears and reappears. It fixes the flash and avoids unnecessary refreshes. But it does need more memory according to the doc.

Preview.test.md.-.aifreedom.-.Code.-.OSS.Dev.2022-05-23.16-46-30.mp4

@mjbvz

Copy link
Copy Markdown
Contributor

I don't think we should add retainContextWhenHidden. There's a performance and memory costs to keeping the preview around like this which we want to avoid for markdown previews

@aifreedom

Copy link
Copy Markdown
Contributor Author

The root cause is the webview's states don't get persisted when the webview is hidden. Because the preview webview directly receives the rendered HTML and shows it, the state of it would be the rendered HTML itself. Sure, you can implement WebviewPanelSerializer and serialize and deserialize the body of the webview as the state. I doubt how much more memory can be saved with that approach.

@justanotheranonymoususer

Copy link
Copy Markdown
Contributor

You didn't discuss the scenario of hiding the preview tab, then modifying the md file, then showing the tab again. Serialization won't help with this case since it will become out of date while the preview tab is hidden.

@aifreedom
Song Xie (aifreedom) deleted the markdown-preview branch November 22, 2023 05:38
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown preview resets when switching tabs

3 participants