Vite SSG
Vite SSG is a library designed for static-site generation (SSG) of Vue 3 applications built with Vite. It streamlines the process of pre-rendering your Vue application into static HTML files, enhancing SEO and initial load performance. The current stable version is v28.3.0, and the project maintains a regular release cadence, often aligning with updates to its peer dependencies like Vite and Vue Router, with occasional minor and patch releases. Key differentiators include its tight integration with the Vite ecosystem, built-in support for `@unhead/vue` for document head management, and automatic critical CSS generation via the `beasties` package. Since v27.0.0, Vite SSG has exclusively adopted an ESM-only module format, dropping CommonJS support entirely. It supports Vue Router for multi-page SSG and offers a specialized entry point for single-page SSG scenarios, requiring Node.js v20 or newer.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ...vite-ssg... not supported.
cause Attempting to import `vite-ssg` using CommonJS `require()` syntax in a Node.js environment or a configuration that expects CJS.fixMigrate your project to use ECMAScript Modules (ESM). Add `"type": "module"` to your `package.json` and use `import` statements instead of `require()`. Ensure `vite.config.js` is named `vite.config.ts` or `vite.config.mjs` if it contains ESM syntax. -
Error: vite-ssg requires Node.js version >= 20.0.0
cause The installed version of `vite-ssg` (v28.0.0+) requires a Node.js runtime of version 20 or higher.fixUpgrade your Node.js environment. Use a version manager like `nvm` (`nvm install 20 && nvm use 20`) or update your runtime environment to Node.js 20 or newer. -
TypeError: createApp is not a function (runtime error during SSG build)
cause The `createApp` function required by Vite SSG is not exported correctly from your application's entry file, or the `ViteSSG` wrapper was not used.fixVerify that your `src/main.ts` (or equivalent) contains `export const createApp = ViteSSG(...)` as demonstrated in the documentation, rather than directly `createApp(...).mount(...)`.
Warnings
- breaking Vite SSG v28.0.0 and newer require Node.js v20 or greater. Projects on older Node.js versions must upgrade their environment.
- breaking Vite SSG moved to ESM-only (ECMAScript Modules) from v27.0.0, dropping support for CommonJS (CJS) module format. This impacts how the library is imported and used in projects.
- gotcha The main entry file (e.g., `src/main.ts`) must `export const createApp` instead of directly mounting the app (`createApp(App).mount('#app')`). This is crucial for Vite SSG to properly hook into the application creation process.
- gotcha To enable Rollup to effectively tree-shake client-side code and remove server-only logic during the client build, wrap server-specific code blocks with `if (import.meta.env.SSR) { /* server code */ } else { /* client code */ }`.
- breaking Vite SSG frequently updates its peer dependency ranges for `vite` and `vue-router` (e.g., supporting Vite v8 and Vue Router v5 in v28.3.0). While this offers broader compatibility, ensure your project's versions of these dependencies meet the required ranges to avoid build or runtime issues.
Install
-
npm install vite-ssg -
yarn add vite-ssg -
pnpm add vite-ssg
Imports
- ViteSSG
const { ViteSSG } = require('vite-ssg')import { ViteSSG } from 'vite-ssg' - ViteSSG (Single Page)
import { ViteSSG } from 'vite-ssg'import { ViteSSG } from 'vite-ssg/single-page' - useHead
import { useHead } from 'vite-ssg'import { useHead } from '@unhead/vue' - ClientOnly
import { ClientOnly } from 'vite-ssg'import { ClientOnly } from 'vite-ssg/client'
Quickstart
import { ViteSSG } from 'vite-ssg'
import App from './App.vue'
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{ path: '/', component: () => import('./pages/Home.vue') },
{ path: '/about', component: () => import('./pages/About.vue') },
]
export const createApp = ViteSSG(
App,
{ routes, base: import.meta.env.BASE_URL },
({ app, router, routes, isClient, initialState }) => {
// install plugins, register global components, etc.
// app.use(somePlugin)
// if (isClient) console.log('Client-side setup finished!')
}
)
// package.json script update
// {
// "scripts": {
// "dev": "vite",
// "build": "vite-ssg build"
// }
// }