experimental: unstable_after (#65038)
Implements `unstable_after`, which lets the user schedule work to be
executed after the response is finished.
### Implementation notes
- `unstable_after()` is a dynamic function (bypassable only with `export
dynamic = "force-static"`)
- Usable in: server components (including `generateMetadata`), actions,
route handlers, middleware
- It is meant to run its callbacks even if a response didn't complete
successfully (thrown error) or called `notFound()`/`redirect()`
- Currently gated behind a `experimental.after` feature flag, because it
touches many runtime bits (including a React monkeypatch...)
- The state for `unstable_after()` in a given request lives in
`requestAsyncStorage` (added via `RequestAsyncStorageWrapper`)
- the implementation is based around two functions that we inject via
`renderOpts`:
- `waitUntil(promise)` - keep a function invocation alive until a
promise settles. it is provided as a platform primitive in serverless
contexts, and a noop in `next start`
- for serverless (nodejs), Next.js will attempt to get `waitUntil` from
`globalThis[Symbol.for('@next/request-context')].get().waitUntil`. This
should be considered unstable for now. See
`packages/next/src/server/after/wait-until-builtin.ts` for details.
- `onClose(callback)` **[NEW]** - run something when a response is done.
basically `res.on('close', callback)`, but also implemented for Web APIs
- unfortunately, for Web, this requires some potentially expensive
tricks - see `packages/next/src/server/web/web-on-close.ts`
2024-05-20 10:49:53 +02:00
|
|
|
/* eslint-env jest */
|
|
|
|
import { nextTestSetup, isNextDev } from 'e2e-utils'
|
2024-06-20 10:37:32 +02:00
|
|
|
import { assertHasRedbox, getRedboxSource, retry } from 'next-test-utils'
|
experimental: unstable_after (#65038)
Implements `unstable_after`, which lets the user schedule work to be
executed after the response is finished.
### Implementation notes
- `unstable_after()` is a dynamic function (bypassable only with `export
dynamic = "force-static"`)
- Usable in: server components (including `generateMetadata`), actions,
route handlers, middleware
- It is meant to run its callbacks even if a response didn't complete
successfully (thrown error) or called `notFound()`/`redirect()`
- Currently gated behind a `experimental.after` feature flag, because it
touches many runtime bits (including a React monkeypatch...)
- The state for `unstable_after()` in a given request lives in
`requestAsyncStorage` (added via `RequestAsyncStorageWrapper`)
- the implementation is based around two functions that we inject via
`renderOpts`:
- `waitUntil(promise)` - keep a function invocation alive until a
promise settles. it is provided as a platform primitive in serverless
contexts, and a noop in `next start`
- for serverless (nodejs), Next.js will attempt to get `waitUntil` from
`globalThis[Symbol.for('@next/request-context')].get().waitUntil`. This
should be considered unstable for now. See
`packages/next/src/server/after/wait-until-builtin.ts` for details.
- `onClose(callback)` **[NEW]** - run something when a response is done.
basically `res.on('close', callback)`, but also implemented for Web APIs
- unfortunately, for Web, this requires some potentially expensive
tricks - see `packages/next/src/server/web/web-on-close.ts`
2024-05-20 10:49:53 +02:00
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as path from 'path'
|
|
|
|
import * as os from 'os'
|
|
|
|
import * as Log from './utils/log'
|
|
|
|
|
|
|
|
// using unstable_after is a compile-time error in build mode.
|
|
|
|
const _describe = isNextDev ? describe : describe.skip
|
|
|
|
|
|
|
|
_describe('unstable_after() - pages', () => {
|
|
|
|
const logFileDir = fs.mkdtempSync(path.join(os.tmpdir(), 'logs-'))
|
|
|
|
const logFile = path.join(logFileDir, 'logs.jsonl')
|
|
|
|
|
|
|
|
const { next } = nextTestSetup({
|
|
|
|
files: __dirname,
|
|
|
|
env: {
|
|
|
|
PERSISTENT_LOG_FILE: logFile,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
let currentCliOutputIndex = 0
|
|
|
|
beforeEach(() => {
|
|
|
|
currentCliOutputIndex = next.cliOutput.length
|
|
|
|
})
|
|
|
|
|
|
|
|
const getLogs = () => {
|
|
|
|
return Log.readCliLogs(next.cliOutput.slice(currentCliOutputIndex))
|
|
|
|
}
|
|
|
|
|
|
|
|
it('runs in middleware', async () => {
|
|
|
|
const requestId = `${Date.now()}`
|
|
|
|
const res = await next.fetch(
|
|
|
|
`/middleware/redirect-source?requestId=${requestId}`,
|
|
|
|
{
|
|
|
|
redirect: 'follow',
|
|
|
|
headers: {
|
|
|
|
cookie: 'testCookie=testValue',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
await retry(() => {
|
|
|
|
expect(getLogs()).toContainEqual({
|
|
|
|
source: '[middleware] /middleware/redirect-source',
|
|
|
|
requestId,
|
|
|
|
cookies: { testCookie: 'testValue' },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('invalid usages', () => {
|
|
|
|
describe('errors at compile time when used in pages dir', () => {
|
|
|
|
it.each([
|
|
|
|
{
|
|
|
|
title: 'errors when used in getServerSideProps',
|
|
|
|
path: '/pages-dir/invalid-in-gssp',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'errors when used in getStaticProps',
|
|
|
|
path: '/pages-dir/123/invalid-in-gsp',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'errors when used in within a page component',
|
|
|
|
path: '/pages-dir/invalid-in-page',
|
|
|
|
},
|
|
|
|
])('$title', async ({ path }) => {
|
|
|
|
const browser = await next.browser(path)
|
|
|
|
|
2024-06-20 10:37:32 +02:00
|
|
|
await assertHasRedbox(browser)
|
experimental: unstable_after (#65038)
Implements `unstable_after`, which lets the user schedule work to be
executed after the response is finished.
### Implementation notes
- `unstable_after()` is a dynamic function (bypassable only with `export
dynamic = "force-static"`)
- Usable in: server components (including `generateMetadata`), actions,
route handlers, middleware
- It is meant to run its callbacks even if a response didn't complete
successfully (thrown error) or called `notFound()`/`redirect()`
- Currently gated behind a `experimental.after` feature flag, because it
touches many runtime bits (including a React monkeypatch...)
- The state for `unstable_after()` in a given request lives in
`requestAsyncStorage` (added via `RequestAsyncStorageWrapper`)
- the implementation is based around two functions that we inject via
`renderOpts`:
- `waitUntil(promise)` - keep a function invocation alive until a
promise settles. it is provided as a platform primitive in serverless
contexts, and a noop in `next start`
- for serverless (nodejs), Next.js will attempt to get `waitUntil` from
`globalThis[Symbol.for('@next/request-context')].get().waitUntil`. This
should be considered unstable for now. See
`packages/next/src/server/after/wait-until-builtin.ts` for details.
- `onClose(callback)` **[NEW]** - run something when a response is done.
basically `res.on('close', callback)`, but also implemented for Web APIs
- unfortunately, for Web, this requires some potentially expensive
tricks - see `packages/next/src/server/web/web-on-close.ts`
2024-05-20 10:49:53 +02:00
|
|
|
expect(await getRedboxSource(browser)).toMatch(
|
|
|
|
/You're importing a component that needs "?unstable_after"?\. That only works in a Server Component which is not supported in the pages\/ directory\./
|
|
|
|
)
|
|
|
|
expect(getLogs()).toHaveLength(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|