Skip to main content
Version: v2

Hosting resources

The camera steps of the Platform Web SDK use WebAssembly (WASM) and worker files to scan documents and cards, and to capture faces. These files aren't bundled with your JavaScript: you serve them as static files, and the SDK loads them at runtime from a resources/ folder.

Serve .wasm files with the application/wasm content type.

Copy the files​

The files come from the packages of the individual scanning SDKs, which the Platform SDK depends on. To copy them, use the platform-web-sdk copy-resources command that comes with the SDK. Run it as part of your build, with your public output directory as the argument; it creates the resources folder inside it:

package.json
{
"scripts": {
"prebuild": "platform-web-sdk copy-resources public"
}
}

The files take about 130 MB in total. If your workflows don't use all camera steps, copy only the ones they use with --only, for example --only document,face:

  • document: document scanning and document verification, about 95 MB
  • card: card scanning, about 30 MB
  • face: face capture, about 5 MB

If a step's files are missing, that step fails when it starts the camera.

Vite​

If you use Vite, you can use the SDK's plugin instead. It copies the files for all camera steps into public/resources every time you start the dev server or a build:

vite.config.ts
import { defineConfig } from 'vite';
import { scanResourcesPlugin } from '@microblink/platform-sdk/vite';

export default defineConfig({
plugins: [...scanResourcesPlugin()],
});

Set the location​

By default, the SDK loads the files from a resources/ folder next to the current page. For a page at https://example.com/app/verify, that's https://example.com/app/resources/.

If your files are elsewhere, set resourcesPath to the URL of the folder that contains the resources folder. For example, to load the files from /resources/ at the root of your site, wherever the page is, set resourcesPath: '/'.

A relative path is resolved against the page's URL. Prefer a path that starts with /, because a path without it depends on the page's route.

Cache the files​

The files change with each SDK release. Unless their path contains the SDK version, cache them with revalidation (for example, with ETag) rather than as immutable.

Same-domain hosting​

If you can, host the files on the same domain as your web app. This avoids all cross-origin security complexities.

Cross-domain hosting​

If you must host the files on a different domain, for example on a CDN, upload the resources folder as it is, and set resourcesPath to the URL of the folder that contains it. For example, if the files are in https://cdn.example.com/sdk/resources/, set resourcesPath: 'https://cdn.example.com/sdk/'.

You should also configure the headers of your app server, and you must configure the headers of the server that hosts the files.

Configure headers on your main application server​

To use the multithreaded (SharedArrayBuffer) builds of the scanning SDKs, your web app should serve its HTML pages with these two headers:

  • Cross-Origin-Opener-Policy: same-origin: isolates your page context from other top-level windows.
  • Cross-Origin-Embedder-Policy: require-corp: requires all embedded cross-origin resources to explicitly opt in through CORS.

These headers help on the same domain too.

[[headers]]
for = "/*"
[headers.values]
Cross-Origin-Opener-Policy = "same-origin"
Cross-Origin-Embedder-Policy = "require-corp"

If you don't set these headers, the scanning SDKs fall back to their single-threaded builds. The camera steps still work, but more slowly.

Configure headers on your resource server (CDN)​

The server that hosts the files must return these headers:

  • Access-Control-Allow-Origin: explicitly allows your web app's domain to fetch the files.
  • Cross-Origin-Resource-Policy: cross-origin: required when your web app uses Cross-Origin-Embedder-Policy: require-corp, so that the browser allows the cross-origin files to load.
[[headers]]
for = "/*"
[headers.values]
"Access-Control-Allow-Origin" = "https://my-app-url.com"
"Cross-Origin-Resource-Policy" = "cross-origin"