sdxc
SearchCtrlK

[ The sdxc command ]

Make an initializer

Write the module that builds the one instance of something the app shares, such as a client, a cache or a configured package, with sdxc make initializer.

Last updated 2026-10-19

An initializer builds the one instance of something the app shares: an API client, a cache, a configured package. sdxc make initializer writes one in app/initializers/, beside the router, the dispatcher, the logger and the rest of the template's instances:

sdxc make initializer cache
Wrote app/initializers/cache.ts
Wrote app/initializers/cache.test.ts

Reach for it when a package needs configuring once and importing everywhere. Code that knows nothing about the app is a package; code that reads or writes stored state is a model.

Flags

FlagDefaultWhat it does
--test, --no-testmake.testswrite the test beside it, or skip it
--dry-runprint the files and change nothing

What it writes

A module-level instance named after the file's last segment in camelCase, built once when the module is first imported:

app/initializers/cache.ts
/** The Cache every caller imports. */
export const cache = { name: "cache" };

Nothing is declared or mapped: callers import the instance.

The test

app/initializers/cache.test.ts
import { describe, expect, test } from "vitest";

import { cache } from "~/app/initializers/cache";

describe("cache", () => {
	test("is built once, at import", () => {
		expect(cache).toBeDefined();
	});
});
bun run test app/initializers/cache.test.ts

Fill it in

Replace the placeholder with the instance, configured from env and your app's constants:

app/initializers/cache.ts
import { env } from "cloudflare:workers";

export const cache = createCache({ namespace: env.CACHE, ttl: "1 hour" });

createCache stands for the package you configure. Keep the module's top level to building the object, since it runs in the Worker's global scope. An instance that needs something only a request or an async step has, such as the request's database, or a constructor that reads or fetches, is exported as a function that builds it instead.

To reach the instance as ctx.<name> in handlers, publish it from a middleware in the router's chain, which sdxc make middleware --mount router puts there. Wire the router covers the template's initializers and the context they publish.

Where to go next

Written by Sergio Xalambrí. Follow @sergiodxa for new packages, or sponsor the work.