Environment: server.

Implemented by: vike-vue/vike-react.

You need vike-vue/vike-react to be able to use bodyHtmlEnd.

The settings bodyHtmlEnd/bodyHtmlBegin enable you to insert HTML at the beginning/end of the <body> tag.

For use cases, such as adding <script> tags, you can also use <Head> or +client.js.

// pages/+config.js
 
export default {
  bodyHtmlEnd: '<div id="something"></div>'
}
// pages/+bodyHtmlEnd.js
 
export default (pageContext) => {
  const { something } = pageContext
  return `<div>${something}</div>`
}
⚠️

Be cautious about the security risk called XSS injections.

Vue

When using Vue, it's usually used for adding the HTML targets of teleports.

Teleports work out of the box when using <Teleport to="teleported"> and you don't have to use bodyHtml{Begin,End} then.

vike-vue always inserts this at the end of the <body> tag:

<div id="teleported">${
  pageContext.ssrContext.teleports?.['#teleported'] ?? ''
}</div>

You can use Vike's pageContext object to access Vue's ssrContext object:

// pages/+bodyHtmlEnd.js
 
export default (pageContext) => {
  const content = pageContext.ssrContext.teleports?.['#someTeleport'] ?? ''
  return `<div id="someTeleport">${content}</div>`
}

Global

This setting is global: its value always applies to all your pages. In other words, it doesn't support config inheritance.

You cannot set it at pages/some-page/+config.js: you have to set it at pages/+config.js (or some other global location) instead.

If you want to set different values for different pages, then consider creating a custom setting as shown at Guides > <head> tags > Custom settings.

See also