-
-
Notifications
You must be signed in to change notification settings - Fork 807
feat(vercel): queues #4127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RihanArfan
wants to merge
28
commits into
main
Choose a base branch
from
feat/vercel-queues
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+561
β5
Open
feat(vercel): queues #4127
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
da1808e
feat(vercel): allow overriding function config by route
RihanArfan 24d92dc
fix: factor in base url with function route matching
RihanArfan ba05eb0
fix: replace arrays rather than merge
RihanArfan 2758d9b
docs(vercel): function config overrides
RihanArfan af5775a
Update docs/2.deploy/20.providers/vercel.md
RihanArfan 0b8d4c5
test(vercel): move to main fixture
RihanArfan a904f13
Merge remote-tracking branch 'origin/main' into feat/vercel-per-functβ¦
RihanArfan 88c1972
Merge remote-tracking branch 'origin/main' into feat/vercel-per-functβ¦
RihanArfan ac313e1
feat(vercel): generate consumer for queue triggers
RihanArfan 1dd21d5
fix(vercel): copy function output instead of symlinking when config oβ¦
RihanArfan fba759c
test: update fixture
RihanArfan 90d8bb6
test: copy function output instead of symlinking when config overridden
RihanArfan beb380e
test: copy function output instead of symlinking when config overridden
RihanArfan bbe5bf9
feat(vercel): queues
RihanArfan 54892f1
refactor: use nitro routing
RihanArfan 8c23741
style: move new utils to end
RihanArfan 5355a5b
test: tidy snapshot
RihanArfan 74e10ff
chore: add vercel queues example
RihanArfan 0efd43b
chore: apply automated updates
autofix-ci[bot] b73f763
chore: apply automated updates (attempt 2/3)
autofix-ci[bot] c55c0e2
fix avoid writing extra fn
pi0 1823b2c
Merge branch 'feat/vercel-per-function-config' into feat/vercel-queues
RihanArfan 5fe65f0
chore: rename to functionRules
RihanArfan 1febc69
Merge remote-tracking branch 'origin/main' into feat/vercel-queues
RihanArfan ffba307
chore: add @vercel/queue
RihanArfan 9fd1b1a
Delete examples/vercel-queues/.gitignore
RihanArfan e1ce4f1
chore: move to @vercel/queue dev deps
RihanArfan 1f6cdf2
chore: apply automated updates
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| --- | ||
| category: deploy | ||
| icon: i-simple-icons-vercel | ||
| --- | ||
|
|
||
| # Vercel Queues | ||
|
|
||
| > Process background work asynchronously with Vercel Queues and Nitro tasks. | ||
| <!-- automd:ui-code-tree src="../../examples/vercel-queues" default="nitro.config.ts" ignore="README.md,GUIDE.md" expandAll --> | ||
|
|
||
| ::code-tree{defaultValue="nitro.config.ts" expandAll} | ||
|
|
||
| ```ts [nitro.config.ts] | ||
| import { defineConfig } from "nitro"; | ||
|
|
||
| export default defineConfig({ | ||
| serverDir: ".", | ||
| experimental: { | ||
| tasks: true, | ||
| }, | ||
| vercel: { | ||
| queues: { | ||
| triggers: [{ topic: "notifications" }], | ||
| }, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ```json [package.json] | ||
| { | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite dev", | ||
| "build": "vite build" | ||
| }, | ||
| "devDependencies": { | ||
| "@vercel/queue": "^0.1.4", | ||
| "nitro": "latest" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```json [tsconfig.json] | ||
| { | ||
| "extends": "nitro/tsconfig" | ||
| } | ||
| ``` | ||
|
|
||
| ```ts [vite.config.ts] | ||
| import { defineConfig } from "vite"; | ||
| import { nitro } from "nitro/vite"; | ||
|
|
||
| export default defineConfig({ plugins: [nitro()] }); | ||
| ``` | ||
|
|
||
| ```ts [plugins/queue.ts] | ||
| import { runTask } from "nitro/task"; | ||
| import { definePlugin } from "nitro"; | ||
|
|
||
| export default definePlugin((nitro) => { | ||
| nitro.hooks.hook("vercel:queue", async ({ message, metadata }) => { | ||
| if (metadata.topicName === "notifications") { | ||
| await runTask("notifications:send", { | ||
| payload: message as Record<string, unknown>, | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| ```ts [routes/send.ts] | ||
| import { send } from "@vercel/queue"; | ||
| import { defineHandler, HTTPError } from "nitro"; | ||
|
|
||
| export default defineHandler(async (event) => { | ||
| const body = (await event.req.json()) as Record<string, unknown>; | ||
| if (!body.to || !body.subject || !body.body) { | ||
| throw new HTTPError({ | ||
| status: 400, | ||
| message: "Missing required fields `to`, `subject` or `body`", | ||
| }); | ||
| } | ||
|
|
||
| const { messageId } = await send("notifications", { | ||
| to: body.to, | ||
| subject: body.subject, | ||
| body: body.body, | ||
| }); | ||
| return { messageId }; | ||
| }); | ||
| ``` | ||
|
|
||
| ```ts [tasks/notifications/send.ts] | ||
| import { defineTask } from "nitro/task"; | ||
|
|
||
| export default defineTask({ | ||
| meta: { | ||
| description: "Send a notification", | ||
| }, | ||
| async run({ payload }) { | ||
| console.log(`Sending notification to ${payload.to}: ${payload.subject}`); | ||
| return { result: "Notification sent" }; | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| :: | ||
|
|
||
| <!-- /automd --> | ||
|
|
||
| <!-- automd:file src="../../examples/vercel-queues/README.md" --> | ||
|
|
||
| Nitro integrates with [Vercel Queues](https://vercel.com/docs/queues) to process background work asynchronously. | ||
|
|
||
| ## Configuration | ||
|
|
||
| Add queue triggers to your Nitro config. Nitro registers a queue consumer handler and makes incoming messages available via the `vercel:queue` runtime hook. | ||
|
|
||
| ```ts [nitro.config.ts] | ||
| import { defineConfig } from "nitro"; | ||
|
|
||
| export default defineConfig({ | ||
| experimental: { | ||
| tasks: true, | ||
| }, | ||
| vercel: { | ||
| queues: { | ||
| triggers: [{ topic: "notifications" }], | ||
| }, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Sending Messages | ||
|
|
||
| Use the `@vercel/queue` SDK to send messages to a topic from any route. | ||
|
|
||
| ```ts [routes/send.ts] | ||
| import { send } from "@vercel/queue"; | ||
| import { defineHandler } from "nitro"; | ||
|
|
||
| export default defineHandler(async (event) => { | ||
| const body = await event.req.json(); | ||
| const { messageId } = await send("notifications", { | ||
| to: body.to, | ||
| subject: body.subject, | ||
| body: body.body, | ||
| }); | ||
| return { messageId }; | ||
| }); | ||
| ``` | ||
|
|
||
| ## Processing Messages | ||
|
|
||
| Listen for the `vercel:queue` hook in a plugin to handle incoming messages. This example dispatches them to a Nitro task. | ||
|
|
||
| ```ts [plugins/queue.ts] | ||
| import { runTask } from "nitro/task"; | ||
| import { definePlugin } from "nitro"; | ||
|
|
||
| export default definePlugin((nitro) => { | ||
| nitro.hooks.hook("vercel:queue", async ({ message, metadata }) => { | ||
| if (metadata.topicName === "notifications") { | ||
| await runTask("notifications:send", { | ||
| payload: message as Record<string, unknown>, | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| ```ts [tasks/notifications/send.ts] | ||
| import { defineTask } from "nitro/task"; | ||
|
|
||
| export default defineTask({ | ||
| meta: { | ||
| description: "Send a notification", | ||
| }, | ||
| async run({ payload }) { | ||
| console.log(`Sending notification to ${payload.to}: ${payload.subject}`); | ||
| return { result: "Notification sent" }; | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| <!-- /automd --> | ||
|
|
||
| ## Learn More | ||
|
|
||
| - [Vercel Deployment](/deploy/providers/vercel) | ||
| - [Tasks](/docs/tasks) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| Nitro integrates with [Vercel Queues](https://vercel.com/docs/queues) to process background work asynchronously. | ||
|
|
||
| ## Configuration | ||
|
|
||
| Add queue triggers to your Nitro config. Nitro registers a queue consumer handler and makes incoming messages available via the `vercel:queue` runtime hook. | ||
|
|
||
| ```ts [nitro.config.ts] | ||
| import { defineConfig } from "nitro"; | ||
|
|
||
| export default defineConfig({ | ||
| experimental: { | ||
| tasks: true, | ||
| }, | ||
| vercel: { | ||
| queues: { | ||
| triggers: [{ topic: "notifications" }], | ||
| }, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Sending Messages | ||
|
|
||
| Use the `@vercel/queue` SDK to send messages to a topic from any route. | ||
|
|
||
| ```ts [routes/send.ts] | ||
| import { send } from "@vercel/queue"; | ||
| import { defineHandler } from "nitro"; | ||
|
|
||
| export default defineHandler(async (event) => { | ||
| const body = await event.req.json(); | ||
| const { messageId } = await send("notifications", { | ||
| to: body.to, | ||
| subject: body.subject, | ||
| body: body.body, | ||
| }); | ||
| return { messageId }; | ||
| }); | ||
| ``` | ||
|
|
||
| ## Processing Messages | ||
|
|
||
| Listen for the `vercel:queue` hook in a plugin to handle incoming messages. This example dispatches them to a Nitro task. | ||
|
|
||
| ```ts [plugins/queue.ts] | ||
| import { runTask } from "nitro/task"; | ||
| import { definePlugin } from "nitro"; | ||
|
|
||
| export default definePlugin((nitro) => { | ||
| nitro.hooks.hook("vercel:queue", async ({ message, metadata }) => { | ||
| if (metadata.topicName === "notifications") { | ||
| await runTask("notifications:send", { | ||
| payload: message as Record<string, unknown>, | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| ```ts [tasks/notifications/send.ts] | ||
| import { defineTask } from "nitro/task"; | ||
|
|
||
| export default defineTask({ | ||
| meta: { | ||
| description: "Send a notification", | ||
| }, | ||
| async run({ payload }) { | ||
| console.log(`Sending notification to ${payload.to}: ${payload.subject}`); | ||
| return { result: "Notification sent" }; | ||
| }, | ||
| }); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { defineConfig } from "nitro"; | ||
|
|
||
| export default defineConfig({ | ||
| serverDir: ".", | ||
| experimental: { | ||
| tasks: true, | ||
| }, | ||
| vercel: { | ||
| queues: { | ||
| triggers: [{ topic: "notifications" }], | ||
| }, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite dev", | ||
| "build": "vite build" | ||
| }, | ||
| "devDependencies": { | ||
| "@vercel/queue": "^0.1.4", | ||
| "nitro": "latest" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { runTask } from "nitro/task"; | ||
| import { definePlugin } from "nitro"; | ||
|
|
||
| export default definePlugin((nitro) => { | ||
| nitro.hooks.hook("vercel:queue", async ({ message, metadata }) => { | ||
| if (metadata.topicName === "notifications") { | ||
| await runTask("notifications:send", { | ||
| payload: message as Record<string, unknown>, | ||
| }); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { send } from "@vercel/queue"; | ||
| import { defineHandler, HTTPError } from "nitro"; | ||
|
|
||
| export default defineHandler(async (event) => { | ||
| const body = (await event.req.json()) as Record<string, unknown>; | ||
| if (!body.to || !body.subject || !body.body) { | ||
| throw new HTTPError({ | ||
| status: 400, | ||
| message: "Missing required fields `to`, `subject` or `body`", | ||
| }); | ||
| } | ||
|
|
||
| const { messageId } = await send("notifications", { | ||
| to: body.to, | ||
| subject: body.subject, | ||
| body: body.body, | ||
| }); | ||
| return { messageId }; | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.