WebView2 has no display when reloading inside a plugin editor "wrapper"

Hi everyone,

I want to use WebView2 inside my plugin editor, but I want to prevent the whole page from reloading when the user just closes the plugin window.
My approach is creating a normal plugin editor with a WebBrowserComponent member and another plugin editor “wrapper” that wraps the normal one:

class Webview2pluginAudioProcessorEditor  : public juce::AudioProcessorEditor
{
public:
    Webview2pluginAudioProcessorEditor (Webview2pluginAudioProcessor&);
    ~Webview2pluginAudioProcessorEditor() override;

    void paint (juce::Graphics&) override;
    void resized() override;

private:
    Webview2pluginAudioProcessor& audioProcessor;
    juce::WebBrowserComponent browser; // My WebView2 browser

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Webview2pluginAudioProcessorEditor)
};

//==============================================================================
Webview2pluginAudioProcessorEditor::Webview2pluginAudioProcessorEditor (Webview2pluginAudioProcessor& p)
    : AudioProcessorEditor (&p),
      browser(
        juce::WebBrowserComponent::Options{}
        .withBackend(juce::WebBrowserComponent::Options::Backend::webview2)
        .withWinWebView2Options(juce::WebBrowserComponent::Options::WinWebView2()
          .withStatusBarDisabled()
          .withBuiltInErrorPageDisabled()
          .withUserDataFolder(juce::File{ "C:\\my\\path\\to\\Desktop" })
          .withDLLLocation(juce::File("C:\\my\\path\\to\\WebView2Loader.dll"))
        )
      ),
      audioProcessor (p)
{
    // Make sure that before the constructor has finished, you've set the
    // editor's size to whatever you need it to be.
    setSize (800, 600);
    addAndMakeVisible(browser);
    browser.goToURL("https://positivegrid.com");
}

Webview2pluginAudioProcessorEditor::~Webview2pluginAudioProcessorEditor()
{
}
class PluginEditorWrapper : public juce::AudioProcessorEditor {
public:
    PluginEditorWrapper(Webview2pluginAudioProcessor& p) : AudioProcessorEditor(p) {
      setSize(800, 600);
    }
    ~PluginEditorWrapper() override {}

    void setInnerEditor(Webview2pluginAudioProcessorEditor* e) {
      inner = e;
      const int width = e->getWidth();
      const int height = e->getHeight();

      setSize(width, height);
      addAndMakeVisible(e);
    }

    void resized()
    {
      if (inner) {
        inner->setSize(getWidth(), getHeight());
      }
    }

private:
    // Wrap the real editor
    Webview2pluginAudioProcessorEditor *inner = nullptr;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginEditorWrapper)
};

And inside the processor’s createEditor, I return the wrapper instead:

class Webview2pluginAudioProcessor  : public juce::AudioProcessor
                            #if JucePlugin_Enable_ARA
                             , public juce::AudioProcessorARAExtension
                            #endif
{
public:

    Webview2pluginAudioProcessor();
    ~Webview2pluginAudioProcessor() override;
    // The basic audio processor API.
    
private:
    // The audio processor has the real editor
    juce::AudioProcessorEditor *inner = nullptr;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Webview2pluginAudioProcessor)
};

juce::AudioProcessorEditor* Webview2pluginAudioProcessor::createEditor()
{
  PluginEditorWrapper* newEditor;
  if (nullptr == inner) {
    inner = new Webview2pluginAudioProcessorEditor(*this);
  }

  newEditor = new PluginEditorWrapper(*this);
  newEditor->setInnerEditor((Webview2pluginAudioProcessorEditor*)inner);

  return newEditor;
}

The problem is, when I open the plugin, close the plugin window (but keep DAW opened), and open the plugin window again, the WebView2 fails to display the content.
And it output some error log:

WebView is unable to complete this operation because it is no longer valid. Controller close API is called. You shouldn't call this API after WebView is closed.

Is there any API I should reinitialize to prevent this error, or is there any way to prevent the WebBrowserComponent from destroying when user closes the plugin window?

I’m using JUCE 7.0.5.

Many thanks.