This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
优化更新
编辑页面
本文档已于 2022 年 8 月归档,今后将不再接收任何更新。请改用 EAS Update。了解更多
What’s included in an update
An update to an Expo app includes JavaScript, the manifest, images, and other assets, which can be downloaded and run by compatible Expo client apps such as Expo Go and production apps.
On Android and iOS, the standalone app you submit to the app stores is an example of the client app that runs your updates. On the web, the web browser is the client app that runs the update. In fact, on the web, an Expo update is essentially a web app.
Updates are bundled into the standalone app, and subsequent updates are delivered over the internet after the standalone app is built. Because downloading updates over the internet consumes bandwidth and takes time, one of the most effective ways to optimize update delivery is to make it smaller. This guide introduces several ways to reduce the size of updates.
Estimating the size of an update
One way to estimate update size is to run expo export, which exports a self-hostable update for each platform. That update includes a JSON manifest, a JavaScript bundle, and several assets, such as images and custom fonts.
The exported update contains uncompressed files unless the file format itself is compressed, such as JPEG and PNG. The size of the update embedded in a standalone app is typically the sum of the file sizes for that update.
When updates are served through the updates service, they are compressed with gzip. To estimate the size of a manifest or JavaScript bundle served through the updates service, run gzip <file>. Other files, such as JPEG images and MP3 audio files, are not further compressed by the CDN because their file formats are already compressed.
updates service 的限制
可以把向你的 Expo 应用发布更新理解为发布你网站的新版本。事实上,当你为 Expo Web 应用发布更新时,你发布的就是你站点的新版本。无论底层平台如何,用户都会在 Android、iOS 和 Web 上运行你的应用并下载 Expo 更新;而你的更新越小,用户就越能快速、稳定地下载它们,并且在蜂窝网络连接上消耗更少的数据流量。
Expo 当前的 updates service 设计用于容纳大约 50 MiB 的更新,这些更新由 JS 代码和资源组成。许多工程做得很好的生产应用远远小于这个大小,这有助于带来更好的用户体验。
下面是一些有助于减小更新体积的通用技术。其中很多也是优化网站的技术,因为 Expo 更新和网站都是通过 Web 提供的。
优化图片
如果很多图片之前没有经过优化,那么它们的体积通常可以减少 30% 以上。优化图片的一种简单方法是将其调整为应用实际使用的尺寸;如果你的图片尺寸是 4032x3024,但应用只需要显示一张 400x300 的图片,那么使用高质量插值算法(如 bicubic sharpening)缩小图片会大幅减小体积。
另一种优化图片的方法是使用类似 expo-optimize 的优化器重新编码,它会优化你 Expo 项目中所有兼容的图片:
- npm install -g sharp-cli- npx expo-optimize <project-directory> [options]还有许多其他你可能会喜欢使用的图片优化器,例如 jpegoptim、guetzli、pngcrush 或 optipng。imagemin 也是一个支持各种优化器插件的程序和 JS 库。还有许多在线服务可以为你优化图片。
有些图片优化器是无损的。这意味着它们会重新编码你的图片,使其更小,同时不会改变所显示像素中的任何内容,也不会造成任何信息损失。当你需要每个像素都与原始图片保持一致时,无损优化器和 PNG 这类无损图片格式是不错的选择。
另一些图片优化器是有损的,这意味着优化后的图片与原始图片不同。通常,有损优化器更高效,因为它们会丢弃一些视觉信息,从而减小文件大小,同时让图片对人眼看起来几乎相同。像 imagemagick 这样的工具可以使用 SSIM 等比较算法来判断两张图片看起来有多相似。一个经过优化、与原图相似度超过 95% 的图片,其文件大小远低于原文件大小的 95% 是很常见的!
减少大型依赖
大型 npm 依赖会显著增加更新中代码的体积。虽然像 expo、react 以及其他一些包是必要且非常有用的,但有时某个依赖即使你只用到其中一个方法,它也可能大得惊人;或者你在 JS bundle 中可能包含了同一个依赖的多个版本。react-native-bundle-visualizer 既适用于 Expo 应用,也适用于裸 React Native 应用,并且可以详细展示 JS bundle 中的文件。
在需要时下载文件
有时应用会包含一些在应用启动时并不需要立即使用的文件。在这种情况下,你可以把这些额外文件上传到自己的服务器,并在应用代码中通过 fetch(url) 或 <Image source={{ uri: url }}> 等方式引用这些 URL。通过按需下载这些文件,你可以把它们移出更新,从而减小更新体积。
在自己的电脑上构建应用
如果你需要构建包含大型资源的应用,例如内嵌的大视频,一个简单而实用的解决方案是使用 bare workflow,并通过 Android Studio 和 Xcode 编译应用。你既可以使用自己的电脑,也可以在云服务提供商上配置 Linux 和 macOS VM,并在这些虚拟机上运行 Android 和 iOS 的标准构建工具链。
使用 expo-updates,你还可以在自己的服务器上托管更新,这可以支持任意大的文件。
自托管你的更新
除了在自己的电脑上构建应用之外,你还可以在自己的服务器上托管更新。更多内容请阅读在你的服务器上托管更新。