packages/runtime-class/docs/cloudflare-workers.md
See the the cloudflare sample project for a working example.
When using Marko with Cloudflare Workers, make sure that Marko is loaded with a worker export condition. Most bundlers support defining export conditions.
After that point, imported .marko files will export a .stream method that returns a worker compatible ReadableStream. You can then respond with that returned stream:
import Template from "./index.marko";
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
return new Response(Template.stream(), {
headers: {
status: 200,
headers: { "content-type": "text/html;charset=UTF-8" },
},
});
}
For the large portion of Marko's API a bundler is required. The example code above assumes that Marko templates can be loaded in your environment. Marko supports a number of bundlers, take a look through our supported bundlers and pick what works best for you.