0.1.8 • Published 4 years ago

@egoist/vue-head v0.1.8

Weekly downloads
19
License
MIT
Repository
github
Last release
4 years ago

@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.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

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago