Edge / Workers
Run Lumis on Supabase Edge Functions, Cloudflare Workers and Vercel, including Fluid compute and the Edge runtime.
@lumis-sh/lumis runs on edge and worker hosts with the same API as Node and
browsers. Install it with the parser packages for the languages you highlight:
npm install @lumis-sh/lumis @lumis-sh/themes @lumis-sh/wasm-rustTested on Supabase Edge Functions, Cloudflare Workers, and Vercel on both the Node.js runtime with Fluid compute and the Edge runtime.
Supabase Edge Functions run Deno, so import packages with npm: specifiers:
// supabase/functions/highlight/index.ts
import { createHighlighter } from "npm:@lumis-sh/lumis";
import { htmlInline } from "npm:@lumis-sh/lumis/formatters";
import dracula from "npm:@lumis-sh/themes/dracula";
import rust from "npm:@lumis-sh/wasm-rust";
const highlighter = createHighlighter({ languages: [rust] });
Deno.serve(async (req) => {
const { code } = await req.json();
const hl = await highlighter;
const html = hl.highlight(code, htmlInline({ language: rust, theme: dracula }));
return Response.json({ html });
});npx supabase functions deploy highlightReturn the highlighted HTML inside JSON, as above. On a *.supabase.co URL,
Supabase rewrites a text/html response to text/plain, so a browser shows
the markup as text. Serving HTML directly needs a custom domain.
How it works
Cloudflare Workers and Vercel's Edge runtime cannot compile WebAssembly while a
request runs, only modules their build step compiled at deploy time. Under the
workerd export condition, which Wrangler selects, and the edge-light
condition, which Next.js selects for the Edge runtime, Lumis and every
@lumis-sh/wasm-* package import their .wasm files as modules. It follows
that, on those two hosts:
- every language highlighted there, injected ones included, has to come from a package the code imports
- a resolver cannot work, because the parser bytes it fetches could not be compiled
- a parser you supply yourself must be a
WebAssembly.Module, such asimport parser from "./tree-sitter-rust.wasm"on Workers or"./tree-sitter-rust.wasm?module"on Vercel, not bytes or a URL
Supabase Edge Functions, Deno and Vercel's Node.js runtime compile WebAssembly at run time, so none of this applies there.
On edge hosts runtimeKind() returns 'wasm'. Workers stops the clock while a
request runs, so timing a highlight with performance.now() there reports 0.
Other hosts
Another edge host works when it does one of these:
- compiles WebAssembly from bytes at run time, as Deno does; the ordinary entries work there
- imports
.wasmfiles as compiled modules and resolves theworkerdoredge-lightexport condition, as Wrangler and Next.js do
A host that does neither cannot run Lumis.
See JavaScript / TypeScript for the rest of the API.