Skip to content

Commit da67711

Browse files
authored
Fix crash on Vercel (#3311)
1 parent fecdbe8 commit da67711

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

packages/gitbook-v2/src/lib/data/pages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export async function getPageDocument(
4242
}
4343

4444
// Pre-fetch the document to start filling the cache before we migrate to this API.
45-
if (isInPercentRollout(space.id, 10)) {
45+
if (isInPercentRollout(space.id, 10) || process.env.VERCEL_ENV === 'preview') {
4646
await waitUntil(
4747
getDataOrNull(
4848
dataFetcher.getRevisionPageDocument({

packages/gitbook/src/lib/waitUntil.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ExecutionContext, IncomingRequestCfProperties } from '@cloudflare/workers-types';
22
import { getCloudflareContext as getCloudflareContextV2 } from '@v2/lib/data/cloudflare';
3+
import { GITBOOK_RUNTIME } from '@v2/lib/env';
34
import { isV2 } from './v2';
45

56
let pendings: Array<Promise<unknown>> = [];
@@ -49,20 +50,32 @@ export async function waitUntil(promise: Promise<unknown>) {
4950
return;
5051
}
5152

52-
if (isV2()) {
53-
const context = getCloudflareContextV2();
54-
if (context) {
55-
context.ctx.waitUntil(promise);
56-
return;
53+
if (GITBOOK_RUNTIME === 'cloudflare') {
54+
if (isV2()) {
55+
const context = getCloudflareContextV2();
56+
if (context) {
57+
context.ctx.waitUntil(promise);
58+
return;
59+
}
60+
} else {
61+
const cloudflareContext = await getGlobalContext();
62+
if ('waitUntil' in cloudflareContext) {
63+
cloudflareContext.waitUntil(promise);
64+
return;
65+
}
5766
}
5867
}
5968

60-
const cloudflareContext = await getGlobalContext();
61-
if ('waitUntil' in cloudflareContext) {
62-
cloudflareContext.waitUntil(promise);
63-
} else {
64-
await promise;
69+
if (GITBOOK_RUNTIME === 'vercel' && isV2()) {
70+
// @ts-expect-error - `after` is not exported by `next/server` in next 14
71+
const { after } = await import('next/server');
72+
if (typeof after === 'function') {
73+
after(() => promise);
74+
return;
75+
}
6576
}
77+
78+
await promise;
6679
}
6780

6881
/**

0 commit comments

Comments
 (0)