Skip to content

Commit

Permalink
Merge pull request #59 from FormidableLabs/fix-ssr-detection
Browse files Browse the repository at this point in the history
Fix SSR detection
  • Loading branch information
andyrichardson authored May 7, 2020
2 parents a47f80c + 7b88b1f commit 5450665
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/exchange.ssr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @jest-environment node
*/
import { fromValue } from 'wonka';
import * as messengers from './utils';

const createNativeMessenger = jest.spyOn(messengers, 'createNativeMessenger');
const createBrowserMessenger = jest.spyOn(messengers, 'createBrowserMessenger');

it('returns forwarding exchange', () => {
(global as any).window = undefined;
const { devtoolsExchange } = require('./exchange'); // eslint-disable-line
expect(createNativeMessenger).toBeCalledTimes(0);
expect(createBrowserMessenger).toBeCalledTimes(0);

const value = fromValue('Heloo');
const forward = jest.fn();

devtoolsExchange({ forward })(value);
expect(forward).toBeCalledTimes(1);
expect(forward).toBeCalledWith(value);
});
5 changes: 3 additions & 2 deletions src/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ const messageHandlers = {
} as const;

export const devtoolsExchange = ((): Exchange => {
const isNative = navigator?.product === 'ReactNative';
const isSSR = !isNative && typeof window === undefined;
const isNative =
typeof navigator !== 'undefined' && navigator?.product === 'ReactNative';
const isSSR = !isNative && typeof window === 'undefined';

// Prod or SSR
if (process.env.NODE_ENV === 'production' || isSSR) {
Expand Down

0 comments on commit 5450665

Please sign in to comment.