Skip to content

Commit c493d92

Browse files
authored
Update README (#5214)
1 parent 8599d91 commit c493d92

File tree

1 file changed

+72
-95
lines changed

1 file changed

+72
-95
lines changed

packages/firebase/README.md

Lines changed: 72 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Build Status](https://travis-ci.org/firebase/firebase-js-sdk.svg?branch=master)](https://travis-ci.org/firebase/firebase-js-sdk)
1+
<!-- TODO: Build/Test badges when available. -->
22

33
# Firebase - App success made simple
44

@@ -36,25 +36,41 @@ you should use the
3636

3737
## Get the code (browser)
3838

39-
### Script include
40-
>This brings in all Firebase features. See
41-
>["Include only the features you need"](#include-only-the-features-you-need)
42-
>below for
43-
>how to minimize download size by only including the scripts you need.
39+
We recommend only installing the features you need. The individually installable services are:
4440

45-
Include Firebase in your web application via a `<script>` tag:
41+
- `firebase-app` - The core `firebase` client (required).
42+
- `firebase-app-check` - Firebase App Check (optional).
43+
- `firebase-analytics` - Firebase Analytics (optional).
44+
- `firebase-auth` - Firebase Authentication (optional).
45+
- `firebase-database` - The Firebase Realtime Database (optional).
46+
- `firebase-firestore` - Cloud Firestore (optional).
47+
- `firebase-storage` - Firebase Storage (optional).
48+
- `firebase-messaging` - Firebase Cloud Messaging (optional).
49+
- `firebase-functions` - Firebase Cloud Functions (optional).
50+
- `firebase-remote-config` - Firebase Remote Config (optional).
51+
- `firebase-performance` - Firebase Performance (optional).
52+
53+
### Script include
54+
Include Firebase in your web application via `<script>` tags. Create a script tag for each of the individual services you use (include `firebase-app`
55+
first):
4656

4757
```html
48-
<script src="https://www.gstatic.com/firebasejs/${JSCORE_VERSION}/firebase.js"></script>
58+
<!-- Always required. -->
59+
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-app.js"></script>
60+
<!-- Include only the services you use, for example auth and database below. -->
61+
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-auth.js"></script>
62+
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-database.js"></script>
63+
<!-- See above list for names of the other services. -->
4964

5065
<script>
51-
var app = firebase.initializeApp({
66+
const app = firebase.initializeApp({
5267
apiKey: '<your-api-key>',
5368
authDomain: '<your-auth-domain>',
5469
databaseURL: '<your-database-url>',
5570
projectId: '<your-cloud-firestore-project>',
5671
storageBucket: '<your-storage-bucket>',
57-
messagingSenderId: '<your-sender-id>'
72+
messagingSenderId: '<your-sender-id>',
73+
appId: '<your-app-id>'
5874
});
5975
// ...
6076
</script>
@@ -64,100 +80,76 @@ _Note: To get a filled in version of the above code snippet, go to the
6480
[Firebase console](https://console.firebase.google.com/) for your app and click on "Add
6581
Firebase to your web app"._
6682

67-
### npm bundler (Browserify, Webpack, etc.)
83+
#### Alternative - all-in-one import
6884

69-
>This brings in all Firebase features. See
70-
>["Include only the features you need"](#include-only-the-features-you-need)
71-
>below for
72-
>how to minimize bundle size by only importing the features you need.
85+
>This brings in all Firebase features. We recommend the method above to
86+
>minimize download size by only including the scripts you need.
87+
88+
Include Firebase in your web application via a `<script>` tag:
89+
90+
```html
91+
<script src="https://www.gstatic.com/firebasejs/${JSCORE_VERSION}/firebase.js"></script>
7392

74-
The Firebase JavaScript npm package contains code that can be run in the browser
75-
after combining the modules you use with a package bundler (e.g.,
76-
[Browserify](http://browserify.org/), [Webpack](https://webpack.github.io/)).
93+
<script>
94+
const app = firebase.initializeApp({ ... });
95+
// ...
96+
</script>
97+
```
7798

78-
Install the Firebase npm module:
99+
### NPM Bundler (Browserify, Webpack, Rollup, etc.)
79100

101+
Install the Firebase NPM module:
80102
```
81103
$ npm init
82104
$ npm install --save firebase
83105
```
84106

85-
In your code, you can access Firebase using:
107+
In your code, you can import the services you use:
86108

87109
```js
88-
var firebase = require('firebase');
89-
var app = firebase.initializeApp({ ... });
90-
```
91-
92-
If you are using ES6 imports or TypeScript:
110+
// This import loads the firebase namespace along with all its type information.
111+
import firebase from 'firebase/app';
93112

94-
```js
95-
import firebase from 'firebase';
96-
var app = firebase.initializeApp({ ... });
113+
// These imports load individual services into the firebase namespace.
114+
import 'firebase/auth';
115+
import 'firebase/database';
97116
```
98117

99-
### Include only the features you need
118+
Or if using `require()`:
100119

101-
The full Firebase JavaScript client includes support for Firebase Authentication, the
102-
Firebase Realtime Database, Firebase Storage, and Firebase Cloud Messaging. Including
103-
code via the above snippets will pull in all of these features.
120+
_Use the `.default` import from `firebase/app` in order for
121+
typings to work correctly.
122+
See [release notes for 8.0.0](https://firebase.google.com/support/release-notes/js#version_800_-_october_26_2020)._
104123

105-
You can reduce the amount of code your app uses by just including the features
106-
you need. The individually installable services are:
107-
108-
- `firebase-app` - The core `firebase` client (required).
109-
- `firebase-auth` - Firebase Authentication (optional).
110-
- `firebase-database` - The Firebase Realtime Database (optional).
111-
- `firebase-firestore` - Cloud Firestore (optional).
112-
- `firebase-storage` - Firebase Storage (optional).
113-
- `firebase-messaging` - Firebase Cloud Messaging (optional).
114-
- `firebase-functions` - Firebase Cloud Functions (optional).
124+
```js
125+
const firebase = require('firebase/app').default;
126+
require('firebase/auth');
127+
require('firebase/database');
115128

116-
From the CDN, include the individual services you use (include `firebase-app`
117-
first):
129+
const app = firebase.initializeApp({ ... });
130+
```
118131

119-
```html
120-
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-app.js"></script>
121-
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-auth.js"></script>
122-
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-database.js"></script>
123-
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-firestore.js"></script>
124-
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-storage.js"></script>
125-
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-messaging.js"></script>
126-
<script src="https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-functions.js"></script>
132+
_The type information from the import statement will include all of the SDKs,
133+
not just the ones you have `required`, so you could get a runtime error if you
134+
reference a non-required service._
127135

128-
<script>
129-
var app = firebase.initializeApp({ ... });
130-
// ...
131-
</script>
132-
```
136+
#### Alternative - all-in-one import
133137

134-
When using the firebase npm package, you can `require()` just the services that
135-
you use:
138+
>This brings in all Firebase features. We recommend the method above to
139+
>minimize download size by only including the scripts you need.
136140
137141
```js
138-
var firebase = require('firebase/app');
139-
require('firebase/auth');
140-
require('firebase/database');
141-
142-
var app = firebase.initializeApp({ ... });
142+
// This import loads all Firebase services, whether used in your code or not.
143+
import firebase from 'firebase';
143144
```
144145

145-
If you are using TypeScript with the npm package, you can import just the
146-
services you use:
146+
Or with `require()`:
147147

148148
```js
149-
// This import loads the firebase namespace along with all its type information.
150-
import firebase from 'firebase/app';
151-
152-
// These imports load individual services into the firebase namespace.
153-
import 'firebase/auth';
154-
import 'firebase/database';
149+
// This import loads all Firebase services, whether used in your code or not.
150+
const firebase = require('firebase').default;
155151
```
156152

157-
_The type information from the import statement will include all of the SDKs,
158-
not just the ones you have `required`, so you could get a runtime error if you
159-
reference a non-required service._
160-
161153
## Get the code (Node.js - server and command line)
162154

163155
### NPM
@@ -177,8 +169,10 @@ $ npm install --save firebase
177169
In your code, you can access Firebase using:
178170

179171
```js
180-
var firebase = require('firebase');
181-
var app = firebase.initializeApp({ ... });
172+
const firebase = require('firebase/app').default;
173+
require('firebase/auth');
174+
require('firebase/database');
175+
const app = firebase.initializeApp({ ... });
182176
// ...
183177
```
184178

@@ -196,23 +190,6 @@ import 'firebase/database';
196190

197191
_Known issue for typescript users with --experimental-modules: you have to set allowSyntheticDefaultImports to true in tsconfig.json to pass the type check. Use it with caution since it makes the assumption that all modules have a default export, which might not be the case for the other dependencies you have. And Your code will break if you try to import the default export from a module that doesn't have default export._
198192

199-
Firebase Storage is not included in the server side Firebase npm module.
200-
Instead, you can use the
201-
[`google-cloud` Node.js client](https://github.com/GoogleCloudPlatform/google-cloud-node).
202-
203-
```
204-
$ npm install --save google-cloud
205-
```
206-
207-
In your code, you can access your Storage bucket using:
208-
209-
```js
210-
var gcloud = require('google-cloud')({ ... });
211-
var gcs = gcloud.storage();
212-
var bucket = gcs.bucket('<your-firebase-storage-bucket>');
213-
...
214-
```
215-
216193
Firebase Cloud Messaging is not included in the server side Firebase npm module.
217194
Instead, you can use the
218195
[Firebase Cloud Messaging Rest API](https://firebase.google.com/docs/cloud-messaging/send-message).

0 commit comments

Comments
 (0)