Skip to content

Commit

Permalink
more space
Browse files Browse the repository at this point in the history
  • Loading branch information
remorses committed Jul 28, 2023
1 parent d207c3d commit 37a26d0
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Adding Cloudflare workers on top of your app will have several benefits:
- DDOS protection
- Faster redirects and rewrites

<br>

## Installation

```
Expand All @@ -26,6 +28,8 @@ npm i -D middleflare wrangler

> [wrangler](https://developers.cloudflare.com/workers/wrangler/) is needed to manage the generated Cloudflare worker at `dist/worker.js`
<br>

## Usage

You should create a directory for your Cloudflare worker code, even better if you create a new package in your workspace.
Expand All @@ -48,6 +52,8 @@ You can find your `account_id` in the Cloudflare dashboard.

The `wrangler` cli will use this file to build and deploy your worker.

<br>

## Run locally

The toml config above should be enough to run `wrangler dev` locally and preview your worker:
Expand All @@ -56,6 +62,8 @@ The toml config above should be enough to run `wrangler dev` locally and preview
wrangler dev
```

<br>

## Environment variables

To use environment variables in your middleware you will need to inject them in the build command, here is an example using [Doppler](https://www.doppler.com) to inject them
Expand All @@ -76,6 +84,8 @@ I recommend using a tool like Doppler for your environment variables, if you pre
command = "dotenv -e .env yarn middleflare --middleware ../website/src/middleware.ts --url http://localhost:3000"
```

<br>

## Deployment

To deploy your worker you can change your `wrangler.toml` to add an environment and a custom domain.
Expand Down Expand Up @@ -110,6 +120,8 @@ Then you can deploy your worker with
wrangler publish --env production
```

<br>

## How to not embed environment variables in the generated worker code

By default `middleflare` will embed your environment variables in the generated worker code, this is useful to get something working quickly.
Expand Down Expand Up @@ -144,6 +156,8 @@ You will also need to add the `--use-secrets` argument to the `build` command in
command = "yarn middleflare --use-secrets --middleware ../website/src/middleware.ts --url https://my-deployed-next-app.fly.dev"
```

<br>

## How it works

This cli is a wrapper around esbuild that bundles your middleware file to be used by Cloudflare, some esbuild plugins are used to:
Expand All @@ -159,6 +173,8 @@ The cli will generate the worker at `dist/worker.js`, this worker will convert t
- `NextResponse.redirect` is converted to a simple Response
- `NextResponse.next` is converted to a fetch to the passed `url` option

<br>

## Origin Story

https://twitter.com/__morse/status/1684567607830261761

0 comments on commit 37a26d0

Please sign in to comment.