Unverified Commit c03e94be authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Remove usage of `process.browser` (#7651)

parent ee2bdb89
...@@ -6,15 +6,16 @@ import fetch from 'isomorphic-unfetch' ...@@ -6,15 +6,16 @@ import fetch from 'isomorphic-unfetch'
let apolloClient = null let apolloClient = null
// Polyfill fetch() on the server (used by apollo-client) // Polyfill fetch() on the server (used by apollo-client)
if (!process.browser) { if (typeof window === 'undefined') {
global.fetch = fetch global.fetch = fetch
} }
function create (initialState) { function create (initialState) {
const isBrowser = typeof window !== 'undefined'
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient // Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
return new ApolloClient({ return new ApolloClient({
connectToDevTools: process.browser, connectToDevTools: isBrowser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once) ssrMode: !isBrowser, // Disables forceFetch on the server (so queries are only run once)
link: new HttpLink({ link: new HttpLink({
uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute) uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute)
credentials: 'same-origin' // Additional fetch() options like `credentials` or `headers` credentials: 'same-origin' // Additional fetch() options like `credentials` or `headers`
...@@ -26,7 +27,7 @@ function create (initialState) { ...@@ -26,7 +27,7 @@ function create (initialState) {
export default function initApollo (initialState) { export default function initApollo (initialState) {
// Make sure to create a new client for every server-side request so that data // Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad) // isn't shared between connections (which would be bad)
if (!process.browser) { if (typeof window === 'undefined') {
return create(initialState) return create(initialState)
} }
......
...@@ -34,7 +34,7 @@ export default ComposedComponent => { ...@@ -34,7 +34,7 @@ export default ComposedComponent => {
// Run all GraphQL queries in the component tree // Run all GraphQL queries in the component tree
// and extract the resulting data // and extract the resulting data
if (!process.browser) { if (typeof window === 'undefined') {
const apollo = initApollo() const apollo = initApollo()
try { try {
......
...@@ -6,15 +6,16 @@ import fetch from 'isomorphic-unfetch' ...@@ -6,15 +6,16 @@ import fetch from 'isomorphic-unfetch'
let apolloClient = null let apolloClient = null
// Polyfill fetch() on the server (used by apollo-client) // Polyfill fetch() on the server (used by apollo-client)
if (!process.browser) { if (typeof window === 'undefined') {
global.fetch = fetch global.fetch = fetch
} }
function create (initialState) { function create (initialState) {
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient // Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
const isBrowser = typeof window !== 'undefined'
return new ApolloClient({ return new ApolloClient({
connectToDevTools: process.browser, connectToDevTools: isBrowser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once) ssrMode: !isBrowser, // Disables forceFetch on the server (so queries are only run once)
link: new HttpLink({ link: new HttpLink({
uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute) uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute)
credentials: 'same-origin' // Additional fetch() options like `credentials` or `headers` credentials: 'same-origin' // Additional fetch() options like `credentials` or `headers`
...@@ -26,7 +27,7 @@ function create (initialState) { ...@@ -26,7 +27,7 @@ function create (initialState) {
export default function initApollo (initialState) { export default function initApollo (initialState) {
// Make sure to create a new client for every server-side request so that data // Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad) // isn't shared between connections (which would be bad)
if (!process.browser) { if (typeof window === 'undefined') {
return create(initialState) return create(initialState)
} }
......
...@@ -34,7 +34,7 @@ export default ComposedComponent => { ...@@ -34,7 +34,7 @@ export default ComposedComponent => {
// Run all GraphQL queries in the component tree // Run all GraphQL queries in the component tree
// and extract the resulting data // and extract the resulting data
if (!process.browser) { if (typeof window === 'undefined') {
const apollo = initApollo() const apollo = initApollo()
try { try {
......
...@@ -6,7 +6,7 @@ import fetch from 'isomorphic-unfetch' ...@@ -6,7 +6,7 @@ import fetch from 'isomorphic-unfetch'
let apolloClient = null let apolloClient = null
// Polyfill fetch() on the server (used by apollo-client) // Polyfill fetch() on the server (used by apollo-client)
if (!process.browser) { if (typeof window === 'undefined') {
global.fetch = fetch global.fetch = fetch
} }
...@@ -28,9 +28,10 @@ function create (initialState, { getToken, fetchOptions }) { ...@@ -28,9 +28,10 @@ function create (initialState, { getToken, fetchOptions }) {
}) })
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient // Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
const isBrowser = typeof window !== 'undefined'
return new ApolloClient({ return new ApolloClient({
connectToDevTools: process.browser, connectToDevTools: isBrowser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once) ssrMode: !isBrowser, // Disables forceFetch on the server (so queries are only run once)
link: authLink.concat(httpLink), link: authLink.concat(httpLink),
cache: new InMemoryCache().restore(initialState || {}) cache: new InMemoryCache().restore(initialState || {})
}) })
...@@ -39,7 +40,7 @@ function create (initialState, { getToken, fetchOptions }) { ...@@ -39,7 +40,7 @@ function create (initialState, { getToken, fetchOptions }) {
export default function initApollo (initialState, options) { export default function initApollo (initialState, options) {
// Make sure to create a new client for every server-side request so that data // Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad) // isn't shared between connections (which would be bad)
if (!process.browser) { if (typeof window === 'undefined') {
let fetchOptions = {} let fetchOptions = {}
// If you are using a https_proxy, add fetchOptions with 'https-proxy-agent' agent instance // If you are using a https_proxy, add fetchOptions with 'https-proxy-agent' agent instance
// 'https-proxy-agent' is required here because it's a sever-side only module // 'https-proxy-agent' is required here because it's a sever-side only module
......
...@@ -43,7 +43,7 @@ export default App => { ...@@ -43,7 +43,7 @@ export default App => {
return {} return {}
} }
if (!process.browser) { if (typeof window === 'undefined') {
// Run all graphql queries in the component tree // Run all graphql queries in the component tree
// and extract the resulting data // and extract the resulting data
try { try {
......
...@@ -5,14 +5,15 @@ let apolloClient = null ...@@ -5,14 +5,15 @@ let apolloClient = null
function create (initialState) { function create (initialState) {
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient // Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
const isBrowser = typeof window !== 'undefined'
return new ApolloClient({ return new ApolloClient({
connectToDevTools: process.browser, connectToDevTools: isBrowser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once) ssrMode: !isBrowser, // Disables forceFetch on the server (so queries are only run once)
link: new HttpLink({ link: new HttpLink({
uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute) uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute)
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers` credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
// Use fetch() polyfill on the server // Use fetch() polyfill on the server
fetch: !process.browser && fetch fetch: !isBrowser && fetch
}), }),
cache: new InMemoryCache().restore(initialState || {}) cache: new InMemoryCache().restore(initialState || {})
}) })
...@@ -21,7 +22,7 @@ function create (initialState) { ...@@ -21,7 +22,7 @@ function create (initialState) {
export default function initApollo (initialState) { export default function initApollo (initialState) {
// Make sure to create a new client for every server-side request so that data // Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad) // isn't shared between connections (which would be bad)
if (!process.browser) { if (typeof window === 'undefined') {
return create(initialState) return create(initialState)
} }
......
...@@ -17,7 +17,7 @@ export default App => { ...@@ -17,7 +17,7 @@ export default App => {
// Run all GraphQL queries in the component tree // Run all GraphQL queries in the component tree
// and extract the resulting data // and extract the resulting data
const apollo = initApollo() const apollo = initApollo()
if (!process.browser) { if (typeof window === 'undefined') {
try { try {
// Run all GraphQL queries // Run all GraphQL queries
await getDataFromTree( await getDataFromTree(
......
...@@ -44,7 +44,7 @@ Profile.getInitialProps = async ctx => { ...@@ -44,7 +44,7 @@ Profile.getInitialProps = async ctx => {
const url = `${process.env.API_URL}/api/profile.js` const url = `${process.env.API_URL}/api/profile.js`
const redirectOnError = () => const redirectOnError = () =>
process.browser typeof window !== 'undefined'
? Router.push('/login') ? Router.push('/login')
: ctx.res.writeHead(302, { Location: '/login' }).end() : ctx.res.writeHead(302, { Location: '/login' }).end()
......
...@@ -6,17 +6,17 @@ let graphQLClient = null ...@@ -6,17 +6,17 @@ let graphQLClient = null
function create (initialState = {}) { function create (initialState = {}) {
return new GraphQLClient({ return new GraphQLClient({
ssrMode: !process.browser, ssrMode: typeof window === 'undefined',
url: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', url: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn',
cache: memCache({ initialState }), cache: memCache({ initialState }),
fetch: process.browser ? fetch.bind() : unfetch, // eslint-disable-line fetch: typeof window !== 'undefined' ? fetch.bind() : unfetch, // eslint-disable-line
}) })
} }
export default function initGraphQL (initialState) { export default function initGraphQL (initialState) {
// Make sure to create a new client for every server-side request so that data // Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad) // isn't shared between connections (which would be bad)
if (!process.browser) { if (typeof window === 'undefined') {
return create(initialState) return create(initialState)
} }
......
...@@ -18,7 +18,7 @@ export default App => { ...@@ -18,7 +18,7 @@ export default App => {
// and extract the resulting data // and extract the resulting data
const graphQLClient = initGraphQL() const graphQLClient = initGraphQL()
let graphQLState = {} let graphQLState = {}
if (!process.browser) { if (typeof window === 'undefined') {
try { try {
// Run all GraphQL queries // Run all GraphQL queries
graphQLState = await getInitialState({ graphQLState = await getInitialState({
......
...@@ -2,7 +2,7 @@ import { action } from 'mobx' ...@@ -2,7 +2,7 @@ import { action } from 'mobx'
import { useObservable, useStaticRendering } from 'mobx-react-lite' import { useObservable, useStaticRendering } from 'mobx-react-lite'
import { createContext, useCallback } from 'react' import { createContext, useCallback } from 'react'
const isServer = !process.browser const isServer = typeof window === 'undefined'
useStaticRendering(isServer) useStaticRendering(isServer)
let StoreContext = createContext() let StoreContext = createContext()
......
...@@ -21,7 +21,7 @@ class MyMobxApp extends App { ...@@ -21,7 +21,7 @@ class MyMobxApp extends App {
constructor(props) { constructor(props) {
super(props) super(props)
const isServer = !process.browser const isServer = typeof window === 'undefined'
this.mobxStore = isServer this.mobxStore = isServer
? props.initialMobxState ? props.initialMobxState
: initializeStore(props.initialMobxState) : initializeStore(props.initialMobxState)
......
import { action, observable } from 'mobx' import { action, observable } from 'mobx'
import { useStaticRendering } from 'mobx-react' import { useStaticRendering } from 'mobx-react'
const isServer = !process.browser const isServer = typeof window === 'undefined'
useStaticRendering(isServer) useStaticRendering(isServer)
class Store { class Store {
......
...@@ -24,7 +24,7 @@ class MyApp extends App { ...@@ -24,7 +24,7 @@ class MyApp extends App {
const mutations = props.pageProps.mutations || [] const mutations = props.pageProps.mutations || []
if (process.browser) { if (typeof window !== 'undefined') {
// On the client we just instantiate the Overmind instance and run // On the client we just instantiate the Overmind instance and run
// the "changePage" action // the "changePage" action
this.overmind = createOvermind(config) this.overmind = createOvermind(config)
......
...@@ -2,7 +2,7 @@ import React from 'react' ...@@ -2,7 +2,7 @@ import React from 'react'
import App, { Container } from 'next/app' import App, { Container } from 'next/app'
import { prepareClientPortals } from '@jesstelford/react-portal-universal' import { prepareClientPortals } from '@jesstelford/react-portal-universal'
if (process.browser) { if (typeof window !== 'undefined') {
// On the client, we have to run this once before React attempts a render. // On the client, we have to run this once before React attempts a render.
// Here in _app is a great place to do it as this file is only required once, // Here in _app is a great place to do it as this file is only required once,
// and right now (outside the constructor) is before React is invoked. // and right now (outside the constructor) is before React is invoked.
......
...@@ -11,8 +11,9 @@ const BreakingNews = props => ( ...@@ -11,8 +11,9 @@ const BreakingNews = props => (
<p>{breaking.body}</p> <p>{breaking.body}</p>
</article> </article>
))} ))}
We are <b>{process.browser ? 'client-side' : 'server-side'}</b> (now, check We are{' '}
the source of this page) <b>{typeof window !== 'undefined' ? 'client-side' : 'server-side'}</b> (now,
check the source of this page)
<div> <div>
<small>generated at {new Date().toISOString()}</small> <small>generated at {new Date().toISOString()}</small>
</div> </div>
......
...@@ -7,7 +7,7 @@ export default class TopArticles extends React.Component { ...@@ -7,7 +7,7 @@ export default class TopArticles extends React.Component {
static async getInitialProps ({ props, req, res }) { static async getInitialProps ({ props, req, res }) {
// Fetch the weather from a remote API, it may take some time... // Fetch the weather from a remote API, it may take some time...
return new Promise(resolve => { return new Promise(resolve => {
console.log(process.browser ? 'client-side' : 'server-side') console.log(typeof window !== 'undefined' ? 'client-side' : 'server-side')
// Simulate a delay (slow network, huge computation...) // Simulate a delay (slow network, huge computation...)
setTimeout( setTimeout(
() => () =>
...@@ -21,7 +21,7 @@ export default class TopArticles extends React.Component { ...@@ -21,7 +21,7 @@ export default class TopArticles extends React.Component {
} }
render () { render () {
console.log(process.browser ? 'client-side' : 'server-side') console.log(typeof window !== 'undefined' ? 'client-side' : 'server-side')
return ( return (
<section> <section>
......
...@@ -43,7 +43,7 @@ let pageContext ...@@ -43,7 +43,7 @@ let pageContext
export default function getPageContext () { export default function getPageContext () {
// Make sure to create a new context for every server-side request so that data // Make sure to create a new context for every server-side request so that data
// isn't shared between connections (which would be bad). // isn't shared between connections (which would be bad).
if (!process.browser) { if (typeof window === 'undefined') {
return createPageContext() return createPageContext()
} }
......
import 'isomorphic-fetch' import 'isomorphic-fetch'
export const { initEnvironment, createEnvironment } = (!process.browser export const { initEnvironment, createEnvironment } = (typeof window ===
'undefined'
? require('./server') ? require('./server')
: require('./client') : require('./client')
).default ).default
...@@ -9,7 +9,7 @@ import { Button, ColorPicker, DatePicker, ProgressRing } from 'react-uwp' ...@@ -9,7 +9,7 @@ import { Button, ColorPicker, DatePicker, ProgressRing } from 'react-uwp'
class Index extends Component { class Index extends Component {
static async getInitialProps ({ req }) { static async getInitialProps ({ req }) {
let userAgent let userAgent
if (process.browser) { if (typeof window !== 'undefined') {
userAgent = navigator.userAgent userAgent = navigator.userAgent
} else { } else {
userAgent = req.headers['user-agent'] userAgent = req.headers['user-agent']
......
...@@ -7,7 +7,8 @@ let relayEnvironment = null ...@@ -7,7 +7,8 @@ let relayEnvironment = null
// and returns its results as a Promise: // and returns its results as a Promise:
function fetchQuery (operation, variables, cacheConfig, uploadables) { function fetchQuery (operation, variables, cacheConfig, uploadables) {
// Because we implement the graphql server, the client must to point to the same host // Because we implement the graphql server, the client must to point to the same host
const relayServer = process.browser ? '' : process.env.RELAY_SERVER const relayServer =
typeof window !== 'undefined' ? '' : process.env.RELAY_SERVER
return fetch(`${relayServer}/graphql`, { return fetch(`${relayServer}/graphql`, {
method: 'POST', method: 'POST',
headers: { headers: {
...@@ -28,7 +29,7 @@ export default function initEnvironment ({ records = {} } = {}) { ...@@ -28,7 +29,7 @@ export default function initEnvironment ({ records = {} } = {}) {
// Make sure to create a new Relay environment for every server-side request so that data // Make sure to create a new Relay environment for every server-side request so that data
// isn't shared between connections (which would be bad) // isn't shared between connections (which would be bad)
if (!process.browser) { if (typeof window === 'undefined') {
return new Environment({ return new Environment({
network, network,
store store
......
...@@ -26,7 +26,7 @@ export default function initEnvironment ({ records = {} } = {}) { ...@@ -26,7 +26,7 @@ export default function initEnvironment ({ records = {} } = {}) {
// Make sure to create a new Relay environment for every server-side request so that data // Make sure to create a new Relay environment for every server-side request so that data
// isn't shared between connections (which would be bad) // isn't shared between connections (which would be bad)
if (!process.browser) { if (typeof window === 'undefined') {
return new Environment({ return new Environment({
network, network,
store store
......
// NOTE: This require will be replaced with `@sentry/browser` when // NOTE: This require will be replaced with `@sentry/browser` when
// process.browser === true thanks to the webpack config in next.config.js
const Sentry = require('@sentry/node') const Sentry = require('@sentry/node')
const SentryIntegrations = require('@sentry/integrations') const SentryIntegrations = require('@sentry/integrations')
const Cookie = require('js-cookie') const Cookie = require('js-cookie')
...@@ -53,7 +52,7 @@ module.exports = (release = process.env.SENTRY_RELEASE) => { ...@@ -53,7 +52,7 @@ module.exports = (release = process.env.SENTRY_RELEASE) => {
scope.setExtra('statusCode', res.statusCode) scope.setExtra('statusCode', res.statusCode)
} }
if (process.browser) { if (typeof window !== 'undefined') {
scope.setTag('ssr', false) scope.setTag('ssr', false)
scope.setExtra('query', query) scope.setExtra('query', query)
scope.setExtra('pathname', pathname) scope.setExtra('pathname', pathname)
......
...@@ -6,7 +6,7 @@ import { counterStore } from '../containers/CounterContainer' ...@@ -6,7 +6,7 @@ import { counterStore } from '../containers/CounterContainer'
class MyApp extends App { class MyApp extends App {
static async getInitialProps () { static async getInitialProps () {
// do your server state here // do your server state here
if (!process.browser) { if (typeof window === 'undefined') {
// reset state for each request // reset state for each request
counterStore.resetState() counterStore.resetState()
// process state, in this case counter start with 999 // process state, in this case counter start with 999
...@@ -20,7 +20,7 @@ class MyApp extends App { ...@@ -20,7 +20,7 @@ class MyApp extends App {
super(props) super(props)
// pass the state to client store // pass the state to client store
// serverState will be reset when client navigate with Link // serverState will be reset when client navigate with Link
if (process.browser) { if (typeof window !== 'undefined') {
counterStore.initState(props.serverState.count) counterStore.initState(props.serverState.count)
} }
} }
......
...@@ -47,7 +47,7 @@ export function createHook( ...@@ -47,7 +47,7 @@ export function createHook(
} }
// @ts-ignore webpack optimization // @ts-ignore webpack optimization
if (process.browser) { if (typeof window !== 'undefined') {
const res = fetch(router.route + '?' + stringify(router.query), { const res = fetch(router.route + '?' + stringify(router.query), {
headers: { headers: {
accept: 'application/amp.bind+json', accept: 'application/amp.bind+json',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment