ข้ามไปยังเนื้อหา
InstaPlay
หัวข้อในคู่มือ

Large games: external code (by agreement)

A regular InstaPlay game ships all of its JavaScript inside the build archive (up to 250 MB) and loads only data from your CDN (Network and large games). Large games — hundreds of megabytes of code, frequent client updates — get an exception: external code. A studio with the right ships only index.html and a small loader in the archive, and the game code comes from its CDN. Client updates need no new build: a new version on the same CDN origins is picked up at once.

This is trust, not a setting: InstaPlay grants the right by agreement, a moderator checks code origins, and we regularly open games with external code like a player and disable the code on violations.

Getting access

  • The external code right is granted by InstaPlay staff (portal owner or admin) after you sign an addendum: you answer for everything your CDN serves, just like for the build contents. Write to support from the studio dashboard: the game, code and art size, your CDN origin.
  • The right covers the whole studio. The studio page in the dashboard shows “External code allowed” with a date, and the game network editor gets a “Code from CDN” group.
  • Without the right the group is locked, and a build or draft with network.script gets 422 network_invalid with code script_not_allowed.
  • The right can be revoked: code of all studio games is then disabled (below).

A tiny archive

The build keeps only what must live on our games domain:

  • index.html — the game page;
  • the loader (loader.js or inline code) — decides which client version to load; with top-level await include it as a module: <script type="module" src="./loader.js"></script> (a classic tag fails with SyntaxError);
  • the SDK tag <script src="/_ip/sdk/v1.js"></script> in index.html. InstaPlay serves the SDK file from the game origin — do not put your own copy in the archive or load the SDK from the CDN. The portal bridge, saves, leaderboards and the player token work as in any game.
  • styles — in the archive or inline as well (below): the browser will not apply CSS from the CDN.

Everything else — engine bundles, Vite chunks, .wasm, art — lives on your CDN. Build limits stay the same (Uploading a build), but a two-file archive uploads and passes checks in seconds.

Manifest and network.script

Code origins are a separate script group in the network field (instaplay.json for the CLI or the network editor in the dashboard). Data and APIs still go to assets and connect:

{
  "network": {
    "script": ["https://static.my-game.example"],
    "assets": ["https://static.my-game.example", "https://art.my-game.example"],
    "connect": ["https://api.my-game.example", "wss://chat.my-game.example"]
  },
  "privacyUrl": "https://my-game.example/privacy"
}

script rules are stricter than usual (address rules):

  • https:// and an exact host only, no wildcards — otherwise script_mask;
  • up to 4 origins;
  • your own domain or a studio subdomain; shared hosting — *.github.io, storage.yandexcloud.net and s3.amazonaws.com buckets, public package CDNs (unpkg.com, cdn.jsdelivr.net, esm.sh) — is not allowed (shared_host): anyone can put code there;
  • the same origin may also be listed in assets — art and code from one CDN.

A new or changed code origin always goes through a submission, even for a trusted studio with CLI auto-publishing: the CLI build is marked script_needs_review, the draft is updated, and publishing waits for a moderator. An already approved set of origins publishes as usual. The moderator checks that the domain is yours: a DNS TXT record or a page on the studio site — agree on the method in the submission note (Moderation).

CSP: what script opens

script origins enter the game Content Security Policy only in script-src and connect-src. Everything else stays the same — including style-src:

Directive Allowed
script-src own origin, inline, eval, wasm-unsafe-eval, blob: and script origins
connect-src own origin, connect, assets and script (fetch, XHR, WebSocket, metadata for import())
worker-src own origin and blob: only — a worker cannot start straight from the CDN
style-src own origin, inline, data: and blob: only — CSS from the CDN is blocked
frame-src 'none' — no iframes inside the game

CSS goes in the archive or inline. With code splitting Vite itself inserts <link rel="stylesheet" href="<CDN>/assets/*.css"> for styles of dynamic chunks — CSP blocks such a file and the UI stays unstyled. Bundle styles into one file in the archive or into index.html (build.cssCodeSplit: false), or load CSS with fetch and insert a <style>.

A game with external code opens at https://<game id>.<games domain>/<hash>-x<revision>/…. Pages under this path are not cached by the portal CDN: disabling the code reaches players within 30 seconds.

