# \[BUG?\] Juce 8 WebView and pageFinishedLoading

**URL:** <https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472>\
**Category:** General JUCE discussion\
**Created:** [May 20, 2024, 7:43pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472 "2024-05-20T19:43:41Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![MeijisIrlnd](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@MeijisIrlnd](https://forum.juce.com/u/MeijisIrlnd)\
**Post date:** [May 20, 2024, 7:43pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/1 "2024-05-20T19:43:41Z")

</div>

Heya - I noticed the new webui stuff makes the bounds of its rect transparent for a split second before actually showing the webpage, and figured it was just a quirk of web browsers in general, so set about having some sort of loading screen beneath the webview, setting it invisible, and then in response to `pageFinishedLoading`, invoking a callback to set it visible - so something like:

```cpp
// SinglePageBrowser.h
class SinglePageBrowser final : public juce::WebBrowserComponent { 
public:
    // Page about to load etc..
    void pageFinishedLoading(const juce::String&) override { 
        if(!pageLoadedCallback) return;
        pageLoadedCallback();
    }
    std::function<void(void)> pageLoadedCallback{ nullptr };
};

// Editor
PluginEditor::PluginEditor(/* etc etc etc */) { 
    m_browser.setVisible(false);
    addChildComponent(&m_browser);
    auto pageLoadedCallback = [this]() -> void { 
        jassert(!m_browser.isVisible());
        m_browser.setVisible(true);
    };
    m_browser.pageLoadedCallback = std::move(pageLoadedCallback);
    m_browser.goToUrl(/*etc*/);
    // set size etc etc etc etc
}

void PluginEditor::paint(juce::Graphics& g) { 
    g.fillAll(juce::Colour{ 0xFF000000 });
}

// and then obviously set the browser's bounds etc etc 

```

And the same thing happens: just before my `pageLoadedCallback` gets called, the area the WebUI takes up is made transparent (and _then_ my breakpoint is hit within my callback) - the assertion passes too, so it seems to not even be directly caused by the page loading.  
Any ideas on what’s going on here? I have admittedly only tried this on Windows, and have only rigourously tested its behaviour in Standalone, maybe it’s different in a VST3 context somehow?

---

<div class="post-metadata">

**Author:** ![ibisum](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ibisum/32/21288_2.png) [@ibisum](https://forum.juce.com/u/ibisum)\
**Post date:** [May 21, 2024, 10:38am UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/2 "2024-05-21T10:38:08Z")

</div>

Isn’t this happening because all Components start off transparent with unknown bounding-box sizes?

---

<div class="post-metadata">

**Author:** ![MeijisIrlnd](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@MeijisIrlnd](https://forum.juce.com/u/MeijisIrlnd)\
**Post date:** [May 21, 2024, 1:56pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/3 "2024-05-21T13:56:38Z")

</div>

The browser component isn’t set visible until the callback in my snippet - and it passes the assertion, so it seems to be unrelated to the component’s visibility - so don’t think so, it seems to be more like the clip rect _for_ the browser component gets turned transparent for a second regardless of its visibility.

---

<div class="post-metadata">

**Author:** ![mthierman](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mthierman](https://forum.juce.com/u/mthierman)\
**Post date:** [May 28, 2024, 3:55pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/4 "2024-05-28T15:55:57Z")

</div>

I’ve tried some hacks but couldn’t figure out a solution (it doesn’t help that it only happens sometimes):

- Made some changes to window procedure w/ WM\_PAINT and WM\_ERASEBKGND and set a background brush
- Set the webview2 background color through an env var instead of using the webview settings (there is a known issue with this, see here: [WebView2 Win32 C++ ICoreWebView2Controller2 | Microsoft Learn](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2controller2?view=webview2-1.0.2478.35#get_defaultbackgroundcolor))

I have the same issue using CHOC’s webview with a HWNDComponent.

---

<div class="post-metadata">

**Author:** ![MeijisIrlnd](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@MeijisIrlnd](https://forum.juce.com/u/MeijisIrlnd)\
**Post date:** [July 11, 2024, 2:35pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/5 "2024-07-11T14:35:17Z")

</div>

Resurrecting this - was anyone on the JUCE team able to recreate this by any chance? Lmk if further info is needed!

---

<div class="post-metadata">

**Author:** ![attila](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/attila/32/11206_2.png) [@attila](https://forum.juce.com/u/attila)\
**Post date:** [July 11, 2024, 5:02pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/6 "2024-07-11T17:02:34Z")

</div>

A fix has been released on `develop` addressing the flicker to the JUCE default window background colour

> <https://github.com/juce-framework/JUCE/commit/7ead20d575d9bc943ac94b301b0c1b23d16460dc>
>
> The default background colour of the WebBrowserComponent is white on all
> platfor…ms. Before the first page finishes loading this is the colour
> that should fill the WebBrowserComponent's area.
> 
> Prior to this change however, on Windows a sudden flash to the default
> JUCE background colour would occur, before the default white background
> could take effect.
> 
> At the time of this commit there is a known issue with the
> icorewebview2controller2, where a white flash is inevitable, unless the
> WEBVIEW2\_DEFAULT\_BACKGROUND\_COLOR environment variable is set. Using
> a white background behind the WebView avoids this issue.

---

<div class="post-metadata">

**Author:** ![MeijisIrlnd](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@MeijisIrlnd](https://forum.juce.com/u/MeijisIrlnd)\
**Post date:** [July 11, 2024, 6:00pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/7 "2024-07-11T18:00:27Z")

</div>

Awesome just tried it, seems a bit better but I’m still getting weird behaviour on startup, I can’t attach a video but some screenshots: (this is loading the files from Binary Data as opposed to from localhost if that makes a difference)  
So it flickers white when it opens:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/d/9/d96bd2b79046481ccf94f776d1ef13e2ec4345a3.png)  
then the window goes transparent:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/c/9/c969ca18decda04feea5fc86c49969b5a0627ec0.jpeg)  
and then it sort of “half-renders” (this is probably my end, the background is an svg so I guess it’s taking a split second to load it)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/6/3/63671879c33f76a2b4a79b9389ea431698109428.png)  
and _then_ it fully loads  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/5/7/570bd26be582f90affb94e1499da30539f80f863.png)

---

<div class="post-metadata">

**Author:** ![attila](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/attila/32/11206_2.png) [@attila](https://forum.juce.com/u/attila)\
**Post date:** [July 12, 2024, 9:19am UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/8 "2024-07-12T09:19:53Z")

</div>

The default background colour prior to loading the content is white, this is now consistent across all platforms.

We could probably make this configurable, but right now on Windows a white flash may be inevitable unless the `WEBVIEW2_DEFAULT_BACKGROUND_COLOR` environment variable is set.

I’m not sure what the flash to transparency is caused by, this isn’t something I observed. Could you share maybe a simple example that produces this?

As a workaround I’d try to delay showing the WebBrowserComponent. Initially by a Timer, to see if it’s even possible to wait long enough, and then by using maybe adding a NativeFunction that toggles visibility, and then a Javascript onload script could control this.

---

<div class="post-metadata">

**Author:** ![attila](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/attila/32/11206_2.png) [@attila](https://forum.juce.com/u/attila)\
**Post date:** [July 12, 2024, 2:11pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/9 "2024-07-12T14:11:19Z")

</div>

Correction, I am seeing a transparent flash with the WebBrowserComponent in a plugin window. Just for our own record, this is not a regression caused by the latest change.

We’ll investigate. Until then, I can only suggest trying to explicitly toggle visibility.

---

<div class="post-metadata">

**Author:** ![MeijisIrlnd](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@MeijisIrlnd](https://forum.juce.com/u/MeijisIrlnd)\
**Post date:** [July 12, 2024, 5:09pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/10 "2024-07-12T17:09:18Z")

</div>

Ah awesome, reassuring that you were able to recreate it (makes me slightly less worried about my javascript).  
Will give the timer stuff a go, thanks for the tip, and will post if I get a nice workaround set up, thanks for looking into this!

---

<div class="post-metadata">

**Author:** ![MeijisIrlnd](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@MeijisIrlnd](https://forum.juce.com/u/MeijisIrlnd)\
**Post date:** [July 12, 2024, 5:36pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/11 "2024-07-12T17:36:09Z")

</div>

So actually another interesting find - on Windows, `setVisible()` seems to be ignored by the browser component - I’m explicitly setting it invisible and never calling `setVisible(true)` but it’s still showing up - maybe I’m missing something but thought I’d mention it

---

<div class="post-metadata">

**Author:** ![rollo](https://avatars.discourse-cdn.com/v4/letter/r/e9bcb4/32.png) [@rollo](https://forum.juce.com/u/rollo)\
**Post date:** [July 23, 2024, 5:20pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/12 "2024-07-23T17:20:51Z")

</div>

I’m on mac and didn’t test on windows yet.  
I was annoyed with the white background when opening a window as well.

What i did was:

```cpp
void setBrowserVisible ()
{
    browserFinishedLoading = true;
    resized();
}

bool browserFinishedLoading = false;

SinglePageBrowser webComponent {juce::WebBrowserComponent::Options {}
                                   //.withAllWantedOptions
        .withNativeFunction("completedLoading", [this] (auto&var, auto complete) {
               setBrowserVisible();
               complete(juce::var()); // Complete the promise with an empty result
           })
juce::URL {localDevServerAddress}.getOrigin())};

void PluginEditor::resized()
{
    if (browserFinishedLoading) { webComponent.setBounds (getLocalBounds()); }
    else { webComponent.setBounds (0, 0, 1, 1); } // Yes this feels hacky!
}

```

and in App.js

```js
const completedLoading = Juce.getNativeFunction("completedLoading");
function App() {

  useEffect(() => {
    completedLoading().then((result) => {});
  }, []); 
...
}

```

I tried to solve it with `.setVisible(true)` after the loading finished, but the webbrowser wouldn’t load if it wasn’t visible.

@attila what are the changes of getting faster browser/webview loadings? 😃  
I’m getting around half a second between window opens and content is displayed.

---

<div class="post-metadata">

**Author:** ![MeijisIrlnd](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@MeijisIrlnd](https://forum.juce.com/u/MeijisIrlnd)\
**Post date:** [July 24, 2024, 2:41pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/13 "2024-07-24T14:41:37Z")

</div>

Thanks for the snippet, will give this a try later on!

@attila were you able to recreate the setVisible issue? To clarify it a little:

```cpp
PluginEditor::PluginEditor() { 
    addChildComponent(&m_webBrowser);
    // Should already be invisible iirc, but to sanity check::
    m_webBrowser.setVisible(false);
}

void PluginEditor::resized() { 
    m_webBrowser.setBounds(getLocalBounds()); // This is still visible, but not interactable
    // Waiting to call setBounds until the page has loaded works, so it seems like something is either happening in setBounds that *calls* setVisible, or there's something weird going on with setVisible in the first place
}

```

It’s possible I’ve missed something in the docs about setVisible and the browser, but thought I’d verify that this wasn’t intended behaviour!

---

<div class="post-metadata">

**Author:** ![attila](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/attila/32/11206_2.png) [@attila](https://forum.juce.com/u/attila)\
**Post date:** [July 24, 2024, 2:52pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/14 "2024-07-24T14:52:59Z")

</div>

Thanks, I think I can investigate it based on the information you provided. This just needs to wait a little.

---

<div class="post-metadata">

**Author:** ![attila](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/attila/32/11206_2.png) [@attila](https://forum.juce.com/u/attila)\
**Post date:** [September 13, 2024, 1:21pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/15 "2024-09-13T13:21:16Z")

</div>

A fix for this issue has been released on `develop`

> <https://github.com/juce-framework/JUCE/commit/5b98066a48ea87e4fe26a10f9243624ad422ef5e>
>
> Prior to this commit a WebBrowserComponent created with
> setVisible (false) would… still be visible.

---

<div class="post-metadata">

**Author:** ![samithy](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@samithy](https://forum.juce.com/u/samithy)\
**Post date:** [December 27, 2024, 3:52pm UTC](https://forum.juce.com/t/bug-juce-8-webview-and-pagefinishedloading/61472/16 "2024-12-27T15:52:32Z")

</div>

FWIW this was really helpful but didn’t quite get me there. Even with a fairly simple webpage, the resize on setting visibility still caused the white flash. Pages still don’t load when the browser is invisible, so Instead I use (pseudocode):

```auto
if(pageReady){
  webComponent.setBounds(getLocalBounds());
} else {
  const auto w = getWidth();
  const auto h = getHeight();
  webComponent.setBounds (w-1, h-1, w, h);
}

```
