mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
Fixes a bug where Route Handlers and Server Actions were prevented from
calling `headers()` (and other request APIs) if
- the call happened after the response is finished
- the call *did not happen* inside an after-callback
generally, this happens when you pass a promise to after.
```ts
const longRunning = () => {
// assume this resolves after the response
await new Promise((resolve) => setTimeout(resolve, 100))
// response is done
after(() => {
await headers() // ❌ should be allowed, but throws
})
}
after(longRunning()) // or waitUntil, doesn't matter
```
The logic we used in the check was incorrect and assumed that an
`afterTaskStore` was present, but we can only wrap callbacks in this ALS
- we cannot change the async context of already-running promises.
One noteworthy change is that i had to exit `actionAsyncStorage` when we
do a render after the server action. Otherwise the checks in
`isRequestApiAllowedInCurrentPhase` might believe that they're still in
a server action when they aren't.
27 lines
545 B
TypeScript
27 lines
545 B
TypeScript
import { testApiInPromisePassedToAfter } from '../common'
|
|
import { Suspense } from 'react'
|
|
|
|
export default async function Page({
|
|
searchParams,
|
|
}: {
|
|
searchParams: Promise<{ apiName?: string }>
|
|
}) {
|
|
return (
|
|
<main>
|
|
<Suspense>
|
|
<TestApiInAfter searchParams={searchParams} />
|
|
</Suspense>
|
|
</main>
|
|
)
|
|
}
|
|
|
|
async function TestApiInAfter({
|
|
searchParams,
|
|
}: {
|
|
searchParams: Promise<{ apiName?: string }>
|
|
}) {
|
|
const { apiName } = await searchParams
|
|
testApiInPromisePassedToAfter('render', apiName!)
|
|
return null
|
|
}
|