Skip to content

WebView state unexpectedly persists across sessions without a WebviewPanelSerializer #240207

Open

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. This can be reproduced by opening the webview-view-sample project and making the following change:

// ORIGINAL CODE
				<script nonce="${nonce}" src="${scriptUri}"></script>
// NEW CODE
				<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>

Loaded the webview, run "Reload Webviews" a few times, then shut down VS Code. Then re-opened VS Code and open the webview and you'll see the following:

Image

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.97.0 (user setup)
Commit: 33fc5a9
Date: 2025-02-04T22:41:26.688Z
Electron: 32.2.7
ElectronBuildId: 10660205
Chromium: 128.0.6613.186
Node.js: 20.18.1
V8: 12.8.374.38-electron.0
OS: Windows_NT x64 10.0.26100

I previously raised this at #236494 but it was closed asking for more info, however I added more info over a month ago but the issue remains closed and it's not clear how else this issue can be progressed. If it's better to reopen that issue and close this one, that's fine by me. Thanks!

Activity

  1. DanTup commented on Feb 17, 2025

    @DanTup
    ContributorAuthor

    I pushed the repro here: https://github.com/DanTup/vscode-extension-samples/tree/repro-webview-state

    Specifically, here is the change to just print the state:

    DanTup/vscode-extension-samples@536a397

    Just:

    • run the extension
    • expand the Colors sidebar panel
    • reload the window a few times
    • completely close all VS Codes
    • reopen VS Code
    • re-run the extension
    • open the dev tools console
    • notice that the log contains many timestamps from before the restart

    Image

  2. DanTup commented on Feb 17, 2025

    @DanTup
    ContributorAuthor

    Matt Bierner (@mjbvz) I started looking through the code to see if I could understand where it's going wrong, and I found this change that I wonder if is related?

    be6d3c4

  3. DanTup commented on Feb 25, 2025

    @DanTup
    ContributorAuthor

    As a workaround, putting all state into a randomly named key (with the random key being a constant created in the extension's activate method) seems to work.

  4. added
    bugIssue identified by VS Code Team member as probable bug
    webviewWebview issues
    on Dec 9, 2025
  5. added this to the Backlog milestone on Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugIssue identified by VS Code Team member as probable bugwebviewWebview issues

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions