reloadForNewBuild() function

Available since @irtio/client 5.0Browsers

The reloadForNewBuild() function reloads the page after a 4004 close, so the browser loads the live build’s site, and returns whether it reloaded. It reloads at most once until a connection succeeds.

Syntax

reloadForNewBuild()

Parameters

None.

Return value

A boolean: true if it reloads the page, or false if it doesn’t. It returns false if it already reloaded since the last connection that succeeded, outside a browser and when sessionStorage is blocked.

Exceptions

None.

Description

A client whose schema differs from the room build’s gets close code 4004, and an IrtError named VersionError. The page is older than the live build, so reloading it loads the new site.

A room still running an older build refuses the new site’s clients with 4004 too, until it hibernates, so reloading on every 4004 would loop. reloadForNewBuild() keeps a mark in sessionStorage when it reloads and returns false while the mark is there. A client that becomes ready clears it.

Reload only for 4004. The client already reconnects after the close codes that allow it, and a 4015, a room of another type, isn’t fixed by reloading.

Examples

Reloading when a join is refused

When connect() rejects with a VersionError, the page reloads once. If it already has, the player gets a message instead.

// join-latest.ts
import { connect, IrtError, reloadForNewBuild } from "@irtio/client";

export async function join() {
  try {
    return await connect("main");
  } catch (e) {
    if (!(e instanceof IrtError) || e.name !== "VersionError") throw e;
    // The room still runs the older build until it hibernates
    if (!reloadForNewBuild()) console.warn("the game was updated; try again in a minute");
    return undefined;
  }
}

Reloading when a new build goes live mid-game

A connected client that reconnects to a room running a new build closes with 4004.

// reload-on-close.ts
import { CloseCode, reloadForNewBuild, type ReadyRoomClient } from "@irtio/client";

export function watchForNewBuild(room: ReadyRoomClient) {
  room.addEventListener("close", ({ error }) => {
    if (error?.code === CloseCode.SchemaMismatch && reloadForNewBuild()) return;
    if (error) console.error(`left the room: ${error.message}`);
  });
}

See also

Runtime availability

Browsers
reloadForNewBuild()5.0