Onykia Docs

Vite

Vite is the foundation for SvelteKit, Nuxt, Astro, SolidStart, and Vite-based React.

TL;DR

// vite.config.ts
import { defineConfig } from 'vite';
 
const crossOriginIsolation = {
  'Cross-Origin-Opener-Policy': 'same-origin',
  'Cross-Origin-Embedder-Policy': 'require-corp',
};
 
export default defineConfig({
  // exclude from dev-optimizer
  optimizeDeps: { exclude: ['@mudomi/onykia-engine'] },
  worker: { format: 'es' },
  build: { target: 'esnext' },
 
  // cross-origin isolation
  server: { headers: crossOriginIsolation },
  preview: { headers: crossOriginIsolation },
});

Call createWasmFactory() as in Quickstart. For plain Vite, this config plus a self-hosted worker (below) is all that's needed.

SvelteKit / Nuxt: headers on the document

server.headers only covers assets Vite serves directly. Frameworks that serve their own HTML document (SvelteKit, Nuxt, Astro SSR) bypass it, so the document loads without COOP/COEP, crossOriginIsolated is false, and the engine refuses to boot - in dev too.

Set the headers with middleware that runs on every response:

function coiHeaders() {
  const mw = (_req, res, next) => {
    res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
    res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
    next();
  };
  return {
    name: 'onykia-coi-headers',
    configureServer(s) { s.middlewares.use(mw); },
    configurePreviewServer(s) { s.middlewares.use(mw); },
  };
}

Add coiHeaders() to plugins. In production your host must send the same two headers on the document too (adapter / CDN / reverse proxy).

Self-hosting the worker

A Vite production build inlines the worker as a data: URL, which breaks its internal imports (onykia-engine: worker error, zero pages). Dev is fine; the build is not. The fix is to serve the assets yourself:

  1. Copy node_modules/@mudomi/onykia-engine/dist/wasm/ (the whole tree, including snippets/) into a served directory - static/ for SvelteKit, public/ for plain Vite - via a build script.

  2. Point the factory at the stable URLs:

    createWasmFactory({
      wasmUrl: '/onykia/onykia_engine.wasm',
      workerUrl: '/onykia/onykia_worker.js',
    });

Troubleshooting

Console messageCauseFix
requires a cross-origin-isolated host page / SharedArrayBuffer is not definedDocument not isolatedCOOP/COEP on every response, document included (middleware above)
onykia-engine: worker error (build only, dev fine)Worker inlined as data: URLSelf-host the worker + explicit factory URLs
optimized info should be defined / 404 under .vite/deps/Dep-optimizer mangled the asset URLsoptimizeDeps.exclude, then delete node_modules/.vite and restart
Worker fails with an import syntax errorWorker emitted as a classic workerworker: { format: 'es' }

After changing optimizeDeps, delete node_modules/.vite and restart - Vite caches optimizer results and won't re-run on config change alone.

On this page