Skip to content

Commit 2969aed

Browse files
committed
fixup! +OpenNextHandlerOptions +changeset
1 parent 9b17724 commit 2969aed

File tree

7 files changed

+50
-12
lines changed

7 files changed

+50
-12
lines changed

.changeset/pink-papayas-smoke.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
"@opennextjs/aws": minor
3+
---
4+
5+
refactor: `waitUntil` passed around via ALS.
6+
7+
BREAKING CHANGE: `waitUntil` is passed around via ALS to fix #713.
8+
9+
`globalThis.openNextWaitUntil` is no more available, you can access `waitUntil`
10+
on the ALS context: `globalThis.__openNextAls.getStore()`
11+
12+
The `OpenNextHandler` signature has changed: the second parameter was a `StreamCreator`.
13+
It was changed to be of type `OpenNextHandlerOptions` which has both a `streamCreator` key
14+
and a `waitUntil` key.
15+
16+
If you use a custom wrapper, you need to update the call to the handler as follow:
17+
18+
```ts
19+
// before
20+
globalThis.openNextWaitUntil = myWaitUntil;
21+
handler(internalEvent, myStreamCreator);
22+
23+
// after
24+
handler(internalEvent, { streamCreator: myStreamCreator, waitUntil: myWaitUntil });
25+
```

packages/open-next/src/adapters/image-optimization-adapter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { resolveImageLoader } from "../core/resolve.js";
3131
import { debug, error } from "./logger.js";
3232
import { optimizeImage } from "./plugins/image-optimization/image-optimization.js";
3333
import { setNodeEnv } from "./util.js";
34+
import type { OpenNextHandlerOptions } from "types/overrides.js";
3435

3536
setNodeEnv();
3637
const nextDir = path.join(__dirname, ".next");
@@ -59,7 +60,7 @@ export const handler = await createGenericHandler({
5960

6061
export async function defaultHandler(
6162
event: InternalEvent,
62-
options?: { streamCreator?: StreamCreator; waitUntil?: WaitUntil },
63+
options?: OpenNextHandlerOptions,
6364
): Promise<InternalResult> {
6465
// Images are handled via header and query param information.
6566
debug("handler event", event);

packages/open-next/src/adapters/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type {
22
InternalEvent,
33
InternalResult,
44
MiddlewareResult,
5-
WaitUntil,
65
} from "types/open-next";
76
import { runWithOpenNextRequestContext } from "utils/promise";
87

@@ -19,13 +18,14 @@ import routingHandler, {
1918
INTERNAL_HEADER_INITIAL_PATH,
2019
INTERNAL_HEADER_RESOLVED_ROUTES,
2120
} from "../core/routingHandler";
21+
import type { OpenNextHandlerOptions } from "types/overrides";
2222

2323
globalThis.internalFetch = fetch;
2424
globalThis.__openNextAls = new AsyncLocalStorage();
2525

2626
const defaultHandler = async (
2727
internalEvent: InternalEvent,
28-
options?: { waitUntil?: WaitUntil },
28+
options?: OpenNextHandlerOptions,
2929
): Promise<InternalResult | MiddlewareResult> => {
3030
const originResolver = await resolveOriginResolver(
3131
globalThis.openNextConfig.middleware?.originResolver,

packages/open-next/src/core/requestHandler.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import routingHandler, {
2222
MIDDLEWARE_HEADER_PREFIX_LEN,
2323
} from "./routingHandler";
2424
import { requestHandler, setNextjsPrebundledReact } from "./util";
25+
import type { OpenNextHandlerOptions } from "types/overrides";
2526

2627
// This is used to identify requests in the cache
2728
globalThis.__openNextAls = new AsyncLocalStorage();
@@ -30,10 +31,7 @@ patchAsyncStorage();
3031

3132
export async function openNextHandler(
3233
internalEvent: InternalEvent,
33-
options?: {
34-
streamCreator?: StreamCreator;
35-
waitUntil?: WaitUntil;
36-
},
34+
options?: OpenNextHandlerOptions
3735
): Promise<InternalResult> {
3836
const initialHeaders = internalEvent.headers;
3937
// We run everything in the async local storage context so that it is available in the middleware as well as in NextServer

packages/open-next/src/overrides/wrappers/dummy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { InternalEvent, StreamCreator, WaitUntil } from "types/open-next";
2-
import type { Wrapper, WrapperHandler } from "types/overrides";
1+
import type { InternalEvent } from "types/open-next";
2+
import type { OpenNextHandlerOptions, Wrapper, WrapperHandler } from "types/overrides";
33

44
const dummyWrapper: WrapperHandler = async (handler, converter) => {
55
return async (
66
event: InternalEvent,
7-
options?: { streamCreator?: StreamCreator; waitUntil?: WaitUntil },
7+
options?: OpenNextHandlerOptions,
88
) => {
99
return await handler(event, options);
1010
};

packages/open-next/src/types/overrides.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,19 @@ export type Wrapper<
123123
edgeRuntime?: boolean;
124124
};
125125

126+
export type OpenNextHandlerOptions = {
127+
// Create a `Writeable` for streaming responses.
128+
streamCreator?: StreamCreator;
129+
// Extends the liftetime of the runtime after the response is returned.
130+
waitUntil?: WaitUntil;
131+
};
132+
126133
export type OpenNextHandler<
127134
E extends BaseEventOrResult = InternalEvent,
128135
R extends BaseEventOrResult = InternalResult,
129136
> = (
130137
event: E,
131-
options?: { streamCreator?: StreamCreator; waitUntil?: WaitUntil },
138+
options?: OpenNextHandlerOptions,
132139
) => Promise<R>;
133140

134141
export type Converter<

packages/open-next/src/utils/promise.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,18 @@ function provideNextAfterProvider() {
102102
}
103103
}
104104

105+
export type RunWithOpenNextContextOptions = {
106+
// Whether we are in ISR revalidation
107+
isISRRevalidation: boolean;
108+
// Extends the liftetime of the runtime after the response is returned.
109+
waitUntil?: WaitUntil;
110+
};
111+
105112
export function runWithOpenNextRequestContext<T>(
106113
{
107114
isISRRevalidation,
108115
waitUntil,
109-
}: { isISRRevalidation: boolean; waitUntil?: WaitUntil },
116+
}: RunWithOpenNextContextOptions,
110117
fn: () => Promise<T>,
111118
): Promise<T> {
112119
return globalThis.__openNextAls.run(

0 commit comments

Comments
 (0)