Skip to content

Commit

Permalink
Fix dynamic import of atmosphere
Browse files Browse the repository at this point in the history
Fixes #2867
  • Loading branch information
krissvaa committed Nov 13, 2024
1 parent dfca9e8 commit 784cefe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
30 changes: 10 additions & 20 deletions packages/ts/frontend/src/FluxConnection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { ReactiveControllerHost } from '@lit/reactive-element';

import type Atmosphere from 'atmosphere.js';

import type { Subscription } from './Connect.js';
import { getCsrfTokenHeadersForEndpointRequest } from './CsrfUtils.js';
import {
Expand Down Expand Up @@ -75,14 +73,6 @@ type EndpointInfo = {
reconnect?(): ActionOnLostSubscription | void;
};

interface ImportMetaEnv {
readonly SW_CONTEXT: boolean;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}

/**
* A representation of the underlying persistent network connection used for subscribing to Flux type endpoint methods.
*/
Expand All @@ -101,16 +91,16 @@ export class FluxConnection extends EventTarget {

constructor(connectPrefix: string, atmosphereOptions?: Partial<Atmosphere.Request>) {
super();
// @ts-expect-error - vite environment variable
const meta: ImportMeta = import.meta;
if (!meta.env.SW_CONTEXT) {
(async () => {
atmosphere = await import('atmosphere.js');
this.#connectWebsocket(connectPrefix.replace(/connect$/u, ''), atmosphereOptions ?? {});
})().catch((e) => {
// eslint-disable-next-line no-console
console.error('Failed to load atmosphere.js', e);
});
if (!import.meta.env['VITE_SW_CONTEXT']) {
import('atmosphere.js')
.then((module) => {
atmosphere = module.default;
this.#connectWebsocket(connectPrefix.replace(/connect$/u, ''), atmosphereOptions ?? {});
})
.catch((error) => {
// eslint-disable-next-line no-console
console.error('Failed to load atmosphere', error);
});
}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/ts/frontend/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// / <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_SW_CONTEXT: boolean;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}

export {};

0 comments on commit 784cefe

Please sign in to comment.