A starter template for Remix + Cloudflare Workers + DO + KV + Turborepo

Overview

Remix + Cloudflare Workers starter with Turborepo ๐Ÿš€

Starter to get going with Remix and Cloudflare Workers.

This template is based on the starter created by Jacob(remix-cloudflare-worker-template).

What's inside? ๐Ÿ‘€

This turborepo uses npm as a package manager. It includes the following packages/apps:

Apps and Packages ๐Ÿ“ฆ

  • counter-do: a Durable Object class for managing the count state
  • remix-app: a Remix app (with Tailwind)
  • worker: a Worker handler function
  • cloudflare-env: types for Cloudflare environement variables
  • eslint-config-custom: eslint configurations (includes @remix-run/eslint-config and eslint-config-prettier)
  • tsconfig: tsconfig.json used throughout the monorepo

Getting Started ๐Ÿช„

Let's setup wrangler CLI instalation (wrangler requires a minimum Node version of 16)

npm i @cloudflare/wrangler -g

After installation, log in to your Cloudflare account from the CLI.

wrangler login

Let's install the dependencies.

npm install

Now we can set up the project.

npm run setup

The setup script will ask for your worker's name. It can be any name. And then, we also need to add the Account ID, which you can find by visiting the Cloudflare Dashboard.

You can add the Worker name and the Account ID later to the `wrangler.toml.

That's all we're ready to push to prod! ๐Ÿš€

Before pushing to the worker via GitHub action, we have to configure the CF_API_TOKEN secret in GitHub. We can generate an API Token from here. When presented with the list of templates to choose from, select the "Edit Cloudflare Workers" option. This template should have the necessary permissions to push a Worker from GitHub. Now we can commit the changes made to wrangler.toml and push the changes.

git commit -am "<message>"
git push

Recap ๐ŸŒ€

npm install @cloudflare/wrangler -g
wrangler login
npm install
npm run setup
# configure CF_API_TOKEN action secret
git add -A -m "<message>"
git push

Durable Objects ๐Ÿ”ฅ

Durable Objects are only available with a Workers paid subscription. However, you can upgrade to the paid plan from the Dashboard.

This starter template comes with a simple DO implementation to keep track of the number of times the root loader is invoked.

If you're starting with DO and not sure what it is, go through the official docs on Durable Objects will be a good start! And checkout using durable objects for more applications of DO.

Defining a Durable Object

This template comes with a script to create the boilerplate for a new Durable Object class.

npm run new:do

The script will have instructions to initialise the DO with the worker. Don't forget to follow them!

More information on DO

You can skip this section if you have used the script to generate the DO class. Continue for more information on DO :)

To define a DO class, check out the docs.

To include the DO class into the worker, we have to add the new DO package as a dependendency of the worker. Then we need to create a binding for the DO in the configuration file wrangler.toml.

[durable_objects]
bindings = [
  {name = "<DO_BINDING_NAME>", class_name = "<DO_CLASS_NAME>"},
]

For development add the following to wrangler.dev.toml

[env.dev.durable_objects]
bindings = [
  {name = "<DO_BINDING_NAME>", class_name = "<DO_CLASS_NAME>"},
]

We must create a migration to register the DO in the configuration file.

[[migrations]]
tag = "v<RUNNING_TAG_ID>"
new_classes = ["<DO_CLASS_NAME>"]

More info about uploading a DO here.

The DO binding will be available in the data functions (loader/action) through the context argument. Types for the context can be added at cloudflare-env. To add a type for a newly created DO, we have to add the following to the .d.ts file

<DO_BINDING_NAME>: DurableObjectNamespace

Now we can access the DO binding from the data function through the context.

export let loader: LoaderFunction = ({ context }) => {
  context.MY_DO
  //        ^ Will have proper type definitions
  return null
}

Deleting a DO

It requires a migration to delete a DO. More info here

Worker KV ๐Ÿ“’

This template does not come with a KV namespace attached to it. However, you can create one using the Wrangler CLI.

wrangler kv:namespace create "MY_KV"

The above command will create a KV namespace. Now we need to bind the created namespace with the worker. When we run the create command, the CLI will print the binding configuration we need to add to our wrangler.toml configuration file. It will look like

kv_namespaces = [
  { binding = "MY_KV", id = "xxxx" }
]

We must add this above the [site] block in the wrangler.toml file.

We have added the KV namespace binding for the production environment, but we also need a namespace for dev. We can do that by creating a new namespace for dev.

wrangler kv:namespace create "MY_KV" --preview

This will generate a namespace for the dev environment, and we must add this below the [env.dev] block in the dev configuration file wrangler.dev.toml.

[env.dev]
kv_namespaces = [
  { binding = "MY_KV", id = "xxxx", preview_id = "xxxx" }
]

Note: We need to add the preview_id key to the configuration file along with the id key with the same value (ref: stackoverflow).

The bounded KV will be available in the loader/action via the context argument passed to the functions. We define types for the context at cloudflare-env. To add types for a newly bounded KV, we have to add the following to the .d.ts file

MY_KV: KVNamespace

Now we can access the KV namespace from the data function through the context.

export let loader: LoaderFunction = ({ context }) => {
  context.MY_KV
  //        ^ Will have proper type definitions
  return null
}

Environment Variables (Secrets) ๐Ÿ”

Adding Worker Environment Variables

You must run wrangler commands from a directory which contains the wrangler.toml file. Either we can cd into the worker directory present at packages/worker, or we can specify the path to the configuration file in the CLI.

To set a worker secret, we can

cd packages/worker
wrangler secret put <SECRET_NAME>

or,

wrangler secret -c packages/worker/wrangler.toml

Like DO/KV binding, the Env variables will be passed to the data functions via the context argument. But we have only configured the production worker with the secret. So let's configure the local environment with the secret.

When we ran npm run setup, the CLI would have created packages/worker/wrangler.dev.toml. And the configuration file should have a vars key under the [env.dev] table. So we can add the new secret there.

[env.dev]
vars = {SECRET_KEY = "<value>"}

One last thing to do is add the type definition for the Env var at config/cloudflare-env/index.d.ts.

SECRET_KEY: string

Now, we can access SESSION_SECRET via context.env.SESSION_SECRET in the data functions inside our Remix app.

Turbo Setup โœจ

This repository is used in the npx create-turbo@latest command and selected when choosing which package manager you wish to use with your monorepo (npm).

Build ๐Ÿ› 

To build all apps and packages, run the following command:

npm run build

Develop ๐Ÿ’ป

To develop all apps and packages, run the following command:

npm run dev

Remote Caching ๐Ÿ’ฝ

Turborepo can use a technique known as Remote Caching (Beta) to share cache artefacts across machines, enabling you to share build caches with your team and CI/CD pipelines.

By default, Turborepo will cache locally. To enable Remote Caching (Beta), you will need an account with Vercel. If you don't have an account, you can create one, then enter the following commands:

npx turbo login

This will authenticate the Turborepo CLI with your Vercel account.

Next, you can link your Turborepo to your Remote Cache by running the following command from the root of your turborepo:

npx turbo link

Useful Links ๐Ÿ”—

Remix + Cloudflare

Turborepo

Learn more about the power of Turborepo:

You might also like...

simple-remix-blog is a blog template built using Remix and TailwindCSS. Create your own blog in just a few minutes!

simple-remix-blog is a blog template built using Remix and TailwindCSS. Create your own blog in just a few minutes!

simple-remix-blog is a blog template built using remix.run and TailwindCSS. It supports markdown and MDX for the blog posts. You can clone it and star

Dec 8, 2022

Blog and Resume template with turborepo

Blog and Resume template with turborepo

Comet-land BLOG DEMO RESUME DEMO Blog and Resume template with turborepo ํ•œ๊ตญ์–ด ๋ฌธ์„œ๋Š” ๋‹ค์Œ ๋งํฌ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Blog Feature ๐Ÿ‘” Code highlight with line-highlig

Dec 27, 2022

ๅŸบไบŽ gh-proxy + Jsdelivr+ cnpmjs + cloudflare workers ็š„ GitHub Serverless API ๅทฅๅ…ทใ€‚

better-github-api Better, Eazy, Access Anywhere ไป‹็ป ๅŸบไบŽ gh-proxy + Jsdelivr + cnpmjs + cloudflare workers ็š„ GitHub Serverless API ๅทฅๅ…ทใ€‚ cdn.js๏ผšไป…ๅซ gh-proxy

Nov 23, 2022

A URL shortener that runs on Cloudflare Workers

ITP Works A URL shortener that runs on Cloudflare Workers. It stores the rules in Cloudflare KV storage and sends a 301 redirect when a matched pathna

Mar 4, 2022

Google-Drive-Directory-Index | Combining the power of Cloudflare Workers and Google Drive API will allow you to index your Google Drive files on the browser.

๐Ÿฟ Google-Drive-Directory-Index Combining the power of Cloudflare Workers and Google Drive will allow you to index your Google Drive files on the brow

Jan 2, 2023

Server-side rendering blog runs on Cloudflare workers

Serverside rendered blog I have tried something completely against to current web trends. What are these? I am using the React as backend framework wi

Jun 24, 2022

A collection of useful tools for building web apps on Cloudflare Workers.

Keywork is a batteries-included, magic-free, library for building web apps on Cloudflare Workers. Features ๐Ÿ’ช Written in TypeScript ๐Ÿ“š Modules Support

Dec 22, 2022

Abusing Cloudflare Workers to establish persistence and exfiltrate sensitive data at the edge.

Abusing Cloudflare Workers This repository contains companion code for the blog post MITM at the Edge: Abusing Cloudflare Workers. malicious-worker/ c

Sep 16, 2022

Lightweight universal Cloudflare API client library for Node.js, Browser, and CF Workers

Cloudflare API Client Lightweight universal HTTP client for Cloudflare API based on Fetch API that works in Node.js, browser, and CF Workers environme

Nov 13, 2022
Comments
  • Github deploy update

    Github deploy update

    Currently, the default branch on github is main, but it remains master in the deploy.yml file. I added a note for deployment in readme. The condition there causes the deploy action not to work.

    opened by mehmetcansahin 1
  • Make KV bindings work in local development

    Make KV bindings work in local development

    If we use wrangler dev with --local flag, wrangler will not use the remote KV data store set in wrangler.dev.toml. Instead, wrangler will use the in-memory KV data store, and all KV data will be gone after the wrangler dev process stops.

    To make KV data persist locally, we need to add --experimental-enable-local-persistence to the wrangler dev command.

    However, even if we use the KV locally, we still need to set the KV bindings inside the wrangler.dev.toml as README suggests with the id and perview_id of the same namespace id of the preview KV namespace (created with the --preview flag) which wil not be touched anyway if we use the --local flag in wrangler dev.

    opened by supachaidev 2
Starting template for building a Remix site with CloudFlare Workers (ES Modules Syntax)

Starting template for building a Remix site with CloudFlare Workers (ES Modules Syntax)

null 12 May 20, 2022
sveltekit + turborepo + histoire in a turborepo

swyx's SvelteKit monorepo starter This is my starter for a monorepo with 2022 tech: SvelteKit Turborepo Histoire pnpm - brew install pnpm Demo Proof t

swyx 36 Nov 25, 2022
Functional-style Cloudflare Durable Objects with direct API calls from Cloudflare Workers and TypeScript support.

durable-apis Simplifies usage of Cloudflare Durable Objects, allowing a functional programming style or class style, lightweight object definitions, a

Dabble 12 Jan 2, 2023
Remix + Cloudflare Workers + Wrangler2 + Tailwind + ESLint + Prettier + Vitest + Playwright

Welcome to Remix! Remix Docs Development You will be running two processes during development: The Miniflare server (miniflare is a local environment

null 48 Dec 19, 2022
Remix TypeScript monorepo with Turborepo pipelines, Prisma, PostgreSQL, Docker deploy to Fly.io, pnpm, TailwindCSS and Tsyringe for DI.

Remix template with Turborepo, TypeScript and pnpm. The remix app deploys to fly.io or build to Docker image. Example packages for Database with prisma, Tsyringe dependency injection, UI, and internal TypeScript packages.

Philippe L'ATTENTION 33 Dec 29, 2022
Grupprojekt fรถr kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet fรถr kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide Fรถr information om hur utv

Svante Jonsson IT-Hรถgskolan 3 May 18, 2022
Hemsida fรถr personer i Sverige som kan och vill erbjuda boende till mรคnniskor pรฅ flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo fรถr kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Fullstack Turborepo starter. Typescript, Nestjs, Nextjs, Tailwind, Prisma, Github Actions, Docker, And Reverse proxy configured

Turborepo (NestJS + Prisma + NextJS + Tailwind + Typescript + Jest) Starter This is fullstack turborepo starter. It comes with the following features.

Ejaz Ahmed 132 Jan 9, 2023
Turborepo starter with pnpm

Turborepo starter with pnpm This is an official starter turborepo. What's inside? This turborepo uses pnpm as a packages manager. It includes the foll

Jack Herrington 23 Dec 18, 2022