Skip to content

Commit 2725596

Browse files
committed
docs: many improvements
1 parent 8cab300 commit 2725596

File tree

7 files changed

+117
-47
lines changed

7 files changed

+117
-47
lines changed

docs/cookbook/migration-v2-v3.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ Terms starting with _rtdb_ are now prefixed with _database_ to match the Firebas
1717

1818
The `firestorePlugin` and `rtdbPlugin` are now deprecated in favor of _modules_. They are still available but will be removed in the next major version. You should use `VueFire`, `VueFireFirestoreOptionsAPI` and `VueFireDatabaseOptionsAPI` instead:
1919

20-
```diff
20+
```ts
2121
const app = createApp({})
2222

2323
// for firestore
24-
-app.use(firestorePlugin)
25-
+app.use(VueFire, { modules: [VueFireFirestoreOptionsAPI] })
24+
app.use(firestorePlugin, options) // [!code --]
25+
app.use(VueFire, { modules: [VueFireFirestoreOptionsAPI(options)] }) // [!code ++]
2626

2727
// for database
28-
-app.use(rtdbPlugin)
29-
+app.use(VueFire, { modules: [VueFireDatabaseOptionsAPI] })
30-
````
28+
app.use(rtdbPlugin, options) // [!code --]
29+
app.use(VueFire, { modules: [VueFireDatabaseOptionsAPI(options)] }) // [!code ++]
30+
```
3131

3232
## Breaking changes
3333

docs/cookbook/subscriptions-external.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ export const useTodoStore = defineStore('todos', () => {
2323
})
2424
```
2525

