Skip to content

WebView state unexpectedly persists across sessions without a WebviewPanelSerializer #236494

Closed as not planned

Description

@DanTup

In the docs at https://code.visualstudio.com/api/references/vscode-api#WebviewPanelSerializer<T>, it says:

There are two types of webview persistence:

  • Persistence within a session.
  • Persistence across sessions (across restarts of the editor).

A WebviewPanelSerializer is only required for the second case: persisting a webview across sessions.

The suggestion is that without a WebviewPanelSerializer, state is not persisted across sessions.

However, that's not what I'm seeing. I don't have a WebviewPanelSerializer but my state is persisting across restarts. I added the following to the top of my webview script:

const vscode = acquireVsCodeApi();
const originalState = vscode.getState();
const originalFrameUrl = originalState?.frameUrl;

console.log(\`State log: \${originalState.log?.join("\\n")}\`);
vscode.setState({
	...(originalState ?? {}),
	log: [...(originalState.log ?? []), \`Webview state added at \${new Date()}\`]
});

Then I loaded the webview, ran "Reload Webviews" a few times, then shut down VS Code. Then when I re-opened VS Code and opened my webview, I saw the following:

Image

This is not what I'd expect - I would've expected any state from previous launches of VS Code to be gone.

As an aside, things that aren't clear to me from the limited docs:

  • is the intention that state is lost in VS Code restart, or extension host restart? What about "Reload Window"?
  • is state shared across all webviews, or scoped by an id?
  • does setState always replace the entire object, or is there a way to add just new fields (I ask because if the state is shared amongst all webviews, that could result in them overwriting each others state)

Does this issue occur when all extensions are disabled?: Yes

Version: 1.96.0 (user setup)
Commit: 138f619
Date: 2024-12-11T02:29:09.626Z
Electron: 32.2.6
ElectronBuildId: 10629634
Chromium: 128.0.6613.186
Node.js: 20.18.1
V8: 12.8.374.38-electron.0
OS: Windows_NT x64 10.0.26100

Activity

  1. mjbvz commented on Dec 19, 2024

    @mjbvz
    Contributor

    Can you share some example extension code for this. I tested with our built in markdown preview and don't see this happening

  2. vs-code-engineering commented on Dec 27, 2024

    @vs-code-engineering
    Contributor

    This issue has been closed automatically because it needs more information and has not had recent activity. See also our issue reporting guidelines.

    Happy Coding!

  3. DanTup commented on Jan 6, 2025

    @DanTup
    ContributorAuthor

    Matt Bierner (@mjbvz) sorry for the delay. I can reproduce this with the webview-view-sample sample. I replaced the reference to the external script with inline script:

    // Old
    				<script nonce="${nonce}" src="${scriptUri}"></script>
    // New
    				<script nonce="${nonce}">
    				const vscode = acquireVsCodeApi();
    				const originalState = vscode.getState() ?? {};
    
    				console.log(\`State log: \${originalState.log?.join("\\n")}\`);
    				vscode.setState({
    					...(originalState ?? {}),
    					log: [...(originalState.log ?? []), \`Webview state added at ${new Date()}\`]
    				});
    				</script>
    

    Then I run the extension and run Reload Window a few times. Then close the extension dev host, close VS Code. Reopen VS Code, and re-run and you see the issue:

    Image

  4. DanTup commented on Jan 22, 2025

    @DanTup
    ContributorAuthor

    Matt Bierner (@mjbvz) ping in case you didn't see this :-)

  5. DanTup commented on Jan 28, 2025

    @DanTup
    ContributorAuthor
  6. locked and limited conversation to collaborators on Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

info-neededIssue requires more information from posterwebviewWebview issues

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions