Skip to content

Conversation

@t-arn
Copy link
Contributor

@t-arn t-arn commented Jan 8, 2026

This PR adds a workaround for Windows, so that WebView content > 2MB can still be displayed without causing a WebView2 error: The content is written to a temporary file in the 'Cache' directory. This file is deleted when the page load is finished.

Although the Microsoft documentation says that the limit is at 2 MB, the tests have revealed that the limit actually is at 1.5 MB

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

@t-arn
Copy link
Contributor Author

t-arn commented Jan 8, 2026

@freakboy3742 Please review this PR

Copy link
Contributor

@johnzhou721 johnzhou721 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 small notes, otherwise this is a great feature.

if self._large_content_filepath and os.path.exists(
self._large_content_filepath
):
os.remove(self._large_content_filepath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure of the goals of WebView.... but navigating back to the previous page is an usual use case for a web view widget. As written, if you navigate to another page from the static content loaded, and you navigate back, it'll probably error. IMO it would be safer to defer removing the large content file when the widget is discussed.

Copy link
Contributor Author

@t-arn t-arn Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I could use a static file name or a list of uuid. Then, the temporary file(s) could be deleted in the WebView destructor

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of a cache folder is that you shouldn't need to do this sort of cleanup; it should be an automated operation that happens independent of widget lifecycle.

@@ -0,0 +1 @@
The 2MB limit for setting static WebView content has been removed for Windows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 cosmetic things:

  1. missing period at end
  2. IMO it'll be clearer to say "2MB size limit" not just "2MB limit".
  3. This shouldn't be a misc note -- it has user impact. I believe bugfix would be appropriate.

@t-arn
Copy link
Contributor Author

t-arn commented Jan 9, 2026

Would this feature also make sense for other platforms, even when there is no documented upper limit?

@johnzhou721
Copy link
Contributor

@t-arn The way you test for that is by making a non platform-specific test, i.e. removing the branch of line 210 of your testbed change. And then we see if it fails on other platforms... if it does but you feel it will be scope creep if worked on in this PR, add a property to the probes and use that to exclude platforms, instead of platform-specific checks in the testbed.

If you want to exclude platforms stuff, instead of a probe you can also use a probe-specific method to perform an operation specific to the platform, and pytest.skip on others. That makes it easier to find missing functionalities through test probes.

@freakboy3742
Copy link
Member

Would this feature also make sense for other platforms, even when there is no documented upper limit?

This isn't a "feature" - it's a bug fix for a specific detail/limitation of the Winforms implementation of WebView. If a similar limitation exists for other platforms, then yes - that should be resolved; any test that you add for this feature should be in the testbed, which means any platform-specific limitation will be exposed.

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broadly makes sense; a couple of comments about cache strategy inline.

self.native.NavigateToString(content)
if len(content) > 1500000:
# according to the Microsoft documentation, the max content size is
# 2 MB, but in fact, the limit seems to be at 1.5 MB
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it literally 1.5MB, or is this a Megabyte vs Mebibyte discrepancy?

# according to the Microsoft documentation, the max content size is
# 2 MB, but in fact, the limit seems to be at 1.5 MB
file_name = str(uuid.uuid4()) + ".html"
self._large_content_filepath = toga.App.app.paths.cache / file_name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that a root_url is a required part of the set_content() call, that would seem to be the appropriate cache key. I'd also suggest that the base cache path isn't an appropriate directory location - we should be putting webview cache content into some sort of internal Toga directory (e.g., paths.cache / "toga/webview"). There's also a need to differentiate this widget from others - if I have 2 web views ,both displaying "index.html", I don't want them to collide in the cache.

if self._large_content_filepath and os.path.exists(
self._large_content_filepath
):
os.remove(self._large_content_filepath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of a cache folder is that you shouldn't need to do this sort of cleanup; it should be an automated operation that happens independent of widget lifecycle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants