Packaging a Three.js or Vite game for the web as a zip
Turn a Vite or Three.js project into a zip that runs from any folder: a relative base path, staying under 50 MB, testing in a sandbox, licence and credits.
Most web games end up as a folder of static files: an HTML page, some JavaScript, and the models, textures and sounds it loads. This tutorial turns a Vite project, with or without Three.js, into a zip that runs from any folder, inside a sandboxed iframe, without loading anything from other servers. That is what Open Web Arcade needs for a hosted game, and it also suits itch.io, GitHub Pages or your own site. The commands and code were checked with Vite 8.3 and three.js r187.
1. Use a relative base path
By default Vite assumes the game will be served from the root of a domain, so the built page asks for /assets/index-abc123.js. On a game host the build usually lives in a folder of its own, and on Open Web Arcade it is served from a path like /b/<build id>/. A path that starts with a slash then points at the wrong place and the game loads nothing. Set base to ./ so every path is relative to the page:
import { defineConfig } from "vite";
export default defineConfig({
base: "./",
});After npm run build, the script tag in dist/index.html points to a relative path:
<script type="module" crossorigin src="./assets/index-BA8ZnmyY.js"></script>2. Load assets with relative paths too
Vite rewrites the paths it knows about, but not strings you pass to a loader at run time. There are two safe ways to load a model or a sound. Import it with the ?url suffix, and Vite copies the file into the build and gives you its final address. Or put it in the public folder, which Vite copies to the root of dist as it is, and load it with a path that has no leading slash:
import shipUrl from "./assets/ship.glb?url";
const ship = await loader.loadAsync(shipUrl);
const level = await loader.loadAsync("models/level-1.glb");The second path is resolved against the page, so it works as long as index.html stays at the top of the build.
3. Keep it under 50 MB
Open Web Arcade accepts a zip of up to 50 MB, up to 250 MB unpacked, with at most 3,000 files and no file over 100 MB. Smaller is better for players anyway, since every byte is downloaded before the game starts. Check where the space goes with du -sh dist/* and work on the largest items first.
Textures are usually the biggest part. Halving a texture from 2048 to 1024 pixels on each side keeps a quarter of the pixels. glTF-Transform can resize and recompress the textures inside a model and compress its geometry in one step:
npx @gltf-transform/cli optimize ship.glb ship.small.glb \
--compress meshopt --texture-compress webp --texture-size 1024WebP and AVIF make the download smaller. KTX2 (--texture-compress ktx2) keeps textures compressed in GPU memory as well, which helps on phones, but it needs the ktx tool from KTX-Software installed.
Geometry compressed with meshopt or Draco needs a decoder at load time. Tell GLTFLoader about the ones you use:
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { MeshoptDecoder } from "three/addons/libs/meshopt_decoder.module.js";
const loader = new GLTFLoader();
loader.setMeshoptDecoder(MeshoptDecoder);In r187, DRACOLoader and KTX2Loader find their decoders next to their own module, and Vite copies those files into the build. Import them only if your models need them: in a test build, the two together added about 1.9 MB of decoder files. Many older examples call setDecoderPath with a CDN address. Remove that call, because a sandboxed game cannot reach other servers.
Audio in WAV is uncompressed. MP3 and AAC in an .m4a file play in all current major browsers. Ogg Vorbis and Opus can be smaller at the same quality, but check that every browser you target plays the format you choose. Sound effects rarely need stereo:
ffmpeg -i music.wav -c:a libmp3lame -q:a 4 music.mp3
ffmpeg -i hit.wav -ac 1 -c:a aac -b:a 96k hit.m4aCode is minified by Vite, and source maps are off unless you turned them on. If you did, they count toward the size.
4. Make the game work without a network
A sandboxed game should load everything from its own files. Fonts from a font service, analytics scripts and models fetched from a CDN will be blocked, so copy them into the project. Two more things fail in a sandbox: IndexedDB, and SharedArrayBuffer, which multithreaded WebAssembly builds need. Read and write saves through localStorage, and guard every access so the game still starts if storage is unavailable:
function loadSave() {
try {
return JSON.parse(localStorage.getItem("save") ?? "null");
} catch {
return null;
}
}5. Test in a sandboxed iframe
npx vite preview shows whether the build works, but not whether it survives a sandbox. This small Node script serves dist on one port with the same sandbox and network rules as a game host, and a test page on another port that frames it. Save it next to package.json:
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { extname, join, normalize } from "node:path";
const dist = join(process.cwd(), "dist");
const types = {
".html": "text/html; charset=utf-8", ".js": "text/javascript", ".css": "text/css",
".json": "application/json", ".wasm": "application/wasm", ".png": "image/png",
".jpg": "image/jpeg", ".webp": "image/webp", ".svg": "image/svg+xml",
".ktx2": "image/ktx2", ".glb": "model/gltf-binary", ".gltf": "model/gltf+json",
".mp3": "audio/mpeg", ".m4a": "audio/mp4", ".ogg": "audio/ogg", ".woff2": "font/woff2",
};
const harness = `<!doctype html><meta charset="utf-8"><title>Sandbox test</title>
<style>html,body{margin:0;height:100%;background:#000}iframe{border:0;width:100%;height:100%}</style>
<iframe src="http://localhost:8081/index.html"
sandbox="allow-scripts allow-pointer-lock allow-orientation-lock"
allow="fullscreen; gamepad; autoplay"></iframe>`;
createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(harness);
}).listen(8080, "127.0.0.1");
createServer(async (req, res) => {
let path;
try {
path = normalize(decodeURIComponent(new URL(req.url, "http://localhost").pathname)).replace(/^\/+/, "");
} catch {
return res.writeHead(400).end();
}
const file = join(dist, path === "" || path.endsWith("/") ? join(path, "index.html") : path);
if (!file.startsWith(dist)) return res.writeHead(400).end();
try {
const body = await readFile(file);
res.writeHead(200, {
"Content-Type": types[extname(file)] ?? "application/octet-stream",
"Content-Security-Policy": "sandbox allow-scripts allow-pointer-lock allow-orientation-lock; default-src 'self' data: blob:; script-src 'self' 'unsafe-inline' 'unsafe-eval' 'wasm-unsafe-eval' blob:; style-src 'self' 'unsafe-inline'; connect-src 'self' data: blob:; frame-src 'none'; base-uri 'self'",
"Access-Control-Allow-Origin": "*",
"Cache-Control": "no-store",
});
res.end(body);
} catch {
res.writeHead(404).end("Not found");
}
}).listen(8081, "127.0.0.1");
console.log("Open http://localhost:8080 to play the build in a sandboxed frame.");Run npm run build, then node serve-sandboxed.mjs, and open http://localhost:8080. Play for a while and watch the console for blocked requests and errors. This copy has no save shim, so localStorage throws here, which is a good test of your guard. On Open Web Arcade a small script keeps localStorage saves working for each game.
6. Ship the licence and credits
Put a LICENSE file and a CREDITS.md file in public, so they land at the top of the build. List every asset you did not make: what it is, who made it, where it came from, its licence, and whether you changed it. CC BY assets require credit, and showing the same list on a credits screen in the game is a good habit.
# Credits
Code: MIT, see LICENSE.
- ship.glb: "Small Ship" by Jane Doe, https://example.com/small-ship, CC0 1.0. Recoloured.
- hit.m4a: "Impact 03" by John Roe, https://example.com/impact-03, CC BY 4.0.If you are still picking a licence, what makes a browser game open source explains the main ones.
7. Zip it with index.html at the root
Zip the contents of dist, not the folder around your project, and leave out hidden files such as .DS_Store or .nojekyll:
npm run build
cd dist
zip -r ../my-game.zip . -x '.*' '*/.*'
cd ..
unzip -l my-game.zip | headThe listing should start with index.html and an assets folder. Open Web Arcade also accepts a zip with everything inside one top-level folder, such as dist/index.html, which is what compressing the folder in Finder or Explorer produces.
Checklist
base: "./"in the Vite config, and no asset path that starts with a slash.- Everything the game loads is inside the build.
- The zip is under 50 MB, with
index.htmlat the top. - The game starts in the sandboxed test page and saves through guarded localStorage.
LICENSEandCREDITS.mdare in the zip.
When it all checks out, submit it.