Serving Assets and CORS - iProov/web GitHub Wiki

Target SDK Version: 7.0+

Some customers require requests to be routed through a particular domain, or self-host assets for their own reasons.

By default the SDK serves its runtime assets from cdn.iproov.app. All of those assets also ship inside the npm package, so you can host them yourself instead.

This gives the following options:

Service Method Hostnames Benefits
Service from cdn.iproov.app cdn.iproov.app Configuration-free with no further infrastructure setup required.
Self-hosting with a custom assets_url Any domain you control You maintain full control of asset service, DNS, and CDN configuration.
Serve from your app's own hostname.

Both are supported however you load the SDK, including on pages that set Cross-Origin-Embedder-Policy: require-corp: cdn.iproov.app returns the Cross-Origin-Resource-Policy header that policy needs, and a self-hosted origin must be configured to return it too; see Cross Origin Isolation. Self-hosting is the only option that keeps every request on your own infrastructure.

Either way, load iProovMe.js itself as described next.

Loading the SDK

From 7.0.0, iProovMe.js must reach the browser exactly as published. If a build step changes the file in any way — bundling, minification, transpilation, or a dev server's dependency pre-bundling — every transaction fails.

So do not import "@iproov/web-sdk" in code your bundler processes. Serve the file as-is and load it by URL instead. iProovSupport.js is not affected: import { iProovSupport } from "@iproov/web-sdk/iProovSupport.js" can stay in your bundle.

1. Copy the SDK into a folder served as-is

Copy iProovMe.js — and iproov-assets/ too if you self-host the assets — into a folder that both your dev server and your production build serve without processing:

  • Vite and Next.js: a subfolder of public/.
  • Angular: an assets entry in angular.json, shown in the Angular guide.
  • webpack: copy-webpack-plugin, shown below.

Run the copy before every dev server start and every build, so it follows each SDK upgrade:

scripts/copy-iproov.mjs

import { cpSync, rmSync } from "node:fs"

const sdk = "node_modules/@iproov/web-sdk"
const out = "public/iproov"

rmSync(out, { recursive: true, force: true })
cpSync(`${sdk}/iProovMe.js`, `${out}/iProovMe.js`)
cpSync(`${sdk}/iproov-assets`, `${out}/iproov-assets`, { recursive: true })

package.json

{
  "scripts": {
    "dev": "node scripts/copy-iproov.mjs && vite",
    "build": "node scripts/copy-iproov.mjs && vite build"
  }
}

For Next.js, use next dev and next build in place of vite and vite build. The copy is generated, so add public/iproov/ to .gitignore.

2. Load it by URL

With a module script:

<script type="module" src="/iproov/iProovMe.js"></script>

Or from code:

const sdkUrl = new URL("/iproov/iProovMe.js", window.location.href).href
await import(/* webpackIgnore: true */ /* @vite-ignore */ sdkUrl)
  • Keep the URL in a variable. A bundler tries to resolve a string-literal import(), and a Vite build fails on one; a variable it leaves alone. webpackIgnore keeps webpack from touching the call, and @vite-ignore tells Vite the dynamic import is intended.
  • Make it absolute. Vite's dev server rewrites a root-relative import such as "/iproov/iProovMe.js", then refuses to serve a file from public/ to it.
  • Wait for the element. <iproov-me> is registered once the import resolves, so render it after that, or after customElements.whenDefined("iproov-me").

3. Point assets_url at the copy

If you copied iproov-assets/, set assets_url to the folder that holds it:

<iproov-me token="***YOUR_TOKEN_HERE***" assets_url="https://your.app.example/iproov/"></iproov-me>

If you leave assets_url unset, the rest of the SDK loads from cdn.iproov.app, and only iProovMe.js needs copying: drop the script's iproov-assets line.

TypeScript

Use import type IProovMe from "@iproov/web-sdk". TypeScript removes it at compile time, so it keeps the SDK's type declarations without loading any of its code.

webpack

With copy-webpack-plugin, mark every copied SDK file as already minimized. Otherwise a production build minifies the copied JavaScript, iProovMe.js included:

const CopyPlugin = require("copy-webpack-plugin")

new CopyPlugin({
  patterns: [
    { from: "node_modules/@iproov/web-sdk/iProovMe.js", to: "iproov/", info: { minimized: true } },
    { from: "node_modules/@iproov/web-sdk/iproov-assets", to: "iproov/iproov-assets", info: { minimized: true } },
  ],
})

