Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 39881e6

Browse files
committed
fix(next-js): add webpackIgnore for optional dependencies (#1408)
1 parent c04c41e commit 39881e6

File tree

2 files changed

+9
-44
lines changed

2 files changed

+9
-44
lines changed

packages/rivetkit/src/common/eventsource.ts

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { logger } from "@/client/log";
44
// Global singleton promise that will be reused for subsequent calls
55
let eventSourcePromise: Promise<typeof EventSource> | null = null;
66

7-
/**
8-
* Import `eventsource` from the custom `eventsource` library. We need a custom implemnetation since we need to attach our own custom headers to the request.
9-
**/
107
export async function importEventSource(): Promise<typeof EventSource> {
8+
// IMPORTANT: Import `eventsource` from the custom `eventsource` library. We need a custom implementation
9+
// since we need to attach our own custom headers to the request.
10+
//
11+
// We can't use the browser-provided EventSource since it does not allow providing custom headers.
12+
1113
// Return existing promise if we already started loading
1214
if (eventSourcePromise !== null) {
1315
return eventSourcePromise;
@@ -19,7 +21,8 @@ export async function importEventSource(): Promise<typeof EventSource> {
1921

2022
// Node.js environment
2123
try {
22-
const es = await import("eventsource");
24+
const moduleName = "eventsource";
25+
const es = await import(/* webpackIgnore: true */ moduleName);
2326
_EventSource = es.EventSource;
2427
logger().debug("using eventsource from npm");
2528
} catch (err) {
@@ -39,42 +42,3 @@ export async function importEventSource(): Promise<typeof EventSource> {
3942

4043
return eventSourcePromise;
4144
}
42-
43-
//export async function importEventSource(): Promise<typeof EventSource> {
44-
// // Return existing promise if we already started loading
45-
// if (eventSourcePromise !== null) {
46-
// return eventSourcePromise;
47-
// }
48-
//
49-
// // Create and store the promise
50-
// eventSourcePromise = (async () => {
51-
// let _EventSource: typeof EventSource;
52-
//
53-
// if (typeof EventSource !== "undefined") {
54-
// // Browser environment
55-
// _EventSource = EventSource;
56-
// logger().debug("using native eventsource");
57-
// } else {
58-
// // Node.js environment
59-
// try {
60-
// const es = await import("eventsource");
61-
// _EventSource = es.EventSource;
62-
// logger().debug("using eventsource from npm");
63-
// } catch (err) {
64-
// // EventSource not available
65-
// _EventSource = class MockEventSource {
66-
// constructor() {
67-
// throw new Error(
68-
// 'EventSource support requires installing the "eventsource" peer dependency.',
69-
// );
70-
// }
71-
// } as unknown as typeof EventSource;
72-
// logger().debug("using mock eventsource");
73-
// }
74-
// }
75-
//
76-
// return _EventSource;
77-
// })();
78-
//
79-
// return eventSourcePromise;
80-
//}

packages/rivetkit/src/common/websocket.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export async function importWebSocket(): Promise<typeof WebSocket> {
1919
} else {
2020
// Node.js environment
2121
try {
22-
const ws = await import("ws");
22+
const moduleName = "ws";
23+
const ws = await import(/* webpackIgnore: true */ moduleName);
2324
_WebSocket = ws.default as unknown as typeof WebSocket;
2425
logger().debug("using websocket from npm");
2526
} catch {

0 commit comments

Comments
 (0)