onBeforeRenderHtml() hook

Environment: server.

Implemented by: vike-vue.

You need vike-vue to be able to use onBeforeRenderHtml().

Hook called right before rendering the +Page component to HTML.

It's called upon rendering the first page.

  • It's called regardless of whether SSR is disabled. (The first page the user visits is always rendered to HTML: when SSR is disabled then the HTML is just an HTML shell that doesn't contain the content of the page.)
  • It isn't called upon page navigation. (Pages aren't rendered to HTML upon page navigation.)

If you want to perform an action only upon SSR then check whether pageContext.Page is set:

// +onBeforeRenderHtml.js
 
export function onBeforeRenderHtml(pageContext) {
  const isSSR = !!pageContext.Page
  if (isSSR) {
    // ...
  }
}

Use cases

It's usually used for integrating tools, such as collecting the page's CSS defined by a CSS-in-JS library.

See also