facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
118.62k stars 24.29k forks source link

What is the future of Headless JS? #45731

Closed kross-italk closed 2 months ago

kross-italk commented 2 months ago

Description

After going through the implementation, HeadlessJS is poorly advertised. By its name sake, one would expect "Headless" and "Background" to be synonmous. That doesn't appear to be the case:

https://github.com/facebook/react-native/issues/15915 https://github.com/facebook/react-native/issues/27835 https://github.com/facebook/react-native/issues/43950 https://github.com/facebook/react-native/issues/36493

Headless JS tasks have to be started from the Main UI thread, and when the app is put in the background (but left running) the tasks stop. This is next to useless, I could always have asyncrously scheduled my jobs in the main JS conext while the app was forgrounded. The very existence of the "Crash in foreground" being the default suggests this isn't going to be considered.

The documentation was very rough and I had to cobble together various open source repos that demonstrate using it.

How is one expected to make a real background service using Headless JS? Is there any future to this code, or has it gone stale?

Steps to reproduce

  1. install react-native
  2. Kotin: applicationContext.startService(service)
  3. Background the app by sliding it up and picking another app
  4. All JS activity is stopped, network requests, timers, the whole JS context is evicted.

React Native Version

0.73.6

Affected Platforms

Runtime - Android

Output of npx react-native info

root@836c7ff65b76:/usr/src/app# npm info
npm notice
npm notice New minor version of npm available! 10.7.0 -> 10.8.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.2
npm notice To update run: npm install -g npm@10.8.2
npm notice
npm error code E404
npm error 404 Not Found - GET https://registry.npmjs.org/- Not found
npm error 404
npm error 404  '@*' is not in this registry.
npm error 404
npm error 404 Note that you can also install from a
npm error 404 tarball, folder, http url, or git url.

npm error A complete log of this run can be found in: /root/.npm/_logs/2024-07-26T13_08_10_383Z-debug-0.log
root@836c7ff65b76:/usr/src/app# ^C
root@836c7ff65b76:/usr/src/app# exit
exit

Stacktrace or Logs

If you specify "Allow to run in foregound" the headless service works until backgrounded.
Otherwise the application crashes.

Reproducer

### Screenshots and Videos _No response_
react-native-bot commented 2 months ago
:warning: Newer Version of React Native is Available!
:information_source: You are on a supported minor version, but it looks like there's a newer patch available - 0.73.9. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.
react-native-bot commented 2 months ago
:warning: Missing Reproducible Example
:information_source: We could not detect a reproducible example in your issue report. Please provide either:
  • If your bug is UI related: a Snack
  • If your bug is build/update related: use our Reproducer Template. A reproducer needs to be in a GitHub repository under your username.
react-native-bot commented 2 months ago
:warning: Missing Reproducible Example
:information_source: We could not detect a reproducible example in your issue report. Please provide either:
react-native-bot commented 2 months ago
:warning: Newer Version of React Native is Available!
:information_source: You are on a supported minor version, but it looks like there's a newer patch available - undefined. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.
kross-italk commented 2 months ago

And listen, pushing everyone in the community away with an auto-close bot isn't helping.

cortinico commented 2 months ago

And listen, pushing everyone in the community away with an auto-close bot isn't helping.

That's because this is an issue tracker only. It's not the right spot for questions/discussions. The best place is here https://github.com/react-native-community/discussions-and-proposals (there is even a button in https://github.com/facebook/react-native/issues/new/choose).

To answer your question: we'll most likely deprecate and remove Headless JS in a future version. We'll open a discussion in the coming months to collect community feedback from it, but the reality is that is not an API we'd recommend.

We're not using it internally at Meta, and it's backed by an Android Service which is a bad choice in 2024 for background execution. The documentation should also be updated to mention to don't use this API anymore.

The alternative would be to update Headless JS API to depend on WorkManager or JobScheduler but we don't have capacity at the moment to do this work. A community contribution would be more than welcome in this sense.