Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions docs/content/docs/getting-started/nitro.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Nitro

# Nitro

This guide will walk through setting up your first workflow in a Nitro app. Along the way, you'll learn more about the concepts that are fundamental to using the development kit in your own projects.
This guide will walk through setting up your first workflow in a [Nitro v3](https://v3.nitro.build/) project. Along the way, you'll learn more about the concepts that are fundamental to using the development kit in your own projects.

---

Expand All @@ -16,7 +16,7 @@ This guide will walk through setting up your first workflow in a Nitro app. Alon
Start by creating a new Nitro project. This command will create a new directory named `nitro-app` and setup a Nitro project inside it.

```bash
npx giget@latest nitro nitro-app --install
npx create-nitro-app
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

```

Enter the newly made directory:
Expand Down Expand Up @@ -44,16 +44,16 @@ cd nitro-app

### Configure Nitro

Add `workflow/nitro` to your `nitro.config.ts` This enables usage of the `"use workflow"` and `"use step"` directives.
Add `workflow/nitro` module to your `nitro.config.ts` This enables usage of the `"use workflow"` and `"use step"` directives.

```typescript title="nitro.config.ts" lineNumbers
import { defineNitroConfig } from "nitropack/config";
import { defineConfig } from "nitro";

export default defineNitroConfig({
compatibilityDate: "latest",
srcDir: "server",
export default defineConfig({
serverDir: "./server",
modules: ["workflow/nitro"],
});

```

<Accordion type="single" collapsible>
Expand Down Expand Up @@ -175,18 +175,16 @@ To invoke your new workflow, we'll create a new API route handler at `server/api

```typescript title="server/api/signup.post.ts"
import { start } from 'workflow/api';
import { defineEventHandler, readBody } from 'h3';
import { defineEventHandler } from 'nitro/h3';
import { handleUserSignup } from "../../workflows/user-signup";

export default defineEventHandler(async (event) => {
const { email } = await readBody(event);

export default defineEventHandler(async ({ req }) => {
const { email } = await req.json() as { email: string };
// Executes asynchronously and doesn't block your app
await start(handleUserSignup, [email]);

return Response.json({
return {
message: "User signup workflow started",
});
}
});
```

Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class LocalBuilder extends BaseBuilder {
}

export function getWorkflowDirs(nitro: Nitro) {
const srcDir = nitro.options.srcDir || nitro.options.rootDir;
const srcDir = nitro.options.serverDir || nitro.options.rootDir;

return unique(
[
Expand Down
38 changes: 19 additions & 19 deletions packages/nitro/src/vite.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { Nitro } from 'nitro/types';
import type { HotUpdateOptions, Plugin } from 'vite';
import { LocalBuilder } from './builders.js';
import type { ModuleOptions } from './index.js';
import nitroModule from './index.js';
import { workflowRollupPlugin } from './rollup.js';
import type {} from "nitro/vite";
import type { Nitro } from "nitro/types";
import type { HotUpdateOptions, Plugin } from "vite";
import { LocalBuilder } from "./builders.js";
import type { Plugin as VitePlugin } from "vite";
import type { ModuleOptions } from "./index.js";
import nitroModule from "./index.js";
import { workflowRollupPlugin } from "./rollup.js";

export function workflow(options?: ModuleOptions): Plugin[] {
let builder: LocalBuilder | undefined;

return [
workflowRollupPlugin(),
workflowRollupPlugin() as VitePlugin,
{
name: 'workflow:nitro',
// Requires https://github.com/nitrojs/nitro/discussions/3680
// @ts-expect-error
name: "workflow:nitro",
nitro: {
setup: (nitro: Nitro) => {
nitro.options.workflow = {
Expand All @@ -34,21 +34,21 @@ export function workflow(options?: ModuleOptions): Plugin[] {
return () => {
server.middlewares.use((req, res, next) => {
// Only handle workflow webhook routes
if (!req.url?.startsWith('/.well-known/workflow/v1/')) {
if (!req.url?.startsWith("/.well-known/workflow/v1/")) {
return next();
}

// Wrap writeHead to ensure we send empty body for 404s
const originalWriteHead = res.writeHead;
res.writeHead = function (this: typeof res, ...args: any[]) {
const statusCode = typeof args[0] === 'number' ? args[0] : 200;
const statusCode = typeof args[0] === "number" ? args[0] : 200;

// NOTE: Workaround because Nitro passes 404 requests to the vite to handle.
// Causes `webhook route with invalid token` test to fail.
// For 404s on workflow routes, ensure we're sending the right headers
if (statusCode === 404) {
// Set content-length to 0 to prevent Vite from overriding
res.setHeader('Content-Length', '0');
res.setHeader("Content-Length", "0");
}

// @ts-expect-error - Complex overload signature
Expand All @@ -75,14 +75,14 @@ export function workflow(options?: ModuleOptions): Plugin[] {
content = await read();
} catch {
// File might have been deleted - trigger rebuild to update generated routes
console.log('Workflow file deleted, rebuilding...');
console.log("Workflow file deleted, rebuilding...");
if (builder) {
await builder.build();
}
// NOTE: Might be too aggressive
server.ws.send({
type: 'full-reload',
path: '*',
type: "full-reload",
path: "*",
});
return;
}
Expand All @@ -99,13 +99,13 @@ export function workflow(options?: ModuleOptions): Plugin[] {

// Trigger full reload - this will cause Nitro's dev:reload hook to fire,
// which will rebuild workflows and update routes
console.log('Workflow file changed, rebuilding...');
console.log("Workflow file changed, rebuilding...");
if (builder) {
await builder.build();
}
server.ws.send({
type: 'full-reload',
path: '*',
type: "full-reload",
path: "*",
});

// Let Vite handle the normal HMR for the changed file
Expand Down
Loading
Loading