From bf525a7d35ffded6b1864ad71488582ea88cf728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 23 Apr 2019 14:34:19 +0200 Subject: [PATCH 1/3] Use navigator instead of process.browser --- src/withESI.test.tsx | 8 ++++---- src/withESI.tsx | 8 ++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/withESI.test.tsx b/src/withESI.test.tsx index 875fcbc..a89e0d3 100644 --- a/src/withESI.test.tsx +++ b/src/withESI.test.tsx @@ -9,7 +9,7 @@ test("client-side", () => { const DummyESI = withESI(Dummy, "id"); expect(DummyESI.displayName).toBe("WithESI(Dummy)"); - (global.process as any).browser = true; + (global as any).navigator = {}; const component = renderer.create(); expect(component).toMatchSnapshot(); }); @@ -18,7 +18,7 @@ test("client-side with serialized props", () => { const DummyESI = withESI(Dummy, "id"); expect(DummyESI.displayName).toBe("WithESI(Dummy)"); - (global.process as any).browser = true; + (global as any).navigator = {}; ((global as any).__REACT_ESI__ as any) = { id: { name: "Anne" } }; const component = renderer.create(); expect(component).toMatchSnapshot(); @@ -35,7 +35,7 @@ test("client-side call getInitialProps", async () => { const ComponentESI = withESI(Component, "initial-props"); - (global.process as any).browser = true; + (global as any).navigator = {}; renderer.create(); expect(called).toBe(true); }); @@ -45,7 +45,7 @@ test("server-side", () => { expect(DummyESI.displayName).toBe("WithESI(Dummy)"); process.env.REACT_ESI_SECRET = "dummy"; - (global.process as any).browser = false; + delete (global as any).navigator; const component = renderer.create( ); diff --git a/src/withESI.tsx b/src/withESI.tsx index 22bdae8..3f403bc 100644 --- a/src/withESI.tsx +++ b/src/withESI.tsx @@ -8,10 +8,6 @@ declare global { } } -interface IWebpackProcess extends NodeJS.Process { - browser?: boolean; -} - interface IWithESIProps { esi?: { attrs?: object; @@ -46,7 +42,7 @@ export default function withESI

( this.esi = esi || {}; this.state.childProps = childProps; - if (!(process as IWebpackProcess).browser) { + if (typeof window.navigator === 'undefined') { return; } @@ -81,7 +77,7 @@ export default function withESI

( } public render() { - if ((process as IWebpackProcess).browser) { + if (typeof navigator !== 'undefined') { return (

From bfaf8f6dc2300e906d47b39dee274864da373200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 23 Apr 2019 14:36:55 +0200 Subject: [PATCH 2/3] Remove unused import --- src/withESI.test.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/withESI.test.tsx b/src/withESI.test.tsx index a89e0d3..385da72 100644 --- a/src/withESI.test.tsx +++ b/src/withESI.test.tsx @@ -1,4 +1,3 @@ -import express from "express"; import React from "react"; import renderer from "react-test-renderer"; import withESI from "./withESI"; From 07de3bb313e4c4b39bce6b70ab437db615ac326e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 23 Apr 2019 14:55:20 +0200 Subject: [PATCH 3/3] Review --- src/withESI.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/withESI.tsx b/src/withESI.tsx index 3f403bc..7995bfb 100644 --- a/src/withESI.tsx +++ b/src/withESI.tsx @@ -42,7 +42,7 @@ export default function withESI

( this.esi = esi || {}; this.state.childProps = childProps; - if (typeof window.navigator === 'undefined') { + if (typeof navigator === 'undefined') { return; }