Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
6a91ff2c
Unverified
Commit
6a91ff2c
authored
May 16, 2025
by
Lee Robinson
Committed by
GitHub
May 16, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
docs: streaming responses with pages API routes (#79269)
parent
9003170c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
3 deletions
+24
-3
07-api-routes.mdx
...03-building-your-application/01-routing/07-api-routes.mdx
+24
-3
No files found.
docs/02-pages/03-building-your-application/01-routing/07-api-routes.mdx
View file @
6a91ff2c
...
@@ -416,8 +416,29 @@ The `query` objects are as follows:
...
@@ -416,8 +416,29 @@ The `query` objects are as follows:
-
`pages/api/post/[pid].js`
-
Will
match
`/api/post/1`
,
`/api/post/abc`
,
etc
.
But
not
`/api/post/create`
-
`pages/api/post/[pid].js`
-
Will
match
`/api/post/1`
,
`/api/post/abc`
,
etc
.
But
not
`/api/post/create`
-
`pages/api/post/[...slug].js`
-
Will
match
`/api/post/1/2`
,
`/api/post/a/b/c`
,
etc
.
But
not
`/api/post/create`
,
`/api/post/abc`
-
`pages/api/post/[...slug].js`
-
Will
match
`/api/post/1/2`
,
`/api/post/a/b/c`
,
etc
.
But
not
`/api/post/create`
,
`/api/post/abc`
##
Edge API Rout
es
##
Streaming respons
es
If
you
would
like
to
use
API
Routes
with
the
Edge
Runtime
,
we
recommend
incrementally
adopting
the
App
Router
and
using
[
Route
Handlers
](
/
docs
/
app
/
building
-
your
-
application
/
routing
/
route
-
handlers
)
instead
.
While
the
Pages
Router
does
support
streaming
responses
with
API
Routes
,
we
recommend
incrementally
adopting
the
App
Router
and
using
[
Route
Handlers
](
/
docs
/
app
/
building
-
your
-
application
/
routing
/
route
-
handlers
)
if
you
are
on
Next
.
js
14
+
.
The
Route
Handlers
function
signature
is
isomorphic
,
meaning
you
can
use
the
same
function
for
both
Edge
and
Node
.
js
runtimes
.
Here
's how you can stream a response from an API Route with `writeHead`:
```js filename="pages/api/hello.js"
import { NextApiRequest, NextApiResponse } from '
next
'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
res.writeHead(200, {
'
Content
-
Type
': '
text
/
event
-
stream
',
'
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
()
}
``
`
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment