You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
WebView state unexpectedly persists across sessions without a WebviewPanelSerializer #240207
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<scriptnonce="${nonce}"src="${scriptUri}"></script>// NEW CODE<scriptnonce="${nonce}">constvscode=acquireVsCodeApi();constoriginalState=vscode.getState()??{};console.log(\`Statelog: \${originalState.log?.join("\\n")}\`);vscode.setState({
...(originalState??{}),log: [...(originalState.log??[]), \`Webview state added at ${newDate()}\`]});</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:
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
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!
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?
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.
Activity
Repro for microsoft/vscode#240207
DanTup commentedon Feb 17, 2025
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:
DanTup commentedon Feb 17, 2025
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
DanTup commentedon Feb 25, 2025
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.