Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
/ vue-head Public archive

Document <head> manager for Vue 3, SSR ready.

Notifications You must be signed in to change notification settings

egoist/vue-head

Repository files navigation

Use @vueuse/head instead.

@egoist/vue-head

npm version size

Document <head> manager for Vue 3, SSR ready. (Experimental)

Install

yarn add @egoist/vue-head

Usage

Apply this plugin to your Vue instance:

import { createApp } from 'vue'
import { createHead } from '@egoist/vue-head'

const app = createApp()
const head = createHead()

app.use(head)

Use the <Head> component to add tags to <head>:

<template>
  <Head>
    <title>Hello Vue</title>
    <meta name="description" content="Do you like it?" />
  </Head>
</template>

<script>
import { Head } from '@egoist/vue-head'

export default {
  components: {
    Head,
  },
}
</script>

SSR Usage

On your server:

import { createApp, h, Fragment } from 'vue'
import { renderToString } from '@vue/server-renderer'

const app = createApp()
const appHTML = await renderToString(app)
const headHTML = await renderToString(
  h(Fragment, app.config.globalProperties.$head.headTags)
)

const finalHTML = `
<html>

<head>${headHTML}</head>

<body>${appHTML}</body>

</html
`

How does it work

On the client-side, head tags are <Teleport>-ed to document.head element.

On the server-side, head tags are stored as a VNode array, so you can render them to HTML using Vue.renderToString.

TODO

  • Testing
  • Support bodyAttrs, htmlAttrs

Prior Art

Inspired by react-head.

License

MIT © EGOIST