Skip to content

Commit 5c613c4

Browse files
committed
Fix dynamic import of atmosphere
Fixes #2867
1 parent ae8dfd5 commit 5c613c4

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

packages/ts/frontend/src/FluxConnection.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import {
99
type ServerMessage,
1010
} from './FluxMessages.js';
1111

12-
let atmosphere: Atmosphere.Atmosphere;
13-
1412
export enum State {
1513
ACTIVE = 'active',
1614
INACTIVE = 'inactive',
@@ -73,6 +71,15 @@ type EndpointInfo = {
7371
reconnect?(): ActionOnLostSubscription | void;
7472
};
7573

74+
const initAtmosphere = async () => {
75+
if (!import.meta.env.VITE_SW_CONTEXT) {
76+
return await import('atmosphere.js').then((module) => module.default);
77+
}
78+
return undefined;
79+
};
80+
81+
const atmosphere: Atmosphere.Atmosphere | undefined = await initAtmosphere();
82+
7683
/**
7784
* A representation of the underlying persistent network connection used for subscribing to Flux type endpoint methods.
7885
*/
@@ -91,17 +98,7 @@ export class FluxConnection extends EventTarget {
9198

9299
constructor(connectPrefix: string, atmosphereOptions?: Partial<Atmosphere.Request>) {
93100
super();
94-
if (!import.meta.env['VITE_SW_CONTEXT']) {
95-
import('atmosphere.js')
96-
.then((module) => {
97-
atmosphere = module.default;
98-
this.#connectWebsocket(connectPrefix.replace(/connect$/u, ''), atmosphereOptions ?? {});
99-
})
100-
.catch((error) => {
101-
// eslint-disable-next-line no-console
102-
console.error('Failed to load atmosphere', error);
103-
});
104-
}
101+
this.#connectWebsocket(connectPrefix.replace(/connect$/u, ''), atmosphereOptions ?? {});
105102
}
106103

107104
#resubscribeIfWasClosed() {
@@ -198,7 +195,7 @@ export class FluxConnection extends EventTarget {
198195
const extraHeaders = self.document ? getCsrfTokenHeadersForEndpointRequest(self.document) : {};
199196
const pushUrl = 'HILLA/push';
200197
const url = prefix.length === 0 ? pushUrl : (prefix.endsWith('/') ? prefix : `${prefix}/`) + pushUrl;
201-
this.#socket = atmosphere.subscribe?.({
198+
this.#socket = atmosphere?.subscribe?.({
202199
contentType: 'application/json; charset=UTF-8',
203200
enableProtocol: true,
204201
transport: 'websocket',

packages/ts/frontend/src/vite-env.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// eslint-disable-next-line import/unambiguous
44
interface ImportMetaEnv {
5-
readonly VITE_SW_CONTEXT: boolean;
5+
readonly VITE_SW_CONTEXT?: boolean;
66
}
77

88
interface ImportMeta {

0 commit comments

Comments
 (0)