Skip to content

Commit

Permalink
noahdarveau/ssr-safe-window (#1902)
Browse files Browse the repository at this point in the history
* replaced window references with ssrSafeWindow, updated blazor sdk version

* Change files

* reverted blazor sdk upgrade

* updated changelog

* Updated Changelog message

Co-authored-by: Trevor Harris <[email protected]>

* removed unnecessary ssr environment check

---------

Co-authored-by: Trevor Harris <[email protected]>
  • Loading branch information
noahdarveau-MSFT and TrevorJoelHarris authored Sep 6, 2023
1 parent 8708855 commit 5b9be17
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Replaced `window` references with `ssrSafeWindow`",
"packageName": "@microsoft/teams-js",
"email": "[email protected]",
"dependentChangeType": "patch"
}
4 changes: 2 additions & 2 deletions packages/teams-js/src/internal/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { GlobalVars } from './globalVars';
import { callHandler } from './handlers';
import { DOMMessageEvent, ExtendedWindow, MessageRequest, MessageResponse } from './interfaces';
import { getLogger } from './telemetry';
import { validateOrigin } from './utils';
import { ssrSafeWindow, validateOrigin } from './utils';

const communicationLogger = getLogger('communication');

Expand Down Expand Up @@ -63,7 +63,7 @@ export function initializeCommunication(validMessageOrigins: string[] | undefine

// If we are in an iframe, our parent window is the one hosting us (i.e., window.parent); otherwise,
// it's the window that opened us (i.e., window.opener)
Communication.currentWindow = Communication.currentWindow || window;
Communication.currentWindow = Communication.currentWindow || ssrSafeWindow();
Communication.parentWindow =
Communication.currentWindow.parent !== Communication.currentWindow.self
? Communication.currentWindow.parent
Expand Down
23 changes: 23 additions & 0 deletions packages/teams-js/src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,26 @@ export function getBase64StringFromBlob(blob: Blob): Promise<string> {
reader.readAsDataURL(blob);
});
}

/**
* Returns an SSR safe reference to the window object
* @returns Window object reference
*/

export function ssrSafeWindow(): Window {
if (!inServerSideRenderingEnvironment()) {
return window;
} else {
// This should NEVER actually be written.
// If you EVER see this error in ANY log file, something has gone horribly wrong and a bug needs to be filed.
throw new Error('window object undefined at SSR check');
}
}

