Skip to content

Commit 4ffc979

Browse files
authored
Disable httpHeadersOverwriteParam for ReactNative to address #703. (#717)
1 parent 099688f commit 4ffc979

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

packages/firestore/src/local/simple_db.ts

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export class SimpleDb {
111111
}
112112

113113
// Check the UA string to find out the browser.
114+
// TODO(mikelehen): Move this logic into packages/util/environment.ts
114115
const ua = window.navigator.userAgent;
115116

116117
// IE 10

packages/firestore/src/platform_browser/webchannel_connection.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
createWebChannelTransport
2323
} from '@firebase/webchannel-wrapper';
2424

25+
import { isReactNative } from '@firebase/util';
26+
2527
import { Token } from '../api/credentials';
2628
import { DatabaseId, DatabaseInfo } from '../core/database_info';
2729
import { SDK_VERSION } from '../core/version';
@@ -201,10 +203,6 @@ export class WebChannelConnection implements Connection {
201203
// parameter.
202204
httpSessionIdParam: 'gsessionid',
203205
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',
208206
messageUrlParams: {
209207
// This param is used to improve routing and project isolation by the
210208
// backend and must be included in every request.
@@ -215,7 +213,30 @@ export class WebChannelConnection implements Connection {
215213
sendRawJson: true,
216214
supportsCrossDomainXhr: true
217215
};
216+
218217
this.modifyHeadersForRequest(request.initMessageHeaders, token);
218+
219+
// Sending the custom headers we just added to request.initMessageHeaders
220+
// (Authorization, etc.) will trigger the browser to make a CORS preflight
221+
// request because the XHR will no longer meet the criteria for a "simple"
222+
// CORS request:
223+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests
224+
//
225+
// Therefore to avoid the CORS preflight request (an extra network
226+
// roundtrip), we use the httpHeadersOverwriteParam option to specify that
227+
// the headers should instead be encoded into a special "$httpHeaders" query
228+
// parameter, which is recognized by the webchannel backend. This is
229+
// formally defined here:
230+
// https://github.com/google/closure-library/blob/b0e1815b13fb92a46d7c9b3c30de5d6a396a3245/closure/goog/net/rpc/httpcors.js#L32
231+
//
232+
// But for some unclear reason (see
233+
// https://github.com/firebase/firebase-js-sdk/issues/703), this breaks
234+
// ReactNative and so we exclude it, which just means ReactNative may be
235+
// subject to the extra network roundtrip for CORS preflight.
236+
if (!isReactNative()) {
237+
request['httpHeadersOverwriteParam'] = '$httpHeaders';
238+
}
239+
219240
const url = urlParts.join('');
220241
log.debug(LOG_TAG, 'Creating WebChannel: ' + url + ' ' + request);
221242
// tslint:disable-next-line:no-any Because listen isn't defined on it.

0 commit comments

Comments
 (0)