frandiox / vite-ssr

Use Vite for server side rendering in Node
MIT License
824 stars 91 forks source link

How to setup with vite-plugin-pages #122

Closed kylemod closed 2 years ago

kylemod commented 2 years ago

I tried to setup vite-ssr with vite-plugin-pages but i get an error.

Cannot read properties of undefined (reading 'forEach')
at addPagePropsGetterToRoutes (http://localhost:3000/node_modules/.vite/chunk-2JXO2AHT.js?v=4e3c17a5:63:10)
at viteSSR4 (http://localhost:3000/node_modules/.vite/vite-ssr_vue_entry-client.js?v=4e3c17a5:56:5)
at http://localhost:3000/src/main.js:16:16

I also tried this configuration from vite-ssr-vue3-example

Here is my sources

main.js

//import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import viteSSR from 'vite-ssr'
import { createHead } from '@vueuse/head';
import 'virtual:windi.css'
import 'prismjs/themes/prism-tomorrow.css'
import VueFeather from 'vue-feather'

// const options = {
//   router,
//   pageProps: {
//     passToPage: false
//   }
// }

export default viteSSR(App, { router } , (context) => {
  const { app } = context
  const head = createHead()

  app.use(head)
  app.component(VueFeather.name, VueFeather)

  return {
     head
  }
})

vite.config.js

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Pages from "vite-plugin-pages"
...

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue({
      include: [/\.vue$/, /\.md$/]
    }),
    Pages({
      nuxtStyle: true
    }),
    viteSSR(),
    ...
  ]
})

router.js

import { createRouter, createWebHistory } from 'vue-router'
import routes from '~pages'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

export default router
frandiox commented 2 years ago

You can just pass routes to ViteSSR instead of router. ViteSSR will create the correct router for you using the routes you provide. Example here (the layout stuff is a separate plugin but it's not necessary).

kylemod commented 2 years ago

Thanks, it worked!