From 2b215eaab65d6a4d24494a56da76048e28d19616 Mon Sep 17 00:00:00 2001 From: Matthew W Date: Mon, 23 Dec 2024 15:27:44 +1100 Subject: [PATCH] docs(reference/recipes): add next.js example usage snippet --- docs/src/content/docs/reference/recipes.mdx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/src/content/docs/reference/recipes.mdx b/docs/src/content/docs/reference/recipes.mdx index abe2799..95f3097 100644 --- a/docs/src/content/docs/reference/recipes.mdx +++ b/docs/src/content/docs/reference/recipes.mdx @@ -94,6 +94,26 @@ export class SseController { } ``` +### [Next.js](https://nextjs.org/) + +Currently only [API routes](https://nextjs.org/docs/pages/building-your-application/routing/api-routes) under the [Pages Router](https://nextjs.org/docs/pages) is supported. + +```typescript title="pages/api/sse.ts" +import { NextApiRequest, NextApiResponse } from "next"; +import { createSession } from "better-sse"; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + const session = await createSession(req, res); + + session.push("Hello, world!"); +} +``` + +[Issue #79](https://github.com/MatthewWid/better-sse/issues/79) tracks support for the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects which will enable compatibility with [Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) under the [App Router](https://nextjs.org/docs/app). + ### [Fastify](https://fastify.dev/) ```typescript title="server.ts"