-
-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use Nuxt with VueFire and SSR #203
Comments
It is indeed missing the docs. The whole idea goes around data: () => ({ items: [] }), // initialise items
async asyncData () {
await this.$bind('items', db.collection('items'))
// no need to return the data as it is bound in data
return {}
} |
@posva you don't have access to |
Forgot about that! With vuexfire you can still access the store from the context passed to asyncdata but with vuefire it's a bit different. It should be possible to call $bind through the Vue constructor with something like Vue.prototype.$bind.call({}) and await that promise in one asyncdata property but I need to check a bit more since the whole point of using bind was to make it work both on client and on server |
Hi All, I could not find I tried something below and no success data: () => ({ text: '' }), // initialise items
async asyncData () {
const ref = db.collection('test').doc('test')
const snap = await ref.get()
await this.$nuxt.$bind('text', snap.data().text)
// await this.$nuxt.$bind('text', 'Today is a good day.') // also not working by giving a value directly.
return
} I use Nuxt plugin to add |
This is not yet supported on vuefire package. It's only supported by vuexfire since you can await an action in fetch or asyncdata. I need to find a way to fix hydration and will expose a nuxt plugin or module in the future to enable ssr wot vuefire. |
Is there any update on this? Would be really awesome |
No updates no. For the moment use vuexfire, it's more adapted to bigger apps anyway :). When you are doing SSR, your app isn't that simple anymore. |
@posva I understand the vuexfire repo has now been archived, but is there any chance you could take another look at posva/vuexfire#124 (comment) and address it here? I linked to a (not working) demo repo in that thread. Someone just commented on the demo, also looking for help with Nuxt and vuexfire, but I still haven't figured it out. Thanks so much for your hard work! |
vuexfire is now here, that's why it got archived. I'll paste the example I put there about how to use Vuexfire with Nuxt: // firebase/index.js
import { firebase } from '@firebase/app'
import 'firebase/firestore'
import 'firebase/auth'
const config = {
apiKey: '...',
authDomain: '...',
projectId: '...',
}
export const db = firebase.initializeApp(config).firestore()
// Export utility functions
export const { Timestamp } = firebase.firestore // firebase/collections.js
import { db } from './index'
export const users = db.collection('users')
export const matches = db.collection('matches')
// store/index.js
import { vuexfireMutations as mutations } from 'vuexfire'
export const state = () => ({})
export { mutations } // store/matches.js
import { firestoreAction } from 'vuexfire'
import { matches, users } from '../firebase/collections'
// omitting state, mutations, getters
const actions = {
subscribeToAllMatches: firestoreAction(({ bindFirestoreRef }) => {
return bindFirestoreRef('matches', matches)
}),
// async version
subscribeToMatch: firebasestore(async ({ bindFirestoreRef }, { matchRef ) => {
await bindFirestoreRef('match', matchRef)
}),
} pages/matches.vue <template>
<div>
<ul>
<li v-for="match in matches">{{ match }}</li>
</ul>
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
computed: mapState('matches', ['matches']),
// return a promise
fetch({ store }) {
return store.dispatch('matches/subscribeToAllMatches')
},
}
</script> |
@posva That's not working for me! I got 'this. subscribetoAllMatches is not a function' error at the fetch hook. But in the create hook it works. |
@posva is the nuxt plugin going to be another package? |
a plugin would be part of the package, but not sure yet of the features to add yet |
I played around with vuexfire and nuxt when testing if vuexfire works with nuxt-fire (see this issue), and I got it to work just fine. Without using fetch() but using the created() hook. I created this example repository for vuexfire with nuxt-fire, I hope it helps someone: I'm using nuxt-fire here but this is basically the same as initiating firebase yourself, so it should work the same way for projects who are not using nuxt-fire. The examples uses universal mode (resp. nuxt generate) and not SPA mode. Update: |
but using the |
How to make it reactive while ssr? After the first load i have content, but it's not reactive, only after i bind again on client side. But when i bind, it automatically unbinds the previously bound property, which means that the property is reseted (null / [] / ...) and it causes a flash on the site before the client subscribe ending. |
@krisztianodor that is something being tracked at #83 |
If anyone is still stumbling on this, I made vuexfire work in a Nuxt SSR app with using See comment: lupas/nuxt-fire-vuexfire-example#1 (comment) Example is with nuxt-fire, but that shouldn't make a difference. Only thing that I would like to improve now is find a way to only need to call the binding action once and have it automatically be bound on server and client side, anyone an idea? |
@lupas Would we want to bind on the server? Maybe the server is just a simple query, get the data we need to render html, then we bind on the client? |
@fergusmeiklejohn But if we want to keep our code simple and make use of vuefire (-> not having to write all the mutations and all that ourselves), this could be an option to save us some lines of code. My thinking here is that on the server the process could be like so:
-> probably a bit less performant Unbinding (e.g. in the destroyed() hook) might be important though to avoid memory leaks. Not sure, would need to be tested properly before used in production for sure. |
Hi guys, vuefire is failing on page refresh when using the new fetch() api and NuxtJS: Screenshot of console error: Here is my stackoverflow question with details: Anyone know how to fix? |
Anyone have thoughts on the above question? Updated documentation for Nuxt would be greatly appreciated! |
Hi all, nuxt IMHO
Here how I use
Here how I bind in my page inside
Here how
Does anyone know why I get the hydration mismatch when loading the page? |
This is currently being tracked at #1241 For those adventurous enough to try the current nuxt module (Nuxt 3 only although it should work with Bridge too):
Some notes: AppCheck is still a work in progress for SSR. I recommend only prerendering routes and not doing SSR yet when the app is deployed. Refer to Firebase docs for information about AppCheck. The docs will also be updated I added this to the roadmap |
An interesting repository I just ran across in terms of appcheck: The person is trying to get auth verification done in edge runtimes. Maybe this is helpful to further bring appcheck support to nuxt in more environments? |
Thanks, that's interesting! It seems that the firebase sdk is not ready yet to run on the edge though. AppCheck and SSR are working better now, still need to update the docs and create some extra playgrounds |
I dont understand how that works, please add to dock or give me a example
The text was updated successfully, but these errors were encountered: