HotUpdater.updateBundle()

The updateBundle function downloads and applies a new update bundle to your React Native application. It uses the provided bundle information obtained from checkForUpdate.

This method is particularly useful when you need a custom update strategy without using the built-in wrap method.

Usage

Use updateBundle to download and apply an available update by providing the bundle's unique identifier and the URL to the bundle file.

import { HotUpdater, getUpdateSource } from "@hot-updater/react-native";

async function applyAppUpdate(updateInfo: UpdateInfo) {
  try {
    const updateInfo = await HotUpdater.checkForUpdate({
      source: getUpdateSource("<your-update-server-url>", {
        updateStrategy: "fingerprint", // or "appVersion"
      }),
      requestHeaders: {
        Authorization: "Bearer <your-access-token>",
      },
    });

    if (!updateInfo) {
      return {
        status: "UP_TO_DATE",
      };
    }

    /**
     * You can apply updates using one of two methods:
     * 
     * Method 1: Use the updateBundle() method from the updateInfo object
     * - A convenience method built into the return value from checkForUpdate
     * - Performs the same function as HotUpdater.updateBundle with all required arguments pre-filled
     */
    // await updateInfo.updateBundle();

    /**
     * Method 2: Call HotUpdater.updateBundle() directly
     * - Explicitly pass the necessary values extracted from updateInfo
     */
    await HotUpdater.updateBundle({
      bundleId: updateInfo.id,
      fileUrl: updateInfo.fileUrl,
      status: updateInfo.status,
    });


    if (updateInfo.shouldForceUpdate) {
      HotUpdater.reload();
    }

    console.log("Update applied successfully");
  } catch (error) {
    console.error("Failed to apply update:", error);
  }
}

UnderstandinggetUpdateSource

The getUpdateSource function is used to construct the final URL for checking for updates. It takes a baseUrl as its first argument and an options object as its second argument. The updateStrategy property within the options object determines the final structure of the request URL.

Example Final Endpoint Structures:

Depending on the updateStrategy value, getUpdateSource generates URLs like the following:

  • For updateStrategy: 'appVersion': GET {baseUrl}/app-version/:platform/:appVersion/:channel/:minBundleId/:bundleId
  • For updateStrategy: 'fingerprint': GET {baseUrl}/fingerprint/:platform/:fingerprintHash/:channel/:minBundleId/:bundleId

For example, if you provide https://your-update-server.com/api/update-check as the baseUrl, getUpdateSource will append the correct path and parameters to the URL depending on whether updateStrategy is 'appVersion' or 'fingerprint'.

Parameters

The updateBundle function accepts the following parameters:

ParameterTypeRequiredDescription
idstringUnique identifier of the update bundle.
fileUrlstringURL from which the update bundle will be downloaded.

Behavior

  • Downloads the specified bundle from the provided fileUrl.
  • Applies the downloaded bundle as the active bundle for the application.
  • Requires an explicit call to HotUpdater.reload() if you want to immediately reload the application after updating, particularly when shouldForceUpdate is true.