Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable background throttling #12181

Open
wants to merge 24 commits into
base: dev
Choose a base branch
from

Conversation

bastiankistner
Copy link

@bastiankistner bastiankistner commented Jan 3, 2025

This PR relates to #5250

It requires a change of wry for which I've opened another PR: tauri-apps/wry#1445

I've added an option disableBackgroundThrottling to allow developers to choose if their view should automatically be throttled / suspended by the browser after a certain time of being hidden or minimised. In the worst case, this default behaviour of browsers can lead to broken applications running in the background.

For instance if a websocket connection is the only reason not to suspend a "tab" and the machine goes into sleep mode and wakes up again, the connection is often lost and the browser won't have any reason to keep the background tab active and therefore unload it, which will not allow the hidden view to "heal" itself.

This default behaviour effectively renders any browser capabilities unusable for long running background processes. One alternative might be moving logic from the frontend to the backend, but browsers have meanwhile become a great choice for local first apps and if the browser already offers the required features easily accessible, why force developers to leave a territory they feel comfortable with?

As of now, this is only available for WebKit based browsers on iOS 17.0+,iPadOS 17.0+,Mac Catalyst 17.0+, macOS 14.0+, visionOS 1.0+ ( see https://developer.apple.com/documentation/webkit/wkpreferences/inactiveschedulingpolicy-swift.enum?language=objc ).

It can be assumed that it will also be implemented for WebView2 (Windows). And it might be that it's already usable for linux and android, although I couldn't find any documentation about it and also cannot test it locally due to a lack of devices.

As an alternative for windows and non-webkit browsers, one can setup a pending WebLock transaction, which, according to the docs, can also keep the browser from suspending it.


I needed to update a snapshot test and am unsure if this is at all related to this change. Please let me know if I did something wrong here.


Pending Tasks

Copy link
Member

@FabianLars FabianLars left a comment

Choose a reason for hiding this comment

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

thanks so much. If nothing major changes in the Wry PR i only have 2 very small comments:

.changes/disable-background-throttling.md Outdated Show resolved Hide resolved
crates/tauri-runtime/src/webview.rs Outdated Show resolved Hide resolved
Copy link
Contributor

github-actions bot commented Jan 6, 2025

Package Changes Through c62e002

There are 8 changes which include tauri-cli with minor, tauri-runtime with minor, tauri-runtime-wry with minor, tauri-utils with minor, tauri with minor, @tauri-apps/api with minor, @tauri-apps/cli with minor, tauri-bundler with patch

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
@tauri-apps/api 2.2.0 2.3.0
tauri-utils 2.1.1 2.2.0
tauri-bundler 2.2.1 2.2.2
tauri-runtime 2.3.0 2.4.0
tauri-runtime-wry 2.3.0 2.4.0
tauri-codegen 2.0.4 2.0.5
tauri-macros 2.0.4 2.0.5
tauri-plugin 2.0.4 2.0.5
tauri-build 2.0.5 2.0.6
tauri 2.2.2 2.3.0
@tauri-apps/cli 2.2.4 2.3.0
tauri-cli 2.2.4 2.3.0

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@bastiankistner
Copy link
Author

@FabianLars I changed the comments according to your suggestions and also updated the wry PR with a version check. Let me know if there is anything else I should do before this PR can be merged.

@bastiankistner
Copy link
Author

I changed the wry version to 0.48.1

@bastiankistner
Copy link
Author

@FabianLars is there something still missing in this PR or can we merge this with the release of wry 0.48.1 ?

@FabianLars
Copy link
Member

sorry, i was waiting with minor PRs intentionally to not block patch releases until the webview2-com and windows updates, but that ended up with me completely ignoring those prs.

Just skipping over the code one major issue i see is that tauri-utils cannot depend on wry, so that has to be reverted. That may also fix some of the build issues.

Api wise i don't think it makes sense to have Option in the WebviewBuilder and WebviewWindowBuilder setter methods. We can use the enum directly here.

Lastly 2 small doc things: 1) the enum and maybe the methods should document clearly what the default variant is. 2) The since tags should be 2.3.0 - currently they are 2.2.0 (and one is 2.0.0)

@bastiankistner
Copy link
Author

bastiankistner commented Jan 14, 2025

@FabianLars thank you! Implemented the requested changes.

I have one question left: Since we have the invoke('plugin:window|set_background|throttling') here https://github.com/bastiankistner/tauri/blob/b82b6efc96c9d62b75f1086ae0435e1630541e03/packages/api/src/window.ts#L1553, don't we also need the corresponding methods in tauri-runtime/src/lib.rs in WindowDispatch and in tauri/src/webview/mod.rs for impl Webview and probably in crates/tauri/src/webview/plugins.rs ?

I'm uncertain if/how this works. I doubt that I can change the value once the webview is loaded. But when I open a new window through JS, it should be possible to specify it.

@FabianLars
Copy link
Member

ah didn't even notice that. since wry has no runtime method for this, only one to set it on the builder you can remove that ts function again. Only the WebviewOptions property will work here.

therefore this is a bit offtopic but the setter function would belong in webview.ts not window.ts because well, it's a webview method and not related to the window / windowing library.

@bastiankistner
Copy link
Author

@FabianLars removed it. Also had to update snapshot tests. I don't really know why. Would be great if you could let me know if I should undo those. Maybe it's due to my environment?

@FabianLars
Copy link
Member

never touched nor looked at the snapshot stuff so no idea 😂

@bastiankistner
Copy link
Author

@FabianLars is there anything missing or can we merge it soon?

@FabianLars
Copy link
Member

i don't see anything missing, no. Just wanted to release a patch release before merging the minor prs. I currently have monday in mind to start merging those because i'm busy over the weekend.

apologies for the long wait times.

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.

2 participants