Skip to content

Worker

Reference doc for the `sst.cloudflare.Worker` component.

The Worker component lets you create a Cloudflare Worker.

Minimal example

new sst.cloudflare.Worker("MyWorker", {
handler: "src/worker.handler"
});

Link resources to the Worker. This will handle the credentials and allow you to access it in your handler.

const bucket = new sst.aws.Bucket("MyBucket");
new sst.cloudflare.Worker("MyWorker", {
handler: "src/worker.handler",
link: [bucket]
});

You can use the SDK to access the linked resources in your handler.

src/worker.ts
import { Resource } from "sst";
console.log(Resource.MyBucket.name);

Enable URLs

Enable worker URLs to invoke the worker over HTTP.

new sst.cloudflare.Worker("MyWorker", {
handler: "src/worker.handler",
url: true
});

Bundling

Customize how SST uses esbuild to bundle your worker code with the build property.

new sst.cloudflare.Worker("MyWorker", {
handler: "src/worker.handler",
build: {
install: ["pg"]
}
});

Constructor

new Worker(name, args, opts?)

Parameters

WorkerArgs

build?

Type Input<Object>

Configure how your function is bundled.

SST bundles your worker code using esbuild. This tree shakes your code to only include what’s used.

build.banner?

Type Input<string>

Use this to insert a string at the beginning of the generated JS file.

{
build: {
banner: "console.log('Function starting')"
}
}

build.esbuild?

Type Input<BuildOptions>

This allows you to customize esbuild config that is used.

build.loader?

Type Input<Record<string, Loader>>

Configure additional esbuild loaders for other file extensions. This is useful when your code is importing non-JS files like .png, .css, etc.

{
build: {
loader: {
".png": "file"
}
}
}

build.minify?

Type Input<boolean>

Default true

Enable or disable if the worker code is minified when bundled.

{
build: {
minify: false
}
}

domain?

Type Input<string>

Set a custom domain for your Worker. Supports domains hosted on Cloudflare.

{
domain: "domain.com"
}

environment?

Type Input<Record<string, Input<string>>>

Key-value pairs that are set as Worker environment variables.

They can be accessed in your function through env.<key>.

{
environment: {
DEBUG: "true"
}
}

handler

Type Input<string>

Path to the handler file for the worker.

The handler path is relative to the root your repo or the sst.config.ts.

{
handler: "packages/functions/src/worker.ts"
}

Type Input<any[]>

Link resources to your worker. This will:

  1. Handle the credentials needed to access the resources.
  2. Allow you to access it in your site using the SDK.

Takes a list of components to link to the function.

{
link: [bucket, stripeKey]
}

transform?

Type Object

Transform how this component creates its underlying resources.

transform.worker?

Type WorkerScriptArgs | (args: WorkerScriptArgs => void)

Transform the Worker resource.

url?

Type Input<boolean>

Default false

Enable a dedicated endpoint for your Worker.

Properties

nodes

Type Object

The underlying resources this component creates.

nodes.worker

Type Output<WorkerScript>

The Cloudflare Worker script.

url

Type Output<undefined | string>

The Worker URL if url is enabled.

SDK

The following are accessible through the SDK at runtime.

url

Type undefined | string

The Worker URL if url is enabled.