vite-ssg: Build failing when pinia store is used in modules

Please check this issue here, its either pinia related or vite-ssg - but in any case I couldn’t figure out the best way to install global middleware that uses the store, that doesn’t throw this error on build.

Any suggestions on how to access the store in a router.beforeEach, which is initialized with the app?

And in case the store uses localStorage initialization which won’t be available during generation, any best practices there?

Would be great if one could direct me to an example repo with such middleware.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 22 (1 by maintainers)

Most upvoted comments

That did the trick!

Try awaiting router only on server side with if (!ctx.isClient)

export const createApp = ViteSSG(
  App,
  { routes },
  async(ctx) => {
    if (!ctx.isClient)
      await ctx.router.isReady()

    // install all modules under `modules/`
    Object.values(import.meta.globEager('./modules/*.ts')).map(i => i.install?.(ctx))
  },
)

Probably you are trying to access pinia store before it’s getting installed or; registered another router hook on other modules.

Try putting await router.isReady() on main.ts like that:

export const createApp = ViteSSG(
  App,
  { routes },
  async(ctx) => {
    await ctx.router.isReady()

    // install all modules under `modules/`
    Object.values(import.meta.globEager('./modules/*.ts')).map(i => i.install?.(ctx))
  },
)

This would fix your issue if you are trying to access pinia store on other modules.