worktop: Edge-Native Web Framework

0.7.3 · active · verified Sun Apr 19

worktop is a lightweight, high-performance web framework designed for edge computing environments. While originally focused on Cloudflare Workers, versions `0.8.0-next.x` are evolving towards platform agnosticism, supporting Cloudflare Workers, Service Workers (web), and Deno, with future plans for Node.js. The current stable release is `0.7.3`, but active development is ongoing in the `0.8.0-next` series, which introduces significant breaking changes and module restructuring to support its multi-runtime strategy. It differentiates itself by providing a minimal core API that adheres to Web Standards, offering a highly efficient router and specialized submodules for platform-specific features like KV storage and WebSockets, ensuring optimal performance in serverless and edge environments.

Common errors

Warnings

Install

Imports

Quickstart

This example sets up a basic `worktop` router with a dynamic GET endpoint and a catch-all route for Cloudflare Workers, demonstrating parameter extraction and static asset serving for Pages.

import { Router, listen } from 'worktop';

const API = new Router();

// A basic GET endpoint
API.add('GET', '/hello/:name', (request, context) => {
  const name = context.params.name ?? 'World';
  return new Response(`Hello, ${name}!`);
});

// A catch-all route for other paths or methods, serving static assets for Cloudflare Workers Pages
API.add('GET', '/*', (request, context) => {
  // For Cloudflare Workers Pages, `env.ASSETS.fetch` is used to serve static files.
  // This assumes 'ASSETS' is bound in your wrangler.toml file.
  // If not using Pages or static assets, this route would typically return a 404.
  const url = new URL(request.url);
  if (url.pathname.startsWith('/api')) {
    // If the request was for an API route not matched above, return a 404
    return new Response('Not Found', { status: 404 });
  }
  // Otherwise, attempt to serve a static asset if ASSETS binding exists
  // This pattern is common when combining Worker API with static site hosting.
  if (context.bindings && context.bindings.ASSETS) {
    return context.bindings.ASSETS.fetch(request);
  }
  return new Response('Not Found', { status: 404 });
});

// Listen for incoming requests on the Workers environment
listen(API.handler);

view raw JSON →