/**
* Checks if running in a Server Side Environment
* @returns True if running in a Server Side Environment
*/
export function inServerSideRenderingEnvironment(): boolean {
return typeof window === 'undefined';
}
6 changes: 3 additions & 3 deletions packages/teams-js/src/internal/videoUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { inServerSideRenderingEnvironment } from '../private/inServerSideRenderingEnvironment';
import { videoEx } from '../private/videoEx';
import { errorNotSupportedOnPlatform } from '../public/constants';
import { video } from '../public/video';
import { sendMessageToParent } from './communication';
import { registerHandler } from './handlers';
import { inServerSideRenderingEnvironment, ssrSafeWindow } from './utils';
import {
AllowSharedBufferSource,
PlaneLayout,
Expand Down Expand Up @@ -113,7 +113,7 @@ async function getInputVideoTrack(
throw errorNotSupportedOnPlatform;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const chrome = window['chrome'] as any;
const chrome = ssrSafeWindow()['chrome'] as any;
try {
videoPerformanceMonitor?.reportGettingTextureStream(streamId);
const mediaStream = await chrome.webview.getTextureStream(streamId);
Expand Down Expand Up @@ -156,7 +156,7 @@ function pipeVideoSourceToGenerator(
transformer: TransformerWithMetadata | DefaultTransformer,
sink: WritableStream,
): void {
const MediaStreamTrackProcessor = window['MediaStreamTrackProcessor'];
const MediaStreamTrackProcessor = ssrSafeWindow()['MediaStreamTrackProcessor'];
const processor = new MediaStreamTrackProcessor({ track: videoTrack });
const source = processor.readable;

Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion packages/teams-js/src/private/videoEx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sendMessageToParent } from '../internal/communication';
import { registerHandler } from '../internal/handlers';
import { ensureInitialized } from '../internal/internalAPIs';
import { inServerSideRenderingEnvironment } from '../internal/utils';
import { VideoPerformanceMonitor } from '../internal/videoPerformanceMonitor';
import {
createEffectParameterChangeCallback,
Expand All @@ -11,7 +12,6 @@ import {
import { errorNotSupportedOnPlatform, FrameContexts } from '../public/constants';
import { runtime } from '../public/runtime';
import { video } from '../public/video';
import { inServerSideRenderingEnvironment } from './inServerSideRenderingEnvironment';

/**
* @hidden
Expand Down
3 changes: 1 addition & 2 deletions packages/teams-js/src/public/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { GlobalVars } from '../internal/globalVars';
import * as Handlers from '../internal/handlers'; // Conflict with some names
import { ensureInitializeCalled, ensureInitialized, processAdditionalValidOrigins } from '../internal/internalAPIs';
import { getLogger } from '../internal/telemetry';
import { compareSDKVersions, runWithTimeout } from '../internal/utils';
import { inServerSideRenderingEnvironment } from '../private/inServerSideRenderingEnvironment';
import { compareSDKVersions, inServerSideRenderingEnvironment, runWithTimeout } from '../internal/utils';
import { logs } from '../private/logs';
import { authentication } from './authentication';
import { ChannelType, FrameContexts, HostClientType, HostName, TeamType, UserTeamRole } from './constants';
Expand Down
3 changes: 2 additions & 1 deletion packages/teams-js/src/public/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { GlobalVars } from '../internal/globalVars';
import { registerHandler, removeHandler } from '../internal/handlers';
import { ensureInitializeCalled, ensureInitialized } from '../internal/internalAPIs';
import { ssrSafeWindow } from '../internal/utils';
import { FrameContexts, HostClientType } from './constants';
import { runtime } from './runtime';

Expand Down Expand Up @@ -471,7 +472,7 @@ export namespace authentication {
link.href = decodeURIComponent(callbackUrl);
if (
link.host &&
link.host !== window.location.host &&
link.host !== ssrSafeWindow().location.host &&
link.host === 'outlook.office.com' &&
link.search.indexOf('client_type=Win32_Outlook') > -1
) {
Expand Down
9 changes: 2 additions & 7 deletions packages/teams-js/src/public/teamsAPIs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GlobalVars } from '../internal/globalVars';
import * as Handlers from '../internal/handlers'; // Conflict with some names
import { ensureInitialized } from '../internal/internalAPIs';
import { ssrSafeWindow } from '../internal/utils';
import { errorNotSupportedOnPlatform } from './constants';
import { LoadContext } from './interfaces';
import { runtime } from './runtime';
Expand Down Expand Up @@ -41,13 +42,7 @@ export namespace teamsCore {
* default print handler
*/
export function print(): void {
if (typeof window !== 'undefined') {
window.print();
} else {
// This codepath only exists to enable compilation in a server-side redered environment. In standard usage, the window object should never be undefined so this code path should never run.
// If this error has actually been thrown, something has gone very wrong and it is a bug
throw new Error('window object undefined at print call');
}
ssrSafeWindow().print();
}

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/teams-js/src/public/video.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { sendMessageToParent } from '../internal/communication';
import { registerHandler } from '../internal/handlers';
import { ensureInitialized } from '../internal/internalAPIs';
import { inServerSideRenderingEnvironment, ssrSafeWindow } from '../internal/utils';
import { VideoPerformanceMonitor } from '../internal/videoPerformanceMonitor';
import { createEffectParameterChangeCallback, processMediaStream } from '../internal/videoUtils';
import { inServerSideRenderingEnvironment } from '../private/inServerSideRenderingEnvironment';
import { errorNotSupportedOnPlatform, FrameContexts } from './constants';
import { runtime } from './runtime';

Expand Down Expand Up @@ -406,9 +406,8 @@ export namespace video {
}

function isTextureStreamAvailable(): boolean {
return (
!inServerSideRenderingEnvironment() &&
!!(window['chrome']?.webview?.getTextureStream && window['chrome']?.webview?.registerTextureStream)
return !!(
ssrSafeWindow()['chrome']?.webview?.getTextureStream && ssrSafeWindow()['chrome']?.webview?.registerTextureStream
);
}

Expand Down

0 comments on commit 5b9be17

Please sign in to comment.