Unverified Commit 4a3ea6a2 authored by 우혁's avatar 우혁 Committed by GitHub

docs: change API route example file extension to .ts and add JS example with switcher prop (#83044)

## For Maintainers

### What?
Changed the API route example in the documentation to use `.ts` file
extension for the TypeScript example since it uses TypeScript types.
Added a JavaScript example with `.js` extension using the `switcher`
prop to provide both TypeScript and JavaScript variants in the docs.

### Why?
The existing example used a `.js` extension but included
TypeScript-specific type annotations which is inconsistent and could
confuse users.
Providing both TypeScript and JavaScript examples improves clarity and
usability, catering to a broader audience.

### How?
- Updated the filename in the TypeScript example code block to
`pages/api/hello.ts`.
- Added a parallel JavaScript example code block with
`pages/api/hello.js` using the `switcher` property.
- Ensured both examples show equivalent handler logic with and without
type annotations.

---------
Co-authored-by: 's avatarJoseph <joseph.chamochumbi@vercel.com>
parent 94414f78
...@@ -422,7 +422,7 @@ While the Pages Router does support streaming responses with API Routes, we reco ...@@ -422,7 +422,7 @@ While the Pages Router does support streaming responses with API Routes, we reco
Here's how you can stream a response from an API Route with `writeHead`: Here's how you can stream a response from an API Route with `writeHead`:
```js filename="pages/api/hello.js" ```ts filename="pages/api/hello.ts" switcher
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
export default async function handler( export default async function handler(
...@@ -431,7 +431,23 @@ export default async function handler( ...@@ -431,7 +431,23 @@ export default async function handler(
) { ) {
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'text/event-stream', 'Content-Type': 'text/event-stream',
'Cache-Control': "no-store", 'Cache-Control': 'no-store',
})
let i = 0
while (i < 10) {
res.write(`data: ${i}\n\n`)
i++
await new Promise((resolve) => setTimeout(resolve, 1000))
}
res.end()
}
```
```js filename="pages/api/hello.js" switcher
export default async function handler(req, res) {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-store',
}) })
let i = 0 let i = 0
while (i < 10) { while (i < 10) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment