What's the equivalent of NuxtServerInit in nuxt-bridge with pinia? #696
-
|
Hi, I have serveral mandatory request to perform before rendering any pages. This request happen in server side and I use to perform them inside nuxtServerInit action. What's the best approach with the new architecture? A module, a middleware, a lazyasyncdata in App.vue? Thanks for the clarification |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
as far i know the nuxtServerInit was only a plugin where the nuxtServerInit action was called maybe you can do the same and call a pinia action on a server plugin |
Beta Was this translation helpful? Give feedback.
-
|
Yes I think I can reproduce this behavior but I don't really know where to put the logic to be the most efficient. |
Beta Was this translation helpful? Give feedback.
-
|
I spent hours on all the docs, including nitro, pinia, looking for hooks, reading source code, so I hope this answer can help someone. When people talk about server plugin they usually talk about a regular plugin suffixed by In order to have a nuxtServerInit action, use this file: // /plugins/nuxtServerInit.server.ts file
import { useGeneralStore } from '~~/stores/general'
export default defineNuxtPlugin(async () => {
const generalStore = useGeneralStore()
await generalStore.fetchSomething()
})// /stores/general.ts file
import { defineStore } from 'pinia'
interface State {
aaa: string
}
export const useGeneralStore = defineStore('generalStore', {
state: (): State => ({
aaa: null
}),
actions: {
async fetchSomething() {
const { data } = await $fetch('http://www.url.com')
this.aaa = data
}
}
})Because of auto imports everything will work as is. |
Beta Was this translation helpful? Give feedback.
I spent hours on all the docs, including nitro, pinia, looking for hooks, reading source code, so I hope this answer can help someone.
When people talk about server plugin they usually talk about a regular plugin suffixed by
.serverand exposed withdefineNuxtPlugin. They usualy don't talk about/server/plugins.In order to have a nuxtServerInit action, use this file: