Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/egoist/vite-vercel

Adding Vercel middleware support to your Vite project
https://github.com/egoist/vite-vercel

Last synced: 12 days ago
JSON representation

Adding Vercel middleware support to your Vite project

Awesome Lists containing this project

README

        

**💛 You can help the author become a full-time open-source maintainer by [sponsoring him on GitHub](https://github.com/sponsors/egoist).**

---

# vite-vercel

[![npm version](https://badgen.net/npm/v/vite-vercel)](https://npm.im/vite-vercel) [![npm downloads](https://badgen.net/npm/dm/vite-vercel)](https://npm.im/vite-vercel)

> Adding a middleware powered by [Vercel Edge Function](https://vercel.com/features/edge-functions) to your existing Vite project.

## Install

```bash
npm i vite-vercel -D
```

## Usage

In your `vite.config.ts`:

```ts
import { defineConfig } from "vite"
import vercel from "vite-vercel"

export default defineConfig({
plugins: [
vercel({
middleware: "./middleware.ts",
}),
],
})
```

Creating a `middleware.ts`:

```ts
import { MiddlewareRequest, MiddlewareResponse } from "vite-vercel/server"

export default (req: MiddlewareRequest) => {
const url = new URL(req.url)

if (url.pathname === "/from-middleware") {
return new Response("from middleware")
}

// Rewrite to another URL
if (url.pathname === "/todo") {
return MiddlewareResponse.rewrite(
"https://jsonplaceholder.typicode.com/todos/1",
)
}

// Continue serving `index.html`
return MiddlewareResponse.next()
}
```

## Deploy on Vercel

This plugin uses Vercel's [Build Output API (v3)](https://vercel.com/docs/build-output-api/v3) which requires an Environment Variable named `ENABLE_VC_BUILD` to be set to `1` in order to enable the feature.

## Credits

A lot of code are taken from Next.js since it's basically the same logic, all credits to Next.js authors.

## Sponsors

[![sponsors](https://sponsors-images.egoist.sh/sponsors.svg)](https://github.com/sponsors/egoist)

## License

MIT © [EGOIST](https://github.com/sponsors/egoist)