Vite SSG

Static site generation for Vue 3 on Vite

README

Vite SSG


Static-site generation for Vue 3 on Vite.

ℹ️ Vite 2 is supported from v0.2.x, Vite 1's support is discontinued.


Install


This library requires Node.js version >= 14


  1. ```
  2. npm i -D vite-ssg
  3. ```
vue-router @unhead/vue

  1. ```diff
  2. // package.json
  3. {
  4.   "scripts": {
  5.     "dev": "vite",
  6. -   "build": "vite build"
  7. +   "build": "vite-ssg build"

  8.     // OR if you want to use another vite config file
  9. +   "build": "vite-ssg build -c another-vite.config.ts"
  10.   }
  11. }
  12. ```

  1. ```ts
  2. // src/main.ts
  3. import { ViteSSG } from 'vite-ssg'
  4. import App from './App.vue'

  5. // `export const createApp` is required instead of the original `createApp(App).mount('#app')`
  6. export const createApp = ViteSSG(
  7.   // the root component
  8.   App,
  9.   // vue-router options
  10.   { routes },
  11.   // function to have custom setups
  12.   ({ app, router, routes, isClient, initialState }) => {
  13.     // install plugins etc.
  14.   },
  15. )
  16. ```

Single Page SSG


For SSG of an index page only (i.e. without vue-router); import vite-ssg/single-page instead, and only install @unhead/vue (npm i -D vite-ssg @unhead/vue).

  1. ```ts
  2. // src/main.ts
  3. import { ViteSSG } from 'vite-ssg/single-page'
  4. import App from './App.vue'

  5. // `export const createApp` is required instead of the original `createApp(App).mount('#app')`
  6. export const createApp = ViteSSG(App)
  7. ```

### ``

The ClientOnly component is registered globally when the app is created.

  1. ```html
  2. <client-only>
  3.   <your-client-side-components />
  4.   <template #placeholder>
  5.     <your-placeholder-components />
  6.   </template>
  7. </client-only>
  8. ```

Document head


We ship [@unhead/vue](https://unhead.harlanzw.com/integrations/vue/setup) to manage the document-head out of the box. You can use it directly in your pages/components.
For example:

  1. ```html
  2. <template>
  3.   <button @click="count++">Click</button>
  4. </template>
  5. <script setup>
  6. import { useHead } from '@unhead/vue'
  7. useHead({
  8.   title: 'Website Title',
  9.   meta: [
  10.     {
  11.       name: 'description',
  12.       content: 'Website description',
  13.     },
  14.   ],
  15. })
  16. </script>
  17. ```

That's all! No configuration is needed. Vite SSG will automatically handle the server-side rendering and merging.

See [@unhead/vue's docs](https://unhead.unjs.io/setup/vue/installation) for more usage information about useHead.