优化 EAS 更新资源
了解 EAS Update 如何下载资源以及如何优化它们的下载大小。
信息 新的资源选择功能可以大大减少下载资源的总数量和大小。
当应用发现新的更新时,它会先下载一个清单,然后下载任何新的或已更新的资源,以便能够运行更新。流程如下:
🌐 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 中,我们可以看到将分别用于 Android 和 iOS 更新的 index.android.js 和 index.ios.js 文件的大小。请注意,这些是未压缩的文件大小;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:
- 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.
- 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:
- 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.