Skip to content

Commit 078604e

Browse files
author
Michael Lehenbauer
committed
Disable httpHeadersOverwriteParam for ReactNative to address #703.
1 parent bfa8a5e commit 078604e

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

packages/firestore/src/platform_browser/webchannel_connection.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { Code, FirestoreError } from '../util/error';
3636
import * as log from '../util/log';
3737
import { Rejecter, Resolver } from '../util/promise';
3838
import { StringMap } from '../util/types';
39+
import { isReactNative } from '../util/misc';
3940

4041
const LOG_TAG = 'Connection';
4142

@@ -201,10 +202,6 @@ export class WebChannelConnection implements Connection {
201202
// parameter.
202203
httpSessionIdParam: 'gsessionid',
203204
initMessageHeaders: {},
204-
// Send our custom headers as a '$httpHeaders=' url param to avoid CORS
205-
// preflight round-trip. This is formally defined here:
206-
// https://github.com/google/closure-library/blob/b0e1815b13fb92a46d7c9b3c30de5d6a396a3245/closure/goog/net/rpc/httpcors.js#L40
207-
httpHeadersOverwriteParam: '$httpHeaders',
208205
messageUrlParams: {
209206
// This param is used to improve routing and project isolation by the
210207
// backend and must be included in every request.
@@ -215,6 +212,19 @@ export class WebChannelConnection implements Connection {
215212
sendRawJson: true,
216213
supportsCrossDomainXhr: true
217214
};
215+
216+
// Send our custom headers as a '$httpHeaders=' url param to avoid CORS
217+
// preflight round-trip. This is formally defined here:
218+
// https://github.com/google/closure-library/blob/b0e1815b13fb92a46d7c9b3c30de5d6a396a3245/closure/goog/net/rpc/httpcors.js#L40
219+
//
220+
// For some unclear reason (see
221+
// https://github.com/firebase/firebase-js-sdk/issues/703), this breaks
222+
// ReactNative and so we exclude it, which just means ReactNative may be
223+
// subject to an extra network roundtrip for CORS preflight.
224+
if (!isReactNative()) {
225+
request['httpHeadersOverwriteParam'] = '$httpHeaders';
226+
}
227+
218228
this.modifyHeadersForRequest(request.initMessageHeaders, token);
219229
const url = urlParts.join('');
220230
log.debug(LOG_TAG, 'Creating WebChannel: ' + url + ' ' + request);

packages/firestore/src/util/misc.ts

+9
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,12 @@ export function immediateSuccessor(s: string): string {
121121
// Return the input string, with an additional NUL byte appended.
122122
return s + '\0';
123123
}
124+
125+
/**
126+
* Detects whether we are running in a React Native app.
127+
*/
128+
export function isReactNative(): boolean {
129+
return (
130+
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
131+
);
132+
}

0 commit comments

Comments
 (0)