0.1.8 • Published 5 years ago
@egoist/vue-head v0.1.8
@egoist/vue-head
Document <head> manager for Vue 3, SSR ready. (Experimental)
Install
yarn add @egoist/vue-headUsage
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