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
- ```
- npm i -D vite-ssg
- ```
vue-router @unhead/vue
- ```diff
- // package.json
- {
- "scripts": {
- "dev": "vite",
- - "build": "vite build"
- + "build": "vite-ssg build"
- // OR if you want to use another vite config file
- + "build": "vite-ssg build -c another-vite.config.ts"
- }
- }
- ```
- ```ts
- // src/main.ts
- import { ViteSSG } from 'vite-ssg'
- import App from './App.vue'
- // `export const createApp` is required instead of the original `createApp(App).mount('#app')`
- export const createApp = ViteSSG(
- // the root component
- App,
- // vue-router options
- { routes },
- // function to have custom setups
- ({ app, router, routes, isClient, initialState }) => {
- // install plugins etc.
- },
- )
- ```
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).
- ```ts
- // src/main.ts
- import { ViteSSG } from 'vite-ssg/single-page'
- import App from './App.vue'
- // `export const createApp` is required instead of the original `createApp(App).mount('#app')`
- export const createApp = ViteSSG(App)
- ```
The ClientOnly component is registered globally when the app is created.
- ```html
- <client-only>
- <your-client-side-components />
- <template #placeholder>
- <your-placeholder-components />
- </template>
- </client-only>
- ```
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:
- ```html
- <template>
- <button @click="count++">Click</button>
- </template>
- <script setup>
- import { useHead } from '@unhead/vue'
- useHead({
- title: 'Website Title',
- meta: [
- {
- name: 'description',
- content: 'Website description',
- },
- ],
- })
- </script>
- ```
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.