配置更新


本文档于 2022 年 8 月存档,不会收到任何进一步更新。请改用 EAS 更新。了解更多

¥This doc was archived in August 2022 and will not receive any further updates. Please use EAS Update instead. Learn more

Expo 提供了各种设置来配置你的应用接收更新的方式。更新允许你发布应用 JavaScript 和资源的新版本,而无需构建应用的新版本并将其重新提交到应用商店。(了解有关限制的更多信息)。

¥Expo provides various settings to configure how your app receives updates. Updates allow you to publish a new version of your app JavaScript and assets without building a new version of your app and re-submitting it to app stores. (Read more about the limitations).

要创建应用的更新,请运行 expo publish。如果你使用发布通道,请使用 --release-channel <channel-name> 选项指定一个。请注意 - 如果你希望更新应用的 SDK 版本或制作 任何这些变化,则需要使用 eas build 重建应用并将二进制文件上传到相应的应用商店 (请参阅此处的文档)。

¥To create an update of your app, run expo publish. If you're using release channels, specify one with --release-channel <channel-name> option. Please note- if you wish to update the SDK version of your app, or make any of these changes, you'll need to rebuild your app with eas build and upload the binary file to the appropriate app store (see the docs here).

更新由 app.json 中的 updates 设置控制,该设置处理初始应用加载,以及更新 SDK 模块,该模块允许你从 JS 异步获取更新。

¥Updates are controlled by the updates settings in app.json, which handle the initial app load, and the Updates SDK module, which allows you to fetch updates asynchronously from your JS.

自动更新

¥Automatic updates

如果你的 app.json 不包含 updates.fallbackToCacheTimeout 字段,它将默认为 0,而 expo-updates 将尝试使用缓存的打包包立即启动你的应用,同时在后台下载较新的打包包以供将来使用。使用此配置时,从商店下载应用并首次启动它的用户将始终看到构建二进制文件所针对的应用版本。Updates.addListener 提供了一个钩子,让你在新包下载完成时做出响应。你可以使用它来通知用户应该重新启动应用,你也可以使用 Updates.reloadAsync() 以编程方式重新启动它,例如:响应用户的提示。

¥If your app.json does not contain an updates.fallbackToCacheTimeout field it will default to 0 and expo-updates will attempt to start your app immediately with a cached bundle while downloading a newer one in the background for future use. When using this configuration, users who download the app from a store and launch it for the first time will always see the version of the app that the binary was built against. Updates.addListener provides a hook to let you respond when the new bundle is finished downloading. You can use this to notify users that they should restart the app, and you can also restart it programmatically with Updates.reloadAsync(), eg: in response to a prompt to the user.

你还可以设置非零超时,在这种情况下,Expo 将在启动应用之前尝试下载新的更新。如果没有可用的网络连接,或者尚未在指定时间内完成下载,Expo 将回退到加载应用的缓存版本,并继续尝试在后台获取更新(此时它将被保存) 进入缓存以供下一个应用加载)。我们强烈建议不要在获取更新时阻止应用加载,因为如果下载更新需要一段时间,它可能会给用户留下应用无法运行的感觉,并且每次启动都会变慢,因为必须发出网络请求 在继续使用该应用之前检查是否有更新。

¥You can also set a nonzero timeout, in which case Expo will attempt to download a new update before launching the app. If there is no network connection available, or it has not finished downloading in the allotted time, Expo will fall back to loading a cached version of your app, and continue trying to fetch the update in the background (at which point it will be saved into the cache for the next app load). We strongly advise against blocking app loading on fetching an update because it can leave users with the perception that the app is not working if it takes a while to download the update, and every launch will be slower because a network request has to be made to check for an update before proceeding to the app.

在 Expo Go 中,无论应用配置中的 updates 设置如何,都将始终启动最新发布的应用包(除非请求超时或网络连接不可用)。

¥In Expo Go, the latest published bundle of your app will always be launched (unless the request times out or the network connection is unavailable), regardless of the updates settings in your app config.

手动更新

¥Manual updates

在独立应用中,还可以关闭自动更新并完全在 JS 代码中控制更新。如果你想要一些关于获取更新的自定义逻辑(例如,仅通过 Wi-Fi),那么这是理想的选择。

¥In standalone apps, it is also possible to turn off automatic updates and to instead control updates entirely within your JS code. This is desirable if you want some custom logic around fetching updates (for example, only over Wi-Fi).

在 app.json 中将 updates.checkAutomatically 设置为 "ON_ERROR_RECOVERY" 将阻止 Expo 在每次启动应用时自动获取最新更新。仅加载打包包的最新缓存版本。如果缓存包的最后一次运行产生了致命的 JS 错误,它只会自动获取更新。

¥Setting updates.checkAutomatically to "ON_ERROR_RECOVERY" in app.json will prevent Expo from automatically fetching the latest update every time your app is launched. Only the most recent cached version of your bundle will be loaded. It will only automatically fetch an update if the last run of the cached bundle produced a fatal JS error.

然后,你可以使用 expo-updates 模块下载新更新,并在适当的情况下通知用户重新加载其应用。

¥You can then use the expo-updates module to download new updates and, if appropriate, notify the user to reload their app.

import * as Updates from 'expo-updates';

try {
  const update = await Updates.checkForUpdateAsync();
  if (update.isAvailable) {
    await Updates.fetchUpdateAsync();
    // ... notify user of update ...
    await Updates.reloadAsync();
  }
} catch (e) {
  // handle or log error
}

与任何网络调用一样,检查更新会占用设备的带宽和电池寿命。此外,Expo 提供的更新可能会受到速率限制。明智地检查更新的一个好的经验法则是在用户启动或前台应用时使用检查。避免频繁循环轮询更新。

¥Checking for an update uses a device's bandwidth and battery life like any network call. Additionally, updates served by Expo may be rate limited. A good rule of thumb to check for updates judiciously is to use check when the user launches or foregrounds the app. Avoid polling for updates in a frequent loop.

请注意,尽管命令式 Updates 方法仍将正常运行,但 Expo Go 中将忽略 checkAutomatically: "ON_ERROR_RECOVERY"

¥Note that checkAutomatically: "ON_ERROR_RECOVERY" will be ignored in Expo Go, although the imperative Updates methods will still function normally.

禁用更新

¥Disabling updates

通过在 app.json 中将 updates.enabled 设置为 false,可以完全禁用应用中的更新。这将忽略从 Expo 服务器获取应用包的所有代码路径。在这种情况下,应用的所有更新都需要通过 iOS App Store 和/或 Google Play Store 进行。

¥It is possible to entirely disable updates in an app, by setting updates.enabled to false in app.json. This will ignore all code paths that fetch app bundles from Expo's servers. In this case, all updates to your app will need to be routed through the iOS App Store and/or Google Play Store.

Expo Go 应用中会忽略此设置。

¥This setting is ignored in the Expo Go app.

Expo 中文网 - 粤ICP备13048890号