Skip to content

Commit 5450665

Browse files
Merge pull request #59 from FormidableLabs/fix-ssr-detection
Fix SSR detection
2 parents a47f80c + 7b88b1f commit 5450665

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/exchange.ssr.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
import { fromValue } from 'wonka';
5+
import * as messengers from './utils';
6+
7+
const createNativeMessenger = jest.spyOn(messengers, 'createNativeMessenger');
8+
const createBrowserMessenger = jest.spyOn(messengers, 'createBrowserMessenger');
9+
10+
it('returns forwarding exchange', () => {
11+
(global as any).window = undefined;
12+
const { devtoolsExchange } = require('./exchange'); // eslint-disable-line
13+
expect(createNativeMessenger).toBeCalledTimes(0);
14+
expect(createBrowserMessenger).toBeCalledTimes(0);
15+
16+
const value = fromValue('Heloo');
17+
const forward = jest.fn();
18+
19+
devtoolsExchange({ forward })(value);
20+
expect(forward).toBeCalledTimes(1);
21+
expect(forward).toBeCalledWith(value);
22+
});

src/exchange.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ const messageHandlers = {
143143
} as const;
144144

145145
export const devtoolsExchange = ((): Exchange => {
146-
const isNative = navigator?.product === 'ReactNative';
147-
const isSSR = !isNative && typeof window === undefined;
146+
const isNative =
147+
typeof navigator !== 'undefined' && navigator?.product === 'ReactNative';
148+
const isSSR = !isNative && typeof window === 'undefined';
148149

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

0 commit comments

Comments
 (0)