diff --git a/packages/rivetkit/src/common/eventsource.ts b/packages/rivetkit/src/common/eventsource.ts index 07caf7fb4..d3292f19d 100644 --- a/packages/rivetkit/src/common/eventsource.ts +++ b/packages/rivetkit/src/common/eventsource.ts @@ -4,10 +4,12 @@ import { logger } from "@/client/log"; // Global singleton promise that will be reused for subsequent calls let eventSourcePromise: Promise | null = null; -/** - * Import `eventsource` from the custom `eventsource` library. We need a custom implemnetation since we need to attach our own custom headers to the request. - **/ export async function importEventSource(): Promise { + // IMPORTANT: Import `eventsource` from the custom `eventsource` library. We need a custom implementation + // since we need to attach our own custom headers to the request. + // + // We can't use the browser-provided EventSource since it does not allow providing custom headers. + // Return existing promise if we already started loading if (eventSourcePromise !== null) { return eventSourcePromise; @@ -19,7 +21,8 @@ export async function importEventSource(): Promise { // Node.js environment try { - const es = await import("eventsource"); + const moduleName = "eventsource"; + const es = await import(/* webpackIgnore: true */ moduleName); _EventSource = es.EventSource; logger().debug("using eventsource from npm"); } catch (err) { @@ -39,42 +42,3 @@ export async function importEventSource(): Promise { return eventSourcePromise; } - -//export async function importEventSource(): Promise { -// // Return existing promise if we already started loading -// if (eventSourcePromise !== null) { -// return eventSourcePromise; -// } -// -// // Create and store the promise -// eventSourcePromise = (async () => { -// let _EventSource: typeof EventSource; -// -// if (typeof EventSource !== "undefined") { -// // Browser environment -// _EventSource = EventSource; -// logger().debug("using native eventsource"); -// } else { -// // Node.js environment -// try { -// const es = await import("eventsource"); -// _EventSource = es.EventSource; -// logger().debug("using eventsource from npm"); -// } catch (err) { -// // EventSource not available -// _EventSource = class MockEventSource { -// constructor() { -// throw new Error( -// 'EventSource support requires installing the "eventsource" peer dependency.', -// ); -// } -// } as unknown as typeof EventSource; -// logger().debug("using mock eventsource"); -// } -// } -// -// return _EventSource; -// })(); -// -// return eventSourcePromise; -//} diff --git a/packages/rivetkit/src/common/websocket.ts b/packages/rivetkit/src/common/websocket.ts index b44e266a0..a5b4b07f0 100644 --- a/packages/rivetkit/src/common/websocket.ts +++ b/packages/rivetkit/src/common/websocket.ts @@ -19,7 +19,8 @@ export async function importWebSocket(): Promise { } else { // Node.js environment try { - const ws = await import("ws"); + const moduleName = "ws"; + const ws = await import(/* webpackIgnore: true */ moduleName); _WebSocket = ws.default as unknown as typeof WebSocket; logger().debug("using websocket from npm"); } catch { diff --git a/packages/rivetkit/src/remote-manager-driver/mod.ts b/packages/rivetkit/src/remote-manager-driver/mod.ts index a52e75a11..27d9e0226 100644 --- a/packages/rivetkit/src/remote-manager-driver/mod.ts +++ b/packages/rivetkit/src/remote-manager-driver/mod.ts @@ -16,7 +16,7 @@ import type { } from "@/driver-helpers/mod"; import type { Encoding, UniversalWebSocket } from "@/mod"; import { uint8ArrayToBase64 } from "@/serde"; -import { combineUrlPath } from "@/utils"; +import { combineUrlPath, getEnvUniversal } from "@/utils"; import { sendHttpRequestToActor } from "./actor-http-client"; import { buildWebSocketProtocols, @@ -56,6 +56,14 @@ export class RemoteManagerDriver implements ManagerDriver { #metadataPromise: Promise | undefined; constructor(runConfig: ClientConfig) { + // Disable health check if in Next.js build phase since there is no `/metadata` endpoint + // + // See https://github.com/vercel/next.js/blob/5e6b008b561caf2710ab7be63320a3d549474a5b/packages/next/shared/lib/constants.ts#L19-L23 + if (getEnvUniversal("NEXT_PHASE") === "phase-production-build") { + logger().info("detected next.js build phase, disabling health check"); + runConfig.disableHealthCheck = true; + } + this.#config = runConfig; // Perform metadata check if enabled