Loader and ES modules

A classic script is added with a tag, modules with import(). A loader example (Vite, Pixi):

const CDN = 'https://static.my-game.example'
const manifest = await (await fetch(`${CDN}/client/manifest.json`, { cache: 'no-cache' })).json()
const app = await import(`${CDN}/client/${manifest.version}/main.js`)
await app.start({ sdk: window.InstaPlay })
  • CORS is required for modules: the browser loads ES modules (<script type="module">, import(), static import inside) in CORS mode. The CDN answers Access-Control-Allow-Origin: * (or the games domain origin) for JS, .wasm and the manifest JSON, otherwise the module fails. For a classic <script src> from the CDN add crossorigin="anonymous" to see script errors with their text.
  • Unity: the archive index.html holds the Unity loader and createUnityInstance with dataUrl, frameworkUrl, codeUrl on the CDN. framework.js loads as a script (script), .data and .wasm via fetch (Unity).

Workers and wasm

worker-src is not extended with CDN origins: the browser blocks new Worker('https://static…/worker.js'). Two ways that work:

  • a worker file in the archive: new Worker('/worker.js') (own origin);
  • a blob: wrapper with importScripts of CDN code — for example, a basis/ktx2 transcoder:
const CDN = 'https://static.my-game.example'
const src = `importScripts('${CDN}/client/v42/basis_transcoder.js')`
const worker = new Worker(URL.createObjectURL(new Blob([src], { type: 'text/javascript' })))

WebAssembly — via fetch: WebAssembly.instantiateStreaming(fetch(url)) or instantiate from arrayBuffer(). wasm-unsafe-eval allows compilation; the CDN serves .wasm with Content-Type: application/wasm and CORS.

CDN rules

  • No redirects to another host. If https://static.my-game.example/loader.js answers 302 to https://other-cdn.example/…, the browser blocks the target — it is not in the CSP. The probe sees such a game as unavailable (redirect_blocked). A redirect within the same host is fine.
  • The version goes in the path (/client/v42/main.js): old versions are immutable, open tabs keep playing theirs, and the loader picks up the new one through a no-cache manifest.
  • HTTPS with a valid certificate; the name resolves to a public address.
  • CDN contents are part of the game: moderation rules apply to them too. Code only from your script origins; a third-party script (ads, trackers) is not possible — CSP blocks it and the probe flags it.

The probe: what we check

InstaPlay opens the published game in regular Chrome like a player — a minute after every release and then roughly every 3 hours (± 1 hour). The probe looks at:

  • the game called ready() within 30 seconds (CDN code loaded, the screen is not blank); before ready() the game loads no more than 300 MB — stream heavy art afterwards;
  • scripts from your origins answer without errors; fingerprints (ETag, length, sha256) are kept as an update history;
  • no attempts to load code from unapproved origins, including through redirects;
  • no service worker and no requests to ad networks or trackers.

What happens next:

  • one failure — a recheck in 10 minutes; two in a row — an email to the studio and a signal to our team; if the game fails to load for 6 hours we may hide it from the catalog — it then comes back on its own after two successful checks;
  • code from an unapproved origin confirmed by a second check — a signal to our team; the game external code may be disabled automatically or manually;
  • automatic actions (hiding and disabling) are switched on gradually; while they are off, an InstaPlay staff member decides;
  • the state is shown on the game page in the dashboard: “active”, “fails to load”, “hidden”, “disabled”.

Takedown

External code of a game is disabled when the studio right is revoked, by a moderator decision, after a confirmed probe violation or when the studio is banned. Within 30 seconds the /<hash>-x…/ path starts answering 403 and the game is hidden from the catalog; tabs already open keep playing until reload.

  • The takedown is sticky: any build, draft or submission with network.script gets 409 external_code_suspended.
  • A version without network.script — all code in the archive — brings the game back to the catalog.
  • Only InstaPlay can restore external code (the “Restore” button in our admin) — write to support.
  • Open submissions with CDN code are canceled when the right is revoked — the dashboard shows why.

CSP reports

