0.23.7 • Published 6 days ago

vite-ssg v0.23.7

Weekly downloads
664
License
MIT
Repository
github
Last release
6 days ago

Vite SSG

Static-site generation for Vue 3 on Vite.

NPM version

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

Install

This library requires Node.js version >= 14

// 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"
  }
}
// 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).

// 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)

<ClientOnly/>

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

<client-only>
  <your-client-side-components />
  <template #placeholder>
    <your-placeholder-components />
  </template>
</client-only>

Document head

We ship @unhead/vue to manage the document-head out of the box. You can use it directly in your pages/components. For example:

<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 for more usage information about useHead.

Critical CSS

Vite SSG has built-in support for generating Critical CSS inlined in the HTML via the critters package. Install it with:

npm i -D critters

Critical CSS generation will automatically be enabled for you.

To configure critters, pass its options into ssgOptions.crittersOptions in vite.config.ts:

// vite.config.ts
export default defineConfig({
  ssgOptions: {
    crittersOptions: {
      // E.g., change the preload strategy
      preload: 'media',
      // Other options: https://github.com/GoogleChromeLabs/critters#usage
    },
  },
})

Initial State

The initial state comprises data that is serialized with your server-side generated HTML and is hydrated in the browser when accessed. This data can be data fetched from a CDN, an API, etc, and is typically needed as soon as the application starts or is accessed for the first time.

The main advantage of setting the application's initial state is that the statically generated pages do not need to refetch the data as it is fetched and serialized into the page's HTML at build time.

The initial state is a plain JavaScript object that can be set during SSR. I.e. when statically generating the pages like this:

// src/main.ts

// ...

export const createApp = ViteSSG(
  App,
  { routes },
  ({ app, router, routes, isClient, initialState }) => {
    // ...

    if (import.meta.env.SSR) {
      // Set initial state during server side
      initialState.data = { cats: 2, dogs: 3 }
    }
    else {
      // Restore or read the initial state on the client side in the browser
      console.log(initialState.data) // => { cats: 2, dogs: 3 }
    }

    // ...
  },
)

Typically, you will use this with an application store, such as Vuex or Pinia. See below for examples:

Following Pinia's guide, you will to adapt your main.{ts,js} file to look like this:

// main.ts
import { ViteSSG } from 'vite-ssg'
import { createPinia } from 'pinia'
import routes from 'virtual:generated-pages'

// use any store you configured that you need data from on start-up
import { useRootStore } from './store/root'
import App from './App.vue'

export const createApp = ViteSSG(
  App,
  { routes },
  ({ app, router, initialState }) => {
    const pinia = createPinia()
    app.use(pinia)

    if (import.meta.env.SSR)
      initialState.pinia = pinia.state.value
    else
      pinia.state.value = initialState.pinia || {}

    router.beforeEach((to, from, next) => {
      const store = useRootStore(pinia)
      if (!store.ready)
        // perform the (user-implemented) store action to fill the store's state
        store.initialize()
      next()
    })
  },
)
// main.ts
import { ViteSSG } from 'vite-ssg'
import routes from 'virtual:generated-pages'
import { createStore } from 'vuex'
import App from './App.vue'

// Normally, you should definitely put this in a separate file
// in order to be able to use it everywhere
const store = createStore({
  // ...
})

export const createApp = ViteSSG(
  App,
  { routes },
  ({ app, router, initialState }) => {
    app.use(store)

    if (import.meta.env.SSR)
      initialState.store = store.state
    else
      store.replaceState(initialState.store)

    router.beforeEach((to, from, next) => {
      // perform the (user-implemented) store action to fill the store's state
      if (!store.getters.ready)
        store.dispatch('initialize')

      next()
    })
  },
)

For an example on how to use a store with an initial state in a single page app, see the single page example.

State Serialization

By default, the state is deserialized and serialized by using JSON.stringify and JSON.parse respectively. If this approach works for you, you should definitely stick to it as it yields far better performance.

You may use the transformState option in the ViteSSGClientOptions options object as shown below. A valid approach besides JSON.stringify and JSON.parse is @nuxt/devalue (which is used by Nuxt.js):

import devalue from '@nuxt/devalue'
import { ViteSSG } from 'vite-ssg'

// ...
import App from './App.vue'

export const createApp = ViteSSG(
  App,
  { routes },
  ({ app, router, initialState }) => {
    // ...
  },
  {
    transformState(state) {
      return import.meta.env.SSR ? devalue(state) : state
    },
  },
)

A minor remark when using @nuxt/devalue: In case, you are getting an error because of a require within the package @nuxt/devalue, you have to add the following piece of config to your Vite config:

// vite.config.ts
// ...

export default defineConfig({
  resolve: {
    alias: {
      '@nuxt/devalue': '@nuxt/devalue/dist/devalue.js',
    },
  },
  // ...
})

Async Components

Some applications may make use of Vue features that cause components to render asynchronously (e.g. suspense). When these features are used in ways that can influence initialState, the onSSRAppRendered may be used in order to ensure that all async operations are complete during the initial application render. For example:

const { app, router, initialState, isClient, onSSRAppRendered } = ctx

const pinia = createPinia()
app.use(pinia)

if (isClient) {
  pinia.state.value = (initialState.pinia) || {}
}
else {
  onSSRAppRendered(() => {
    initialState.pinia = pinia.state.value
  })
}

Configuration

You can pass options to Vite SSG in the ssgOptions field of your vite.config.js

// vite.config.js

export default {
  plugins: [],
  ssgOptions: {
    script: 'async',
  },
}

See src/types.ts. for more options available.

Custom Routes to Render

You can use the includedRoutes hook to include or exclude route paths to render, or even provide some completely custom ones.

// vite.config.js

export default {
  plugins: [],
  ssgOptions: {
    includedRoutes(paths, routes) {
      // exclude all the route paths that contains 'foo'
      return paths.filter(i => !i.includes('foo'))
    },
  },
}
// vite.config.js

export default {
  plugins: [],
  ssgOptions: {
    includedRoutes(paths, routes) {
      // use original route records
      return routes.flatMap((route) => {
        return route.name === 'Blog'
          ? myBlogSlugs.map(slug => `/blog/${slug}`)
          : route.path
      })
    },
  },
}

Alternatively, you may export the includedRoutes hook from your server entry file. This will be necessary if fetching your routes requires the use of environment variables managed by Vite.

// main.ts

import { ViteSSG } from 'vite-ssg'
import App from './App.vue'

export const createApp = ViteSSG(
  App,
  { routes },
  ({ app, router, initialState }) => {
    // ...
  },
)
export async function includedRoutes(paths, routes) {
  // Sensitive key is managed by Vite - this would not be available inside
  // vite.config.js as it runs before the environment has been populated.
  const apiClient = new MyApiClient(import.meta.env.MY_API_KEY)

  return Promise.all(
    routes.flatMap(async (route) => {
      return route.name === 'Blog'
        ? (await apiClient.fetchBlogSlugs()).map(slug => `/blog/${slug}`)
        : route.path
    }),
  )
}

Comparison

Use Vitepress when you want:

  • Zero config, out of the box SSG
  • A single-purpose documentation site
  • Lightweight (No double payload)

Use Vite SSG when you want:

  • More control on the build process and tooling
  • The flexible plugin system
  • Multi-purpose application with some SSG to improve SEO and loading speed

Cons:

  • Double payload

Example

See Vitesse.

Thanks to the prior work

Contribution

Please refer to https://github.com/antfu/contribute.

License

MIT License © 2020-PRESENT Anthony Fu

0.23.7

6 days ago

0.23.6

4 months ago

0.23.5

6 months ago

0.23.4

6 months ago

0.23.3

7 months ago

0.23.2

8 months ago

0.23.1

10 months ago

0.23.0

10 months ago

0.22.2

1 year ago

0.21.2

1 year ago

0.21.1

2 years ago

0.22.1

1 year ago

0.22.0

1 year ago

0.21.0

2 years ago

0.20.1

2 years ago

0.20.2

2 years ago

0.20.0

2 years ago

0.19.0

2 years ago

0.19.1

2 years ago

0.19.2

2 years ago

0.18.0

2 years ago

0.17.11

2 years ago

0.17.2

2 years ago

0.17.3

2 years ago

0.17.4

2 years ago

0.17.5

2 years ago

0.17.6

2 years ago

0.17.7

2 years ago

0.17.8

2 years ago

0.17.9

2 years ago

0.17.0

2 years ago

0.17.1

2 years ago

0.17.10

2 years ago

0.16.1

2 years ago

0.16.2

2 years ago

0.15.6

3 years ago

0.15.5

3 years ago

0.15.4

3 years ago

0.15.3

3 years ago

0.15.2

3 years ago

0.15.0

3 years ago

0.15.1

3 years ago

0.14.7

3 years ago

0.14.6

3 years ago

0.14.5

3 years ago

0.14.1

3 years ago

0.14.2

3 years ago

0.14.3

3 years ago

0.14.4

3 years ago

0.13.0

3 years ago

0.12.0

3 years ago

0.11.3

3 years ago

0.11.4

3 years ago

0.11.2

3 years ago

0.11.0

3 years ago

0.11.1

3 years ago

0.9.3

3 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.9.2

3 years ago

0.9.0

3 years ago

0.9.1

3 years ago

0.8.12

3 years ago

0.8.11

3 years ago

0.8.10

3 years ago

0.8.9

3 years ago

0.8.8

3 years ago

0.8.7

3 years ago

0.8.5

3 years ago

0.8.6

3 years ago

0.8.4

3 years ago

0.8.3

3 years ago

0.8.1

3 years ago

0.8.2

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago