prerender()

You can programmatically trigger pre-rendering:

// my-build-script.js
 
import { prerender } from 'vike/prerender'
 
// Without options
prerender()
 
// All options are optional
prerender({
  pageContextInit,
  viteConfig,
  onPagePrerender
})

Use API > prerender > disableAutoRun to avoid pre-rendering to be run twice.

pageContextInit

The initial value of each page's pageContext object.

prerender({
  pageContextInit: {
    someData: 42,
    // ...
  }
})

viteConfig

The Vite config.

It's optional: if omitted, Vite automatically loads vite.config.js.

We recommend to either omit this option or set it to prerender({ viteConfig: { root }}): the vite.config.js file living at root will be loaded.

Alternatively you can:

  • Set:
    prerender({
      viteConfig: {
        configFile: require.resolve('./path/to/vite.config.js')
      }
    })
  • Not load any vite.config.js file and, instead, use prerender({ viteConfig: { configFile: false, ...myViteConfig }}) to programmatically define the entire Vite config.

You can also load a vite.config.js file while overriding parts of the Vite config.

See Vite > JavaScript API > InlineConfig.

onPagePrerender()

⚠️
Don't use this option without having contacted a Vike maintainer: this functionality may be changed/removed at any time if you don't.

The onPagePrerender option allows you to control where/how HTML files are written.

prerender({
  // If onPagePrerender() is set, then Vike won't write the HTML files to the filesystem
  onPagePrerender(pageContext) {
    // Write the HTML to the filesystem
    // ...
  }
})

More settings

For more settings, see API > prerender.

See also