Project template for scaffolding Cloudflare Workers projects.
- Supports multiple CF Workers within the same (mono)repo; using ES modules syntax
- Pre-configured with TypeScript, Babel, Rollup, ESLint, Vitest, Prettier, Wrangler CLI, Miniflare
- Pre-configured with
local
,test
(staging/QA), andprod
(production) environments - Pre-configured with local testing and debugging; loading environment variables from
*.env
files - Web Crypto API usage example for integrating with 3rd party services (Google Cloud, etc.)
- Code snippets and other VSCode settings; CI/CD workflows with GitHub Actions
This project was bootstrapped with Cloudflare Starter Kit. Be sure to join our Discord channel for assistance.
βββ
.github/workflows
β CI/CD workflows powered by GitHub Actions
βββ
.vscode
β VSCode settings including code snippets, recommended extensions etc.
βββ
api
β Cloudflare Worker script for handling API requests
βββ
app
β Web application front-end powered by Vite and React.js
βββ
edge
β Cloudflare Workers script for serving static websites (reverse proxy)
βββ
scripts
β Automation scripts, such as yarn deploy
βββ
package.json
β The list of NPM dependencies and Yarn workspaces
βββ
tsconfig.base.json
β TypeScript configuration shared across packages/workspaces
- TypeScript, Cloudflare Workers, Hono
- Vite, Rollup, Miniflare, Wrangler CLI, ESLint, Prettier, Vitest, Yarn with PnP
- Node.js v18+ with Corepack (
$ corepack enable
) - VS Code editor with recommended extensions
Generate a new repository from this template, clone, install dependencies, open it in VSCode and start hacking:
$ git clone https://github.com/kriasoft/cloudflare-starter-kit.git
$ cd ./cloudflare-starter-kit
$ yarn install
$ yarn start
$ yarn test
Find the worker scripts inside of the ./edge
and ./api
folders.
IMPORTANT: Ensure that VSCode is using the workspace version of TypeScript.
yarn start
- Launches web application onhttp://localhost:5173/
yarn lint
β Validates the code using ESLintyarn tsc
β Validates the code using TypeScript compileryarn test
β Runs unit tests with Vitest, Miniflare, and Supertestyarn build
β Compiles and bundles worker scripts into the./dist
folder(s)yarn deploy
β Deploys the app to Cloudflare Workers / GCF
Find below the minimal boilerplate for creating a new CF Worker script using TypeScript with ESM syntax:
import { Hono } from "hono";
const app = new Hono<Env>();
app.get("/", ({ text }) => {
return text("Hello world!", 200);
});
export default app;
import { expect, test } from "vitest";
import app from "./index.js";
test("GET /", async () => {
const req = new Request(`https://${env.APP_HOSTNAME}/`);
const res = await app.fetch(req, bindings);
const body = await res.text();
expect({ status: res.status, body }).toEqual({
status: 200,
body: "Hello world!",
});
});
name = "example"
main = "index.js"
compatibility_date = "2022-04-18"
account_id = "$CLOUDFLARE_ACCOUNT_ID"
route = "$APP_HOSTNAME/*"
[vars]
APP_ENV = "$APP_ENV"
APP_HOSTNAME = "$APP_HOSTNAME"
[[rules]]
type = "ESModule"
globs = ["**/*.js"]
Plus package.json
, tsconfig.json
,
and global.d.ts
files configuring TypeScript for the workspace.
Note that $APP_HOSTNAME
and $CLOUDFLARE_ACCOUNT_ID
placeholders in the
example above will be automatically replaced with values from *.env
files for the target environment during local testing or deployment.
For more sophisticated examples visit Cloudflare Workers Examples directory.
The deployments are handled automatically by GitHub Actions
(see .github/workflows
) whenever a new commit lands onto
one of these branches:
main
β Deploys the app tohttps://test.example.com
(test/QA)release
β Deploys the app tohttps://example.com
(production)
Alternatively, you can deploy the app manually by ensuring the all the
required environment variables found in the *.env
files are
up-to-date (e.g. CLOUDFLARE_API_TOKEN
), then running yarn deploy [--env #0]
,
specifying the target deployment area via --env
flag, e.g. --env=test
(default) or --env=prod
.
You can also deploy packages (workspaces) individually, for example:
$ yarn api:deploy --env=prod
$ yarn edge:deploy --env=prod
$ yarn workspace api wrangler tail [--env #0]
$ yarn workspace api wrangler tail [--env #0]
yarn set version stable
β Bump Yarn to the latest versionyarn upgrade-interactive
β Update Node.js modules (dependencies)yarn dlx @yarnpkg/sdks vscode
β Update TypeScript, ESLint, and Prettier settings in VSCode
- React Starter Kit β front-end template for React and Relay using Jamstack architecture
- Node.js API Starter Kit β project template, pre-configured with Node.js, GraphQL, and PostgreSQL
- GraphQL API and Relay Starter Kit β monorepo template, pre-configured with GraphQL API, React, and Relay
Anyone and everyone is welcome to contribute. Start by checking out the list of open issues marked help wanted. However, if you decide to get involved, please take a moment to review the guidelines.
Copyright Β© 2020-present Kriasoft. This source code is licensed under the MIT license found in the LICENSE file.
Made with β₯ by Konstantin Tarkus (@koistya, blog) and contributors.