How-to guide·3D viewerJavaScript

Work offline

Prepare models for offline use while online, then load them in the 3D viewer without a connection — what is stored, what works offline and what does not, errors, storage, and keeping the offline set up to date.

A 3D viewer on a building site, in a basement or on a plane has no server to stream from. Offline mode lets it load models from the browser instead: while online, the viewer downloads everything it needs for a set of model versions into the browser's IndexedDB; later, in offline mode, it loads those versions from there and makes no server calls at all.

Live demo#

Prepare, go offline, loadLive demo
Loads the demo model in this page with the demo API token of your account: open BIM.works and sign in once in this browser.

Click the numbered buttons in order. Prepare downloads the model into this browser, Go offline switches the viewer to offline mode, Load loads the model from the browser and counts the requests that reach the server meanwhile: none. Load an unprepared one shows the error for a model that was not prepared. Remove deletes the stored data again.

Before you start#

You have a viewer (Embed the viewer) and a credential that can load the models: fs:read, geometry:read and query:run. Preparing needs a connection; everything after it does not.

Steps#

import {ModelNotAvailableOfflineError} from "@bimworks/viewer3d";

const factory = new BimWorksClientFactory("https://bim.works");
const client = await factory.createClientWithApiToken("<your API token>");
const modelManager = new ModelManager(client);
const objectStateManager = new ObjectStateManager(modelManager);
const viewer = new Viewer3d(objectStateManager);
await viewer.initializeViewer();
document.getElementById("viewer").appendChild(viewer.container);

const versionUuids = ["<version uuid of the first demo model>", "<version uuid of the second demo model>"];

// 1. While online: download what the viewer needs into the browser (IndexedDB)
async function prepare() {
	const result = await viewer.prepareOfflineModels(versionUuids, {
		progress: (vid, percentage) => console.log(vid + ": " + percentage + "%")
	});
	for (const model of result.models) {
		// "prepared" with sizeBytes, "failed" with error, or "skippedUnsupported"
		console.log(model.vid, model.status, model.sizeBytes || model.error || "");
	}
	if (!result.persistentStorage) {
		console.warn("The browser may delete the offline data when it runs short of space");
	}
}

// 2. Later, with or without a connection: load from the browser
async function openOffline() {
	await viewer.setOfflineMode(true);
	// What is available, online and offline: [{vid, name, preparedAt, sizeBytes}]
	const available = await viewer.getOfflineModels();
	try {
		// The offline replacement for client.fs.getNodeVersion()
		const versions = await viewer.getOfflineVersions(available.map((model) => model.vid));
		await modelManager.addModels(versions);
	} catch (e) {
		if (e instanceof ModelNotAvailableOfflineError) {
			console.error("Not prepared for offline use: " + e.vids.join(", "));
		} else {
			throw e;
		}
	}
}

// 3. Back online; unloading works in both modes
async function goOnline() {
	await viewer.setOfflineMode(false);
}

async function forget() {
	await modelManager.removeModels(modelManager.getVersionsAsArray());
	await viewer.removeOfflineModels(versionUuids);
}

The three functions are the three moments of an offline application; call prepare() while online, openOffline() whenever the models are needed.

  1. Prepare while online. viewer.prepareOfflineModels(vids, {progress}) downloads, per version, the version object, the geometry metadata, the geometry, and the object data the ModelManager needs to load the model, and stores them in the browser. The result has one entry per version: prepared with its sizeBytes, failed with the error, or skippedUnsupported. A failure leaves no partial data behind. Preparing a version that is already stored replaces it.
  2. Enter offline mode. viewer.setOfflineMode(true) routes the viewer's loading to the browser's store. It does not unload what is loaded and does not touch your client, which keeps working for your own calls while there is a connection.
  3. Load from the store. viewer.getOfflineVersions(vids) is the offline replacement for client.fs.getNodeVersion(): it returns the stored Version objects, which go into modelManager.addModels as usual. viewer.getOfflineModels() lists what is stored (vid, name, preparedAt, sizeBytes) and works online and offline, so an offline page builds its model list from it.
  4. Leave and clean up. setOfflineMode(false) restores normal loading. modelManager.removeModels unloads in both modes. viewer.removeOfflineModels(vids) deletes the stored data; removing a version that is not stored does nothing.

What is stored#

Model Offline
IFC The geometry, the object data (types, storeys), and, when the server generated them, the walkable surfaces for walk mode and the space index for the current space.
PDF The first tiles of each page and its vertices: the sheet at the zoom levels it opens with.
Point clouds (E57, LAS, LAZ), GLB Not stored: skippedUnsupported. A point cloud streams its octree as the camera moves, which cannot be prepared in advance.

What works offline#

  • Loading, unloading, navigation, selection, colours, visibility and section planes, with Qids. The ModelManager's object data is stored, so modelManager.getTypeQids("IFCWALL") and the storeys of the loaded models are available offline.
  • Not BQL. A call with a query string (setVisibleSet("GET qid WHERE ..."), client.bim.query) goes to the server and fails offline. Pass Qids instead.
  • Not GEO mode: setMode("GEO") returns false while offline, because the map and terrain tiles come from the internet.
  • Not your page. The viewer stores model data only; the page and its JavaScript must be available offline themselves, for instance through a service worker or an app that packages them.

Errors#

In offline mode, addModels and getOfflineVersions throw ModelNotAvailableOfflineError (exported by @bimworks/viewer3d) when a version was not prepared, before anything is loaded. Its vids lists the missing versions, all of them at once.

Storage#

The first prepare asks the browser for persistent storage (navigator.storage.persist()); result.persistentStorage says whether it was granted. Without it the browser may delete the data when it runs short of space, so check getOfflineModels() before relying on a model. The data lives in the IndexedDB database bimworks-viewer3d-offline-v1 of your page's origin, separate from the viewer's online cache.

Keep it up to date#

The stored data is a copy of one version as it was when you prepared it. A new version of the model is a new Vid to prepare. After the server is updated and its models are reindexed, the stored copies still load and render, but they no longer match what the server has: prepare them again after an update, which replaces them.

Check#

After prepare(), getOfflineModels() lists the versions with their sizes. With the network switched off in the browser's developer tools, openOffline() loads them and the network panel shows no requests to the server.