A game page with external code asks the browser to report blocked loads (report-uri on the game origin). We keep only the directive, the blocked resource origin and an hourly counter — no page URL, IP or player data — for 30 days. Reports from browser extensions are dropped. If many players report code from a foreign origin, we run an extra probe and look at the game manually.

A game from your own website (trusted partners)

A trusted studio can publish a game that lives entirely on its website, like on CrazyGames: the archive is just an index.html with the relay, and the game itself opens in a nested frame from your domain. It is the same external code permission (granted by the InstaPlay owner or an administrator under an addendum); the moderator approves the site origin just like code origins: an exact host and proof that the domain is yours.

How it works:

  • network.frame — up to 4 https origins (exact host, no wildcards, not shared hosting). Only this game's CSP gets frame-src <your site>; every other game keeps frame-src 'none'.
  • /_ip/sdk/relay.js in index.html embeds the site full-page and forwards SDK messages between the site and the portal. The portal still talks only to the game frame.
  • Your site loads the SDK from the game domain and behaves like any game: player, getToken, saves, leaderboard, ads, the pause and audio events, login.

The archive index.html — do not load the SDK on this page, the site loads it:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Battler</title>
  </head>
  <body>
    <script src="/_ip/sdk/relay.js" data-src="https://play.battler.example/?platform=instaplay"></script>
  </body>
</html>

Your own layout around the frame — InstaPlayRelay.mount({ src, frame }) with your <iframe>.

instaplay.json:

"network": {
  "frame": ["https://play.battler.example"],
  "connect": [],
  "assets": []
},
"privacyUrl": "https://battler.example/privacy"

The site's own network (API, WebSocket, CDN) does not go into connect and assets: the game domain CSP applies to the archive index.html, the framed site lives under its own policy.

The site and the platform marker. The relay adds the instaplay_* parameters to the URL: instaplay_origin is the game origin on the portal, instaplay_locale is the language. You put the platform marker (?platform=instaplay) into data-src yourself — the game uses it to pick the InstaPlay provider:

<script>
  var params = new URLSearchParams(location.search)
  if (params.get('platform') === 'instaplay') {
    var origin = params.get('instaplay_origin') || ''
    // Only the InstaPlay games domain: otherwise a crafted link would inject its own script.
    if (/^https:\/\/[0-9a-f-]+\.getinstaplay\.com$/.test(origin)) {
      var s = document.createElement('script')
      s.src = origin + '/_ip/sdk/v1.js'
      s.onload = function () {
        startGame(window.InstaPlay) // init → player → saves → leaderboard → ready
      }
      document.head.appendChild(s)
    }
  }
</script>

What the site needs:

  • Allow framing by the games domain: Content-Security-Policy: frame-ancestors https://*.getinstaplay.com, no X-Frame-Options: DENY or SAMEORIGIN.
  • The game frame sandbox is inherited: scripts, your own origin (localStorage and cookies of your domain), popups, pointer lock; navigating the portal page away is not allowed. The relay delegates fullscreen, autoplay and gamepad to the site frame.
  • The framed site is a third party: Safari and Firefox block third-party cookies, Chrome partitions them by the portal site. Log in through InstaPlay.player.getToken() and your server, keep progress in saves; your own cookies — SameSite=None; Secure; Partitioned.
  • WebSockets and requests go to any of your servers: your policy limits them, not ours.
  • The portal is invisible to the site: window.top is not accessible, direct messages to the portal are dropped — only through the relay.
  • A new site origin needs a submission, even for a trusted studio (script_needs_review); errors are frame_not_allowed, frame_mask, shared_host. The probe (every 3 h) opens the game and waits for ready from the site SDK; takedown works like external code, ≤ 30 s.

Checklist

  1. The right is granted and the addendum signed.
  2. The archive has index.html with the /_ip/sdk/v1.js tag, the loader (as a module) and styles; ready() fires after the code loads, no more than 300 MB before it.
  3. network.script — exact https:// hosts of your CDN; art in assets, API and chat in connect.
  4. CDN: CORS for JS, .wasm and JSON, Content-Type: application/wasm, no redirects to another host, versions in the path.
  5. Workers — as an archive file or blob: with importScripts.
  6. A submission with the new code origin and the domain proof method in the note.