Skip to content

Commit de46f14

Browse files
committed
feat(plugin): split firebase initialization into separate function
1 parent 92b1210 commit de46f14

File tree

4 files changed

+179
-162
lines changed

4 files changed

+179
-162
lines changed

README.md

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,26 @@ Use the module if:
2020
npm i workbox-firebase-auth // or yarn add workbox-firebase-auth
2121
```
2222

23-
2. Import the plugin and use it for your strategies
23+
2. Import the initialization helper and use it to initialize firebase in the service worker.
24+
Import the plugin and use it for your strategies.
2425

2526
Example:
2627

2728
```js
2829
import {registerRoute} from 'workbox-routing/registerRoute.mjs';
2930
import {NetworkFirst} from 'workbox-strategies/NetworkFirst.mjs';
30-
import FirebaseAuthPlugin from 'workbox-plugin-firebase-auth';
31+
import { initFirebase, Plugin as FirebaseAuthPlugin } from 'workbox-plugin-firebase-auth';
32+
33+
initFirebase({
34+
config: { /* your firebase config */ }
35+
})
3136

3237
registerRoute(
3338
/\.(?:png|gif|jpg|jpeg|svg)$/,
3439
new NetworkFirst({
3540
cacheName: 'authorizedApi',
3641
plugins: [
37-
new FirebaseAuthPlugin({
38-
firebase: {
39-
config: { /* your firebase config */ }
40-
}
41-
}),
42+
new FirebaseAuthPlugin(),
4243
],
4344
}),
4445
);
@@ -47,7 +48,7 @@ Use the module if:
4748
### CDN
4849

4950
If you are using [workbox-sw](https://developers.google.com/web/tools/workbox/modules/workbox-sw) to import workbox, you can use the [unpkg CDN](https://unpkg.com/) to import the plugin.
50-
It will then be available under the global variable `WorkboxPluginFirebaseAuth`.
51+
It will then be available under the global variable `WorkboxFirebaseAuth`.
5152

5253
Example:
5354

@@ -57,40 +58,36 @@ importScripts(
5758
'https://unpkg.com/[email protected]/lib/plugin.umd.js'
5859
)
5960

61+
WorkboxFirebaseAuth.initializeFirebase({
62+
config: { /* your firebase config */ }
63+
})
64+
6065
workbox.routing.registerRoute(
6166
/\.(?:png|gif|jpg|jpeg|svg)$/,
6267
new workbox.strategies.NetworkFirst({
6368
cacheName: 'authorizedApi',
6469
plugins: [
65-
new WorkboxPluginFirebaseAuth({
66-
firebase: {
67-
config: { /* your firebase config */ }
68-
}
69-
}),
70+
new WorkboxFirebaseAuth.Plugin(),
7071
],
7172
}),
7273
)
7374
```
7475

75-
## Options
76-
77-
If your service worker is served from firebase hosting, associated with the firebase app you use to authorize users, you can omit configuration altogether.
78-
Otherwise the [`firebase.config`](#firebaseconfig) parameter is **REQUIRED**.
76+
## `initFirebase` options
7977

80-
### firebase
78+
If your service worker is hosted firebase hosting, associated with the firebase app you use to authorize users, you don't have to specify any options (the helper will load the firebase SDK from [reserved URLs](https://firebase.google.com/docs/hosting/reserved-urls)).
79+
Otherwise the [`config`](#config) parameter is **REQUIRED**.
8180

82-
This key is used to configure the firebase instance.
81+
### config
8382

84-
#### firebase.config
85-
86-
**Required:** If your service worker is NOT served from firebase hosting or if you use a different app to authorize users.
83+
**Required:** If your service worker is NOT hosted on firebase hosting or if you use a different app to authorize users.
8784
**Type:** `object`
8885

8986
The [firebase config object](https://firebase.google.com/docs/web/setup?authuser=0#config-object) from the app that you use to authorize your users.
9087

91-
#### firebase.version
88+
#### version
9289

93-
**Type:** `string`
90+
**Type:** `string`
9491
**Default:** `7.14.2`
9592

9693
This key can be used to specify the firebase version to use.
@@ -99,6 +96,8 @@ This key can be used to specify the firebase version to use.
9996
> In the future this should use the version form the dependency used for development.
10097
> Sadly I haven't figured out how to achieve this yet (PRs welcome :sweat_smile:)
10198
99+
## `Plugin` options
100+
102101
### awaitResponse
103102

104103
**Type:** `boolean`
@@ -114,7 +113,7 @@ This key can be used to specify additional constraints on top of the route match
114113

115114
#### constraints.types
116115

117-
**Type:** `string | string[]`
116+
**Type:** `string | string[]`
118117
**Default:** `['*']`
119118

120119
This can be used to authorize only requests that accept certain types of responses (e.g. `application/json`)
@@ -124,14 +123,21 @@ This can be used to authorize only requests that accept certain types of respons
124123
125124
#### constraints.https
126125

127-
**Type:** `boolean`
126+
**Type:** `boolean`
128127
**Default:** `false`
129128

130-
Only allow requests from secure origins (`https://` or `localhost`) to be authorized.
129+
Only allow requests to secure origins (`https://` or `localhost`) to be authorized.
130+
131+
#### constraints.sameOrigin
132+
133+
**Type:** `boolean`
134+
**Default:** `true`
135+
136+
Only allow requests to the same origin as the service worker to be authorized.
131137

132138
#### constraints.ignorePaths
133139

134-
**Type:** `(string | RegExp)[]`
140+
**Type:** `(string | RegExp)[]`
135141
**Default:** `[]`
136142

137143
Paths to ignore when authorizing requests.

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default [
3939
input: 'src/plugin.ts',
4040
output: {
4141
file: 'lib/plugin.umd.js',
42-
name: 'WorkboxPluginFirebaseAuth',
42+
name: 'WorkboxFirebaseAuth',
4343
format: 'iife',
4444
},
4545
plugins: commonPlugins([typescript()]),

0 commit comments

Comments
 (0)