首页指南参考教程

优化 EAS 更新资源

了解 EAS Update 如何下载资源以及如何优化它们的下载大小。


对于 SDK 50 及以上版本,新的 资源选择功能 可以大大减少下载资源的总数和大小。

当应用发现新的更新时,它会下载清单,然后下载任何新的或更新的资源,以便可以运行更新。流程如下:

¥When an app finds a new update, it downloads a manifest and then downloads any new or updated assets so that it can run the update. The process is as follows:

许多运行 Android 和 iOS 应用的用户所使用的移动连接不如使用 Wi-Fi 时那样一致或快速,因此作为更新的一部分提供的资源尽可能小非常重要。

¥Many users running Android and iOS apps are using mobile connections that are not as consistent or fast as when they are using Wi-Fi, so it's important that the assets shipped as a part of an update are as small as possible.

代码资源

¥Code assets

发布更新时,EAS CLI 运行 Expo CLI 将项目打包到更新中。更新将出现在我们项目的 ./dist 目录中。

¥When publishing an update, EAS CLI runs Expo CLI to bundle the project into an update. The update will appear in our project's ./dist directory.

在 ./dist/bundles 中,我们可以看到 index.android.js 和 index.ios.js 文件的大小,它们将分别成为 Android 和 iOS 更新的一部分。请注意,这些是未压缩的文件大小;EAS Update 使用 Brotli 和 gzip 压缩,可以显着减少下载大小。不过,如果设备之前未下载过这些文件,那么在获取新更新时,这些文件将被下载到用户的设备上。使这些文件大小尽可能小有助于终端用户快速下载更新。

¥In ./dist/bundles, we can see the size of the index.android.js and index.ios.js files that will be part of the Android and iOS updates, respectively. Note that these are uncompressed file sizes; EAS Update uses Brotli and gzip compression, which can significantly reduce download sizes. Nevertheless, these files will be downloaded to a user's device when getting the new update if the device has not downloaded them before. Making these file sizes as small as possible helps end-users download updates quickly.

图片资源

¥Image assets

如果这些资源尚未成为其版本的一部分,则应用用户在检测到新更新时必须下载任何新图片或其他资源。你可以在./dist/assets 中查看上传到 EAS 服务器的所有资源。那里的资源经过哈希处理,并删除了扩展名,因此很难知道那里有哪些资源。要查看打印精美的资源列表,我们可以运行:

¥App users will have to download any new images or other assets when they detect a new update if those assets are not already a part of their build. You can view all the assets uploaded to EAS servers in ./dist/assets. The assets there are hashed with their extensions removed, so it is difficult to know what assets are there. To see a pretty-printed list of assets, we can run:

Terminal
npx expo export

优化图片资源

¥Optimizing image assets

要手动优化项目中的图片资源,你可以使用 npx expo-optimize 命令。它使用 sharp 库来压缩图片。

¥To manually optimize image assets in your project, you can use the npx expo-optimize command. It uses sharp library to compress images.

Terminal
npx expo-optimize

运行命令后,除已优化的图片资源外,所有图片资源都将被压缩。你可以通过在命令中包含 --quality [number] 选项来调整压缩质量。例如,要压缩到 90%,请运行:

¥After running the command, all image assets are compressed except the ones that are already optimized. You can adjust the compression quality by including the --quality [number] option with the command. For example, to compress to 90%, run:

Terminal
npx expo-optimize --quality 90

其他手动优化方法

¥Other manual optimization methods

要手动优化图片和视频,请参阅 资源 了解更多信息。

¥To optimize images and videos manually, see Assets for more information.

确保资源包含在更新中

¥Ensuring assets are included in updates

当你发布更新时,EAS 会将你的资源上传到 CDN,以便用户在运行你的应用时可以获取它们。但是,对于要上传到 CDN 的资源,必须在应用代码的某个地方明确要求它们。有条件地要求资源将导致打包器无法检测到它们,并且在你发布项目时不会上传它们。

¥When you publish an update, EAS will upload your assets to the CDN so that they may be fetched when users run your app. However, for assets to be uploaded to the CDN, they must be explicitly required somewhere in your application's code. Conditionally requiring assets will result in the bundler being unable to detect them, and they will not be uploaded when you publish your project.

进一步的考虑

¥Further considerations

需要注意的是,用户的应用只会下载新的或更新的资源。它不会重新下载应用中已存在的未更改的资源。

¥It's important to note that a user's app will only download new or updated assets. It will not re-download unchanged assets that already exist inside the app.

确保更新尽可能少的一种方法是经常构建应用并将其提交到应用商店,以便用户可以下载包含更多最新资源的新应用二进制文件。通常,在添加大型或多个资源时构建和提交应用是一种很好的做法,并且最好使用更新来修复小错误并在应用商店版本之间进行微小更改。

¥One way to make sure that updates stay as slim as possible is to build and submit the app frequently to the app stores so that users can download a new app binary that includes more up-to-date assets. Generally, it's a good practice to build and submit an app when adding large or multiple assets, and it's good to use updates to fix small bugs and make minor changes between app store releases.

Expo 中文网 - 粤ICP备13048890号