Then load it with the dynamic import from step 2.

Hosting iProov Web SDK Assets

All assets are contained inside the iproov-assets/ directory. Below is an example webroot directory structure for the domain web.demo.iproov.com. For the purpose of illustration this doesn't assume any specific web server software.

web.demo.iproov.com/         <- an example domain
  iproov-assets/             <- root directory iproov-assets will always be the same
    7.0.0-85946d64/          <- copy this directory as named; do not rename it
      bin/                   <- binary and WASM files, and the face detection model
      chunks/                <- split JavaScript, loaded as the claim progresses
      css/
      fonts/
      html/
      images/
      workers/               <- JS worker files
        facedetection.worker.js
        webm.worker.js

All assets are namespaced by their build, meaning you can host multiple versions of iProov Web without naming or content clashes between versions. Internally, the SDK requests every external asset with a prefix of iproov-assets/<BUILD>/<FILE>, where <BUILD> is the name of the directory shipped inside the package. From 7.0.0 that name is a build identifier rather than the plain version number, so copy the directory across exactly as it is named — do not rename it, and do not construct the path from the version you installed.

To finish illustrating the above example for the given hostname web.demo.iproov.com, self-hosting iProov Web resources would mean the above directory structure was in place, and the assets_url configuration would be as below:

<iproov-me assets_url="https://web.demo.iproov.com/" />

All requests made would be relative to https://web.demo.iproov.com/, and the SDK would automatically know which directory to search in as it always prepends iproov-assets/<BUILD> to the dependency.

Content Types

The Web SDK needs to load WebAssembly files, JavaScript worker files, various Tensorflow support resources, CSS styles, fonts, images and one HTML page to function correctly.

As a consumer of the Web SDK, if you choose to serve these assets via your own web server or reverse proxy, you must ensure the correct Content-Type and CORS headers are set.

File Type Extension Required Content-Type Compressable?
Binary .bin application/octet-stream ✔️
WASM (Binary) .wasm application/wasm ✔️
Stylesheet .css text/css ✔️
OpenType Fonts .eot application/vnd.ms-fontobject ✔️
Web Open Fonts .woff application/font-woff ✔️
Web Open Fonts 2 .woff2 font/woff2 ❌ (already compressed)
TrueType Fonts .ttf application/x-font-truetype ✔️
Fonts and images .svg image/svg+xml ✔️
Images .gif image/gif ❌ (already compressed)
Iframe Bridge page .html text/html ✔️
JavaScript workers .worker.js application/javascript ✔️
JavaScript chunks .js application/javascript ✔️

CORS

With the exception of the transaction metadata endpoint, the static Web SDK assets do not interact with CORS, because only "simple" requests are made, i.e. GET, no credentials, no request payload.

Our recommendation is that you do not set, add, update or remove any CORS headers for any endpoint that the Web SDK directly interacts with, including the metadata endpoint. Any headers from the iProov API, CDN, or your own service location that serves up assets should be transparently passed through. The metadata endpoint that is called before a transaction requires a preflight check, and the platform responds with specific CORS headers to avoid insecure blanket configurations, such as setting the ACAO header to * for all requests.

If your server configuration deviates from this, it may result in misconfiguration that prevents the Web SDK from working properly. It may be the case that there is an overly permissive configuration that applies to both your application's assets as well as any reverse proxy configuration. If you suspect this is the case, please get in touch with us at [email protected] and we can help - we love talking about CORS!

Read more about CORS here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

One header is worth adding rather than passing through: if your own pages set Cross-Origin-Embedder-Policy: require-corp, the origin serving these assets must also return Cross-Origin-Resource-Policy: cross-origin. Some of the SDK's assets are not requested in CORS mode, so CORS headers alone will not satisfy that policy and those assets are blocked. Our CDN returns it; see Cross Origin Isolation.

Compression

If you serve these files yourself, it's possible and highly recommended to switch on GZip compression.

Common errors

Below are some common errors you'd expect to see in the console as a result of misconfiguration:

  • WebAssembly headers are incorrect or content is corrupt: WebAssembly.instantiate(): expected magic word
  • WebAssembly headers are incorrect: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'., falling back to ArrayBuffer instantiation.

If everything is correctly configured, no such errors should occur in the console and you will be able to run an iProov transaction end to end.

⚠️ **GitHub.com Fallback** ⚠️