Skip to content

Commit 2aecfc6

Browse files
committed
fix: make client-side endpoints compile in service worker context
Depends on: vaadin/flow#21298 Connected to: #2791
1 parent fbc83a6 commit 2aecfc6

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

packages/ts/frontend/src/Connect.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ReactiveControllerHost } from '@lit/reactive-element';
2-
import { ConnectionIndicator, ConnectionState } from '@vaadin/common-frontend';
2+
import type * as CommonFrontendModule from '@vaadin/common-frontend';
33
import { getCsrfTokenHeadersForEndpointRequest } from './CsrfUtils.js';
44
import {
55
EndpointError,
@@ -16,6 +16,11 @@ import {
1616
} from './FluxConnection.js';
1717
import type { VaadinGlobal } from './types.js';
1818

19+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
20+
const commonFrontendModule: Partial<typeof CommonFrontendModule> = globalThis.document
21+
? await import('@vaadin/common-frontend')
22+
: {};
23+
1924
const $wnd = globalThis as VaadinGlobal;
2025

2126
$wnd.Vaadin ??= {};
@@ -291,21 +296,24 @@ export class ConnectClient {
291296
this.atmosphereOptions = options.atmosphereOptions;
292297
}
293298

294-
// add connection indicator to DOM
295-
ConnectionIndicator.create();
296-
297-
// Listen to browser online/offline events and update the loading indicator accordingly.
298-
// Note: if Flow.ts is loaded, it instead handles the state transitions.
299-
addEventListener('online', () => {
300-
if (!isFlowLoaded() && $wnd.Vaadin?.connectionState) {
301-
$wnd.Vaadin.connectionState.state = ConnectionState.CONNECTED;
302-
}
303-
});
304-
addEventListener('offline', () => {
305-
if (!isFlowLoaded() && $wnd.Vaadin?.connectionState) {
306-
$wnd.Vaadin.connectionState.state = ConnectionState.CONNECTION_LOST;
307-
}
308-
});
299+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
300+
if (globalThis.document) {
301+
// add connection indicator to DOM
302+
commonFrontendModule.ConnectionIndicator?.create();
303+
304+
// Listen to browser online/offline events and update the loading indicator accordingly.
305+
// Note: if Flow.ts is loaded, it instead handles the state transitions.
306+
addEventListener('online', () => {
307+
if (!isFlowLoaded() && $wnd.Vaadin?.connectionState && commonFrontendModule.ConnectionState) {
308+
$wnd.Vaadin.connectionState.state = commonFrontendModule.ConnectionState.CONNECTED;
309+
}
310+
});
311+
addEventListener('offline', () => {
312+
if (!isFlowLoaded() && $wnd.Vaadin?.connectionState && commonFrontendModule.ConnectionState) {
313+
$wnd.Vaadin.connectionState.state = commonFrontendModule.ConnectionState.CONNECTION_LOST;
314+
}
315+
});
316+
}
309317
}
310318

311319
/**

packages/ts/frontend/src/CookieManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function calculatePath({ pathname }: URL): string {
66

77
const CookieManager: typeof Cookies = Cookies.withAttributes({
88
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
9-
path: calculatePath(new URL(globalThis.document?.baseURI ?? '/')),
9+
path: calculatePath(new URL(globalThis.document?.baseURI ?? globalThis.location?.href ?? 'data:')),
1010
});
1111

1212
export default CookieManager;

scripts/register.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

scripts/utils/injectRegister.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { PackageJson } from 'type-fest';
33
export default function injectRegister(contents: string, { name, version }: PackageJson): string {
44
return contents.replaceAll(
55
/__REGISTER__/gu,
6-
`((feature, vaadinObj = (window.Vaadin ??= {})) => {
6+
`((feature, vaadinObj = (globalThis.Vaadin ??= {})) => {
77
vaadinObj.registrations ??= [];
88
vaadinObj.registrations.push({
99
is: feature ? \`${name}/\${feature}\` : '${name}',

0 commit comments

Comments
 (0)