Skip to content

Commit

Permalink
fix: made firebase context a function to support SSR use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
codediodeio committed Jul 10, 2020
1 parent 4554f62 commit c2b045e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,12 @@ data.subscribe(v => doStuff(v) )

### Firebase App Context

The Firebase SDK is available via the [Context API](https://svelte.dev/tutorial/context-api) under the key of `firebase`.
The Firebase SDK is available via the [Context API](https://svelte.dev/tutorial/context-api) under the key of `firebase` using the `getFirebase` function.

```js
const db = getContext('firebase').firestore();
const app = getContext('firebase').getFirebase();
const db = app.firestore();
const auth = app.auth();
```

## API
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.4",
"version": "0.1.5",
"name": "sveltefire",
"svelte": "src/index.js",
"main": "dist/index.js",
Expand Down
12 changes: 9 additions & 3 deletions src/FirebaseApp.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
// Emit firebase
const dispatch = createEventDispatcher();
// Set firebase context
firebase = firebase || window.firebase;
setContext("firebase", firebase);
// Must be a function to ensure changes after initialization are caught
setContext("firebase", {
getFirebase: () => firebase
});
onMount(() => {
// Set firebase context from window if needed
firebase = firebase || (window && window.firebase);
if (!firebase) {
throw Error(
"No firebase app was provided. You must provide an initialized Firebase app or make it available globally."
Expand Down
8 changes: 4 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getContext } from 'svelte';


export function getApp() {
return getContext('firebase');
export function getFirebaseContext() {
const { getFirebase } = getContext('firebase');
return getFirebase();
}

// Validates end-user has setup context and imported proper modules into the Svelte app
export function assertApp(pkg) {

const app = getApp();
const app = getFirebaseContext();

if (!app) {
throw new Error(`Missing Firebase app in context. Are you inside a 'FirebaseApp' component?`)
Expand Down

1 comment on commit c2b045e

@vhollo
Copy link

@vhollo vhollo commented on c2b045e Sep 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
I'm still having trouble to implement this wonderful extension in Sapper.
Please, please, provide a short tutorial on v0.1.5!

Please sign in to comment.