Load game assets when they're needed instead of all at once. Asset groups per level or area, priority loading with limited concurrency, prefetching the next level in the background, de-duplicating shared assets, retries, upgrading a prefetch to urgent, unloading old levels while keeping shared assets, loading screens only when necessary, and streaming music. Includes a tested streamer.js. Use f...
Download skill (.zip) Files on GitHubGame asset streaming
Load only what the next screen needs; fetch the rest while the player plays. Drop-in: assets/streamer.js (tested).
import { createStreamer } from "./core/streamer.js";
const assets = createStreamer({ concurrency: 4 }); // image / json / binary / text loaders built in
assets.define("boot", [{ key: "logo", url: "assets/img/logo.png" }, { key: "ui", url: "assets/img/ui-atlas.png" }]);
assets.define("level-1", [{ key: "tiles1", url: "assets/img/tiles-1.png" }, { key: "map1", url: "assets/data/level-01.json" }]);
assets.define("level-2", [{ key: "tiles2", url: "assets/img/tiles-2.png" }, { key: "map2", url: "assets/data/level-02.json" }]);
await assets.loadGroup("boot"); // tiny: title screen appears fast
assets.markInUse("boot");
assets.prefetch("level-1"); // starts in the background while on the title
// Play pressed:
await assets.loadGroup("level-1", { priority: 10, onProgress: (p) => bar.set(p) }); // instant if prefetch finished
assets.prefetch("level-2"); // during level 1
// Level 2 starts:
await assets.loadGroup("level-2"); assets.unloadGroup("level-1"); // free memory; keeps assets shared with in-use groups
Requests are de-duplicated (two groups sharing a file load it once), retried twice, and a pending prefetch is upgraded when something urgent asks for it.
What to load when
| When | What |
|---|---|
| Before the first frame | Only the title screen: logo, UI atlas, font. Aim < 1 MB |
| On the title screen (background) | Level 1, player, common sounds |
| During level N | Level N + 1 |
| On demand, with a short spinner | Rare things: cutscene video, bonus levels, optional music |
| Never in the background on mobile data | Huge optional packs (ask, or wait for wifi via navigator.connection?.saveData) |
Tips
- Music: stream long tracks with
<audio>+MediaElementSourceinstead of decoding whole files into memory. - three.js: load glTF per area;
dispose()geometries, materials and textures on unload; keep shared materials in an in-use group. - Phaser: a
Preloaderscene per level, orthis.load.start()in a running scene for background loads. - Images: call
decode()(the streamer does) so drawing them the first time doesn’t hitch. - Missing files: show a retry message, never a silent black screen.
- Measure with game-loading-optimization’s
load-audit.py: time to title, time to first level, bytes before play.