Skip to content

Commit f2054c7

Browse files
committed
review change
1 parent bff8e95 commit f2054c7

File tree

8 files changed

+67
-62
lines changed

8 files changed

+67
-62
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export default class S3Cache {
216216
// If some tags are stale we need to force revalidation
217217
return null;
218218
}
219-
const requestId = globalThis.__als.getStore()?.requestId ?? "";
219+
const requestId = globalThis.__openNextAls.getStore()?.requestId ?? "";
220220
globalThis.lastModified[requestId] = _lastModified;
221221
if (cacheData?.type === "route") {
222222
return {
@@ -287,7 +287,7 @@ export default class S3Cache {
287287
}
288288
// This one might not even be necessary anymore
289289
// Better be safe than sorry
290-
const detachedPromise = globalThis.__als
290+
const detachedPromise = globalThis.__openNextAls
291291
.getStore()
292292
?.pendingPromiseRunner.withResolvers<void>();
293293
try {

packages/open-next/src/adapters/edge-adapter.ts

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,65 @@ import {
1212
convertToQueryString,
1313
} from "../core/routing/util";
1414

15-
globalThis.__als = new AsyncLocalStorage();
15+
globalThis.__openNextAls = new AsyncLocalStorage();
1616

1717
const defaultHandler = async (
1818
internalEvent: InternalEvent,
1919
): Promise<InternalResult> => {
2020
globalThis.isEdgeRuntime = true;
2121

2222
// We run everything in the async local storage context so that it is available in edge runtime functions
23-
return runWithOpenNextRequestContext(false, async () => {
24-
const host = internalEvent.headers.host
25-
? `https://${internalEvent.headers.host}`
26-
: "http://localhost:3000";
27-
const initialUrl = new URL(internalEvent.rawPath, host);
28-
initialUrl.search = convertToQueryString(internalEvent.query);
29-
const url = initialUrl.toString();
23+
return runWithOpenNextRequestContext(
24+
{ isISRRevalidation: false },
25+
async () => {
26+
const host = internalEvent.headers.host
27+
? `https://${internalEvent.headers.host}`
28+
: "http://localhost:3000";
29+
const initialUrl = new URL(internalEvent.rawPath, host);
30+
initialUrl.search = convertToQueryString(internalEvent.query);
31+
const url = initialUrl.toString();
3032

31-
// @ts-expect-error - This is bundled
32-
const handler = await import(`./middleware.mjs`);
33+
// @ts-expect-error - This is bundled
34+
const handler = await import(`./middleware.mjs`);
3335

34-
const response: Response = await handler.default({
35-
headers: internalEvent.headers,
36-
method: internalEvent.method || "GET",
37-
nextConfig: {
38-
basePath: NextConfig.basePath,
39-
i18n: NextConfig.i18n,
40-
trailingSlash: NextConfig.trailingSlash,
41-
},
42-
url,
43-
body: convertBodyToReadableStream(
44-
internalEvent.method,
45-
internalEvent.body,
46-
),
47-
});
48-
const responseHeaders: Record<string, string | string[]> = {};
49-
response.headers.forEach((value, key) => {
50-
if (key.toLowerCase() === "set-cookie") {
51-
responseHeaders[key] = responseHeaders[key]
52-
? [...responseHeaders[key], value]
53-
: [value];
54-
} else {
55-
responseHeaders[key] = value;
56-
}
57-
});
36+
const response: Response = await handler.default({
37+
headers: internalEvent.headers,
38+
method: internalEvent.method || "GET",
39+
nextConfig: {
40+
basePath: NextConfig.basePath,
41+
i18n: NextConfig.i18n,
42+
trailingSlash: NextConfig.trailingSlash,
43+
},
44+
url,
45+
body: convertBodyToReadableStream(
46+
internalEvent.method,
47+
internalEvent.body,
48+
),
49+
});
50+
const responseHeaders: Record<string, string | string[]> = {};
51+
response.headers.forEach((value, key) => {
52+
if (key.toLowerCase() === "set-cookie") {
53+
responseHeaders[key] = responseHeaders[key]
54+
? [...responseHeaders[key], value]
55+
: [value];
56+
} else {
57+
responseHeaders[key] = value;
58+
}
59+
});
5860

59-
const body =
60-
(response.body as ReadableStream<Uint8Array>) ?? emptyReadableStream();
61+
const body =
62+
(response.body as ReadableStream<Uint8Array>) ?? emptyReadableStream();
6163

62-
return {
63-
type: "core",
64-
statusCode: response.status,
65-
headers: responseHeaders,
66-
body: body,
67-
// Do we need to handle base64 encoded response?
68-
isBase64Encoded: false,
69-
};
70-
});
64+
return {
65+
type: "core",
66+
statusCode: response.status,
67+
headers: responseHeaders,
68+
body: body,
69+
// Do we need to handle base64 encoded response?
70+
isBase64Encoded: false,
71+
};
72+
},
73+
);
7174
};
7275

7376
export const handler = await createGenericHandler({

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import routingHandler from "../core/routingHandler";
1313

1414
globalThis.internalFetch = fetch;
15-
globalThis.__als = new AsyncLocalStorage();
15+
globalThis.__openNextAls = new AsyncLocalStorage();
1616

1717
const defaultHandler = async (internalEvent: InternalEvent) => {
1818
const originResolver = await resolveOriginResolver(
@@ -35,7 +35,7 @@ const defaultHandler = async (internalEvent: InternalEvent) => {
3535

3636
// We run everything in the async local storage context so that it is available in the external middleware
3737
return runWithOpenNextRequestContext(
38-
internalEvent.headers["x-isr"] === "1",
38+
{ isISRRevalidation: internalEvent.headers["x-isr"] === "1" },
3939
async () => {
4040
const result = await routingHandler(internalEvent);
4141
if ("internalEvent" in result) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import routingHandler from "./routingHandler";
1313
import { requestHandler, setNextjsPrebundledReact } from "./util";
1414

1515
// This is used to identify requests in the cache
16-
globalThis.__als = new AsyncLocalStorage();
16+
globalThis.__openNextAls = new AsyncLocalStorage();
1717

1818
patchAsyncStorage();
1919

@@ -23,7 +23,7 @@ export async function openNextHandler(
2323
): Promise<InternalResult> {
2424
// We run everything in the async local storage context so that it is available in the middleware as well as in NextServer
2525
return runWithOpenNextRequestContext(
26-
internalEvent.headers["x-isr"] === "1",
26+
{ isISRRevalidation: internalEvent.headers["x-isr"] === "1" },
2727
async () => {
2828
if (internalEvent.headers["x-forwarded-host"]) {
2929
internalEvent.headers.host = internalEvent.headers["x-forwarded-host"];
@@ -104,7 +104,7 @@ export async function openNextHandler(
104104
preprocessedEvent,
105105
)
106106
: "middleware";
107-
const store = globalThis.__als.getStore();
107+
const store = globalThis.__openNextAls.getStore();
108108
if (store) {
109109
store.mergeHeadersPriority = mergeHeadersPriority;
110110
}

packages/open-next/src/core/routing/util.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ export function addOpenNextHeader(headers: OutgoingHttpHeaders) {
327327
}
328328
if (globalThis.openNextDebug) {
329329
headers["X-OpenNext-Version"] = globalThis.openNextVersion;
330-
headers["X-OpenNext-RequestId"] = globalThis.__als.getStore()?.requestId;
330+
headers["X-OpenNext-RequestId"] =
331+
globalThis.__openNextAls.getStore()?.requestId;
331332
}
332333
}
333334

@@ -367,7 +368,7 @@ export async function revalidateIfRequired(
367368
try {
368369
const hash = (str: string) =>
369370
crypto.createHash("md5").update(str).digest("hex");
370-
const requestId = globalThis.__als.getStore()?.requestId ?? "";
371+
const requestId = globalThis.__openNextAls.getStore()?.requestId ?? "";
371372

372373
const lastModified =
373374
globalThis.lastModified[requestId] > 0
@@ -443,7 +444,7 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
443444
"private, no-cache, no-store, max-age=0, must-revalidate";
444445
return;
445446
}
446-
const requestId = globalThis.__als.getStore()?.requestId ?? "";
447+
const requestId = globalThis.__openNextAls.getStore()?.requestId ?? "";
447448
const _lastModified = globalThis.lastModified[requestId] ?? 0;
448449
if (headers[CommonHeaders.NEXT_CACHE] === "HIT" && _lastModified > 0) {
449450
// calculate age

packages/open-next/src/http/openNextResponse.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
8989
}
9090
// In some cases we might not have a store i.e. for example in the image optimization function
9191
// We may want to reconsider this in the future, it might be intersting to have access to this store everywhere
92-
globalThis.__als
92+
globalThis.__openNextAls
9393
?.getStore()
9494
?.pendingPromiseRunner.add(onEnd(this.headers));
9595
const bodyLength = this.getBody().length;
@@ -161,7 +161,8 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
161161
// Initial headers should be merged with the new headers
162162
// These initial headers are the one created either in the middleware or in next.config.js
163163
const mergeHeadersPriority =
164-
globalThis.__als?.getStore()?.mergeHeadersPriority ?? "middleware";
164+
globalThis.__openNextAls?.getStore()?.mergeHeadersPriority ??
165+
"middleware";
165166
if (this.initialHeaders) {
166167
this.headers =
167168
mergeHeadersPriority === "middleware"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ declare global {
142142
* TODO: should be available everywhere in the future.
143143
* Defined in `requestHandler.ts`, `middleware.ts` and `edge-adapter.ts`.
144144
*/
145-
var __als: AsyncLocalStorage<OpenNextRequestContext>;
145+
var __openNextAls: AsyncLocalStorage<OpenNextRequestContext>;
146146

147147
/**
148148
* The function that is used to run background tasks even after the response has been sent.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class DetachedPromiseRunner {
6161

6262
async function awaitAllDetachedPromise() {
6363
const promisesToAwait =
64-
globalThis.__als.getStore()?.pendingPromiseRunner.await() ??
64+
globalThis.__openNextAls.getStore()?.pendingPromiseRunner.await() ??
6565
Promise.resolve();
6666
if (globalThis.openNextWaitUntil) {
6767
globalThis.openNextWaitUntil(promisesToAwait);
@@ -78,7 +78,7 @@ function provideNextAfterProvider() {
7878
// Remove this when vercel builder is updated to provide '@next/request-context'.
7979
const VERCEL_REQUEST_CONTEXT_SYMBOL = Symbol.for("@vercel/request-context");
8080

81-
const openNextStoreContext = globalThis.__als.getStore();
81+
const openNextStoreContext = globalThis.__openNextAls.getStore();
8282

8383
const awaiter =
8484
globalThis.openNextWaitUntil ??
@@ -102,10 +102,10 @@ function provideNextAfterProvider() {
102102
}
103103

104104
export function runWithOpenNextRequestContext<T>(
105-
isISRRevalidation = false,
105+
{ isISRRevalidation }: { isISRRevalidation: boolean },
106106
fn: () => Promise<T>,
107107
): Promise<T> {
108-
return globalThis.__als.run(
108+
return globalThis.__openNextAls.run(
109109
{
110110
requestId: Math.random().toString(36),
111111
pendingPromiseRunner: new DetachedPromiseRunner(),

0 commit comments

Comments
 (0)