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 #236494
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:
constvscode=acquireVsCodeApi();constoriginalState=vscode.getState();constoriginalFrameUrl=originalState?.frameUrl;console.log(\`Statelog: \${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:
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
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:
Activity
mjbvz commentedon Dec 19, 2024
Can you share some example extension code for this. I tested with our built in markdown preview and don't see this happening
vs-code-engineering commentedon Dec 27, 2024
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!
DanTup commentedon Jan 6, 2025
Matt Bierner (@mjbvz) sorry for the delay. I can reproduce this with the
webview-view-samplesample. I replaced the reference to the external script with inline 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:
DanTup commentedon Jan 22, 2025
Matt Bierner (@mjbvz) ping in case you didn't see this :-)
DanTup commentedon Jan 28, 2025
Matt Bierner (@mjbvz) ping :-)