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
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.
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
-
namestring -
argsWorkerArgs -
opts?ComponentResourceOptions
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
Disable if the worker code should be 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 worker 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"}link?
Type Input<any[]>
Link resources to your worker. This will:
- Handle the credentials needed to access the resources.
- 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?
transform.worker?
Type WorkerScriptArgs | (args: WorkerScriptArgs, opts: ComponentResourceOptions, name: string) => void
Transform the Worker resource.
url?
Type Input<boolean>
Default false
Enable a dedicated endpoint for your Worker.
Properties
nodes
nodes.worker
Type Output<WorkerScript>
The Cloudflare Worker script.
url
Type Output<undefined | string>
The Worker URL if url is enabled.
SDK
Use the SDK in your runtime to interact with your infrastructure.
Links
This is accessible through the Resource object in the SDK.
-
urlundefined|stringThe Worker URL if
urlis enabled.
Bindings
When you link a worker, say WorkerA, to another worker, WorkerB; it automatically creates a service binding between the workers. It allows WorkerA to call WorkerB without going through a publicly-accessible URL.
import { Resource } from "sst";
await Resource.WorkerB.fetch(request);Read more about binding Workers.