Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow dynamically import atmosphere.js for SW context #2901

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 3 additions & 2 deletions packages/ts/frontend/src/Connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from './FluxConnection.js';
import type { VaadinWindow } from './types.js';

const $wnd = window as VaadinWindow;
const $wnd = self as VaadinWindow;

$wnd.Vaadin ??= {};
$wnd.Vaadin.registrations ??= [];
Expand Down Expand Up @@ -300,7 +300,8 @@ export class ConnectClient {
throw new TypeError(`2 arguments required, but got only ${arguments.length}`);
}

const csrfHeaders = getCsrfTokenHeadersForEndpointRequest(document);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const csrfHeaders = self.document ? getCsrfTokenHeadersForEndpointRequest(self.document) : {};
const headers: Record<string, string> = {
Accept: 'application/json',
'Content-Type': 'application/json',
Expand Down
3 changes: 2 additions & 1 deletion packages/ts/frontend/src/CookieManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export function calculatePath({ pathname }: URL): string {
}

const CookieManager: Cookies.CookiesStatic = Cookies.withAttributes({
path: calculatePath(new URL(document.baseURI)),
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
path: calculatePath(new URL(self.document?.baseURI ?? '/')),
});

export default CookieManager;
17 changes: 14 additions & 3 deletions packages/ts/frontend/src/FluxConnection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactiveControllerHost } from '@lit/reactive-element';
import atmosphere from 'atmosphere.js';
import type Atmosphere from 'atmosphere.js';
import type { Subscription } from './Connect.js';
import { getCsrfTokenHeadersForEndpointRequest } from './CsrfUtils.js';
import {
Expand Down Expand Up @@ -71,6 +71,16 @@
reconnect?(): ActionOnLostSubscription | void;
};

let atmosphere: Atmosphere.Atmosphere | undefined;

(async () => {
if (!import.meta.env?.VITE_SW_CONTEXT) {
atmosphere = await import('atmosphere.js').then((module) => module.default);
}
})().catch((e) => {
console.error('Failed to load atmosphere.js', e);

Check warning on line 81 in packages/ts/frontend/src/FluxConnection.ts

View check run for this annotation

Codecov / codecov/patch

packages/ts/frontend/src/FluxConnection.ts#L81

Added line #L81 was not covered by tests
});
Lodin marked this conversation as resolved.
Show resolved Hide resolved

/**
* A representation of the underlying persistent network connection used for subscribing to Flux type endpoint methods.
*/
Expand Down Expand Up @@ -182,10 +192,11 @@
}

#connectWebsocket(prefix: string, atmosphereOptions: Partial<Atmosphere.Request>) {
const extraHeaders = getCsrfTokenHeadersForEndpointRequest(document);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const extraHeaders = self.document ? getCsrfTokenHeadersForEndpointRequest(self.document) : {};
const pushUrl = 'HILLA/push';
const url = prefix.length === 0 ? pushUrl : (prefix.endsWith('/') ? prefix : `${prefix}/`) + pushUrl;
this.#socket = atmosphere.subscribe?.({
this.#socket = atmosphere?.subscribe?.({
contentType: 'application/json; charset=UTF-8',
enableProtocol: true,
transport: 'websocket',
Expand Down
10 changes: 10 additions & 0 deletions packages/ts/frontend/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// / <reference types="vite/client" />
Lodin marked this conversation as resolved.
Show resolved Hide resolved

// eslint-disable-next-line import/unambiguous
interface ImportMetaEnv {
readonly VITE_SW_CONTEXT?: boolean;
}

interface ImportMeta {
readonly env?: ImportMetaEnv;
}
2 changes: 1 addition & 1 deletion packages/ts/react-i18n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class I18n {

constructor() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (!(window as any).Vaadin?.featureFlags?.hillaI18n) {
if (!(self as any).Vaadin?.featureFlags?.hillaI18n) {
// Remove when removing feature flag
throw new Error(
`The Hilla I18n API is currently considered experimental and may change in the future. To use it you need to explicitly enable it in Copilot or by adding com.vaadin.experimental.hillaI18n=true to vaadin-featureflags.properties`,
Expand Down
2 changes: 1 addition & 1 deletion packages/ts/react-signals/src/FullStackSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export abstract class DependencyTrackingSignal<T> extends Signal<T> {

protected constructor(value: T | undefined, onFirstSubscribe: () => void, onLastUnsubscribe: () => void) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (!(window as any).Vaadin?.featureFlags?.fullstackSignals) {
if (!(self as any).Vaadin?.featureFlags?.fullstackSignals) {
// Remove when removing feature flag
throw new Error(
`The Hilla Fullstack Signals API is currently considered experimental and may change in the future. To use it you need to explicitly enable it in Copilot or by adding com.vaadin.experimental.fullstackSignals=true to vaadin-featureflags.properties`,
Expand Down
Loading