26-
Note you will still have to follow the [Firebase API](https://firebase.google.com/docs/firestore/manage-data/structure-data) (e.g. `addDoc()`, `updateDoc()`, etc) to update the data.
26+
Note you will still have to follow the [Firebase API](https://firebase.google.com/docs/firestore/manage-data/structure-data) (e.g. `addDoc()`, `updateDoc()`, etc) to update the data and you will also need [to wait for the data to be loaded on the server](../guide/ssr.md#usage-outside-of-components).

docs/guide/getting-started.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ Before using VueFire, make sure you have a Firebase account and a project setup
66

77
In order to get started make sure to install the latest version of `vuefire` as well as `firebase`:
88

9-
```sh
9+
::: code-group
10+
11+
```sh [pnpm]
12+
pnpm i vuefire firebase
13+
```
14+
15+
```sh [yarn]
1016
yarn add vuefire firebase
11-
# or
12-
npm install vuefire firebase
1317
```
1418

19+
```sh [npm]
20+
npm i vuefire firebase
21+
```
22+
23+
:::
24+
1525
::: warning
1626

1727
VueFire requires Firebase JS SDK >= 9 but **is compatible with Vue 2 and Vue 3**.
@@ -71,7 +81,7 @@ Note that we will refer to `database` and `firestore` as `db` in examples where
7181

7282
First, install the VueFire Vue plugin. It will allow you to add extra modules like [AppCheck](./app-check.md) or [Auth](./auth.md) to your app.
7383

74-
```ts
84+
```ts{2,8-15}
7585
import { createApp } from 'vue'
7686
import { VueFire, VueFireAuth } from 'vuefire'
7787
import App from './App.vue'
@@ -93,10 +103,9 @@ app.mount('#app')
93103

94104
This will give you access to some [convenient composables](./firebase-composables.md) like `useFirebaseApp()`, `useFirestore()` and `useDatabase()` in your components:
95105

96-
```vue
106+
```vue{2-3}
97107
<script setup>
98108
import { useFirestore } from 'vuefire'
99-
100109
const db = useFirestore()
101110
</script>
102111
@@ -216,27 +225,29 @@ VueFire can also be used with the Options API, while less flexible, it's still a
216225

217226
<FirebaseExample>
218227

219-
```js
228+
```js{2,7-10}
220229
import { createApp } from 'vue'
221230
import { VueFire, VueFireDatabaseOptionsAPI } from 'vuefire'
222231
223232
const app = createApp(App)
224233
app.use(VueFire, {
225234
// ...
226235
modules: [
236+
// Add any modules you want to use here
227237
VueFireDatabaseOptionsAPI(),
228238
],
229239
})
230240
```
231241

232-
```js
242+
```js{2,7-10}
233243
import { createApp } from 'vue'
234244
import { VueFire, VueFireFirestoreOptionsAPI } from 'vuefire'
235245
236246
const app = createApp(App)
237247
app.use(VueFire, {
238248
// ...
239249
modules: [
250+
// Add any modules you want to use here
240251
VueFireFirestoreOptionsAPI(),
241252
],
242253
})

docs/guide/realtime-data.md

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const someTodo = useDatabaseObject(dbRef(db, 'todos', 'someId'))
2626
<template>
2727
<ul>
2828
<li v-for="todo in todos" :key="todo.id">
29-
<span>{{ todo.text }}</span>
29+
<span>{{ todo.text }}</span>
3030
</li>
3131
</ul>
3232
</template>
@@ -44,7 +44,7 @@ const someTodo = useDocument(doc(collection(db, 'todos'), 'someId'))
4444
<template>
4545
<ul>
4646
<li v-for="todo in todos" :key="todo.id">
47-
<span>{{ todo.text }}</span>
47+
<span>{{ todo.text }}</span>
4848
</li>
4949
</ul>
5050
</template>
@@ -61,18 +61,16 @@ Sometimes, you need to start observing a different document or collection, let's
6161
```ts
6262
const route = useRoute()
6363
// since route is reactive, `contactSource` will be reactive too
64-
const contactSource = computed(
65-
() => dbRef(db, 'contacts/' + route.params.id)
66-
)
64+
const contactSource = computed(() => dbRef(db, 'contacts/' + route.params.id))
6765
// contact will always be in sync with the data source
6866
const contact = useDatabaseObject(contactSource)
6967
```
7068

7169
```ts
7270
const route = useRoute()
7371
// since route is reactive, `contactSource` will be reactive too
74-
const contactSource = computed(
75-
() => doc(collection(db, 'contacts'), route.params.id)
72+
const contactSource = computed(() =>
73+
doc(collection(db, 'contacts'), route.params.id)
7674
)
7775
// contact will always be in sync with the data source
7876
const contact = useDocument(contactSource)
@@ -102,7 +100,7 @@ const {
102100
// did the subscription fail?
103101
error,
104102
// A promise that resolves or rejects when the initial state is loaded
105-
promise
103+
promise,
106104
} = useDocument(contactSource)
107105
```
108106

@@ -118,20 +116,28 @@ To fetch data only _once_, pass the [`once`](https://vuefire.vuejs.org/api/inter
118116

119117
<FirebaseExample>
120118

121-
```ts{3,4}
119+
```ts{5,8}
122120
import { useDatabaseList, useDatabaseObject } from 'vuefire'
123121
import { ref as dbRef } from 'firebase/database'
124122
125-
const todos = useDatabaseList(dbRef(db, 'todos'), { once: true })
126-
const someTodo = useDatabaseObject(dbRef(db, 'todos', 'someId'), { once: true })
123+
const todos = useDatabaseList(dbRef(db, 'todos'), {
124+
once: true,
125+
})
126+
const someTodo = useDatabaseObject(dbRef(db, 'todos', 'someId'), {
127+
once: true,
128+
})
127129
```
128130

129-
```ts{3,4}
131+
```ts{5,8}
130132
import { useCollection, useDocument } from 'vuefire'
131133
import { collection, doc } from 'firebase/firestore'
132134
133-
const todos = useCollection(collection(db, 'todos'), { once: true })
134-
const someTodo = useDocument(doc(collection(db, 'todos'), 'someId'), { once: true })
135+
const todos = useCollection(collection(db, 'todos'), {
136+
once: true,
137+
})
138+
const someTodo = useDocument(doc(collection(db, 'todos'), 'someId'), {
139+
once: true,
140+
})
135141
```
136142

137143
</FirebaseExample>
@@ -239,7 +245,7 @@ Given some _users_ with _documents_ that are being viewed by other _users_. This
239245

240246
```js
241247
{
242-
name: 'Jessica',
248+
name: 'Claire',
243249
documents: [
244250
// The document is stored somewhere else. Here we are only holding a reference
245251
doc(collection(db, 'documents'), 'gift-list'),
@@ -263,22 +269,22 @@ In the example above, `documents` is an array of References. Let's look at the d
263269

264270
```js
265271
{
266-
name: 'Jessica',
272+
name: 'Claire',
267273
documents: [
268274
{
269275
content: '...',
270276
sharedWith: [
271277
{
272-
name: 'Alex',
278+
name: 'Chris',
273279
documents: [
274-
'documents/alex-todo-list',
280+
'documents/chris-todo-list',
275281
]
276282
},
277283
{
278-
name: 'Robin',
284+
name: 'Leon',
279285
documents: [
280-
'documents/robin-todo-list',
281-
'documents/robin-book',
286+
'documents/leon-todo-list',
287+
'documents/leon-book',
282288
],
283289
},
284290
],
@@ -287,7 +293,7 @@ In the example above, `documents` is an array of References. Let's look at the d
287293
}
288294
```
289295

290-
`documents.sharedWith.documents` end up as arrays of strings. Those strings are actually _paths_ that can be passed to `doc()` as in `doc(db, 'documents/robin-book')` to get the actual reference to the document. By being a string instead of a Reference, it is possible to display a bound document with VueFire as plain text.
296+
`documents.sharedWith.documents` end up as arrays of strings. Those strings are actually _paths_ that can be passed to `doc()` as in `doc(db, 'documents/leon-book')` to get the actual reference to the document. By being a string instead of a Reference, it is possible to display a bound document with VueFire as plain text.
291297

292298
It is possible to customize this behavior by providing a [`maxRefDepth` option](../api/interfaces/UseDocumentOptions.md#maxrefdepth):
293299

@@ -367,3 +373,16 @@ const todoList = useDocument(
367373
})
368374
)
369375
```
376+
377+
In Firebase 10, `withConverter()` takes two generics instead of one:
378+
379+
```ts{2,5}
380+
// ...
381+
import type { DocumentData } from 'firebase/firestore'
382+
383+
const todoList = useDocument(
384+
doc(db, 'todos').withConverter<TodoI, DocumentData>({
385+
// ...
386+
})
387+
)
388+
```

docs/guide/ssr.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ Note that by default, vite-ssg (used by Vitesse) uses `JSON.stringify()` to seri
7575

7676
```ts
7777
// src/main.ts
78-
import devalue from '@nuxt/devalue'
78+
// https://github.com/Rich-Harris/devalue#usage
79+
import devalue from 'devalue'
7980
import { ViteSSG } from 'vite-ssg'
8081
import App from './App.vue'
8182

@@ -87,7 +88,7 @@ export const createApp = ViteSSG(
8788
},
8889
{
8990
transformState(state) {
90-
return import.meta.env.SSR ? devalue(state) : state
91+
return import.meta.env.SSR ? devalue.stringify(state) : devalue.parse(state)
9192
},
9293
},
9394
)
@@ -153,14 +154,16 @@ await usePendingPromises()
153154

154155
## Exclude from hydration
155156

156-
You can exclude data from hydration by passing `false` to the `ssrKey` option:
157+
You can exclude data from hydration by passing `false` to the `ssrKey` option. This is useful when there is no point in waiting for the data to be fetched on the server, e.g. when the data is not being rendered on the server.
157158

158159
```ts
159160
useDocument(..., { ssrKey: false })
160161
useDatabaseList(..., { ssrKey: false })
161162
// etc
162163
```
163164

165+
This only works if you avoid rendering on server these documents or collections. **If still render them on server, you will get a hydration error on client**.
166+
164167
<!-- TODO: I wonder if we could attach effect scopes to applications so `onServerPrefetch()` is still awaited when attached -->
165168

166169
<!--

docs/nuxt/deployment.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ defineNuxtConfig({
2020

2121
The Spark plan is a free plan that enable most of firebase functions. With this plan, you want to **prerender your app and deploy it as a static site**. In order to do this, make sure **not to apply the Firebase preset** when bundling your app and to use the `generate` command:
2222

23-
```bash
23+
```sh
2424
nuxt generate
2525
```
2626

2727
You can then let your CI deploy your app to Firebase or do it manually:
2828

29-
```bash
29+
```sh
3030
firebase deploy
3131
```
3232

33+
Make sure you **don't have** a `nitro.preset` option set in your `nuxt.config.ts` file.
34+
3335
## Blaze plan
3436

3537
::: warning
@@ -38,6 +40,10 @@ The Firebase preset is still experimental. It is not recommended to use it in pr
3840

3941
The Blaze plan is a pay-as-you-go that allows you to run Firebase Functions. It's free up to a certain amount of requests. With this plan, you can either do the same as with the [Spark plan](#spark-plan) (cheaper) or build with the Firebase preset and deploy your app as a serverless function:
4042

41-
```bash
43+
```sh
4244
NITRO_PRESET=firebase nuxt build
4345
```
46+
47+
### Route Rules
48+
49+
On top of prerendering any routes, you can also use [the `routeRules` option](https://nuxt.com/docs/api/configuration/nuxt-config#routerules-1) to apply any headers like cache options, redirects or even static rendering.

docs/nuxt/getting-started.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,40 @@ The Nuxt VueFire module is still a work in progress and it might contain breakin
66

77
## Installation
88

9-
```bash
9+
::: code-group
10+
11+
```sh [pnpm]
12+
pnpm install vuefire nuxt-vuefire firebase
13+
```
14+
15+
```sh [yarn]
16+
yarn add vuefire nuxt-vuefire firebase
17+
```
18+
19+
```sh [npm]
1020
npm install vuefire nuxt-vuefire firebase
1121
```
1222

23+
:::
24+
1325
Additionally, if you are using [SSR](https://nuxt.com/docs/api/configuration/nuxt-config/#ssr), you need to install `firebase-admin` and its peer dependencies:
1426

15-
```bash
16-
npm install firebase-admin @firebase/app-types
27+
::: code-group
28+
29+
```sh [pnpm]
30+
pnpm install firebase-admin firebase-functions @firebase/app-types
31+
```
32+
33+
```sh [yarn]
34+
yarn add firebase-admin firebase-functions @firebase/app-types
35+
```
36+
37+
```sh [npm]
38+
npm install firebase-admin firebase-functions @firebase/app-types
1739
```
1840

41+
:::
42+
1943
## Configuration
2044

2145
Next, add `nuxt-vuefire` to the `modules` section of `nuxt.config.js` and configure it with the credentials created in your app settings in your project overview (`https://console.firebase.google.com/project/YOUR_PROJECT_NAME/overview)`. You can find more details [in Firebase Documentation](https://firebase.google.com/docs/web/setup#create-project). It should look something like this:
@@ -39,6 +63,8 @@ export default defineNuxtConfig({
3963
})
4064
```
4165

66+
### Configuring the Admin SDK
67+
4268
If you are using SSR with any auth related feature, you will need to create a [service account](https://firebase.google.com/support/guides/service-accounts) and provide its content as an _environment variable_ named `GOOGLE_APPLICATION_CREDENTIALS`.
4369

4470
In local development it's more convenient to put the `service-account.json` file alongside other files and refer to its path in the environment variable:
@@ -48,7 +74,12 @@ GOOGLE_APPLICATION_CREDENTIALS=service-account.json
4874
```
4975

5076
:::tip
51-
This service account file contains sensitive information and should **not be committed to your repository**.
77+
This service account file contains sensitive information and should **not be committed to your repository**. Make sure to exclude it from your version control system:
78+
79+
```sh
80+
echo service-account.json >> .gitignore
81+
```
82+
5283
:::
5384

5485
### Additional configuration

0 commit comments

Comments
 (0)