vue-head-slot

0.1.4 • Public • Published

vue-head-slot

Manage content for your head element easily. Internally uses Vue3's <teleport>.

Installation

Installing

yarn add vue-head-slot

or

npm i -S vue-head-slot

Usage

import VueHeadSlot from 'vue-head-slot'
...
app.use(VueHeadSlot)

And anywhere in your app.

<head-slot>
    <meta name="robots" content="nofollow">
</head-slot>

Results in...

<html>
<head>
    ...
    <meta name="robots" content="nofollow">
</head>
...

Just be sure to remove content from index.html that you intend to manage like this. This appends content, so it will duplicate tags that are already declared.

Other examples

Using with vue-router.

<!-- Some page in your route scheme -->
<head-slot>
    <title>{{ $route.name }}</title>
    <link rel="canonical" :href="dynamicallyBuiltUrl" />
    <link v-for="lang in langs" :key="lang" rel="alternate" :hreflang="lang" :href="`${dynamicallyBuiltUrl}/${lang}`" />
</head-slot>

Disclaimer

Vue3's Teleport makes this so easy that you probably don't need this module.

Just use <teleport to="head">...</teleport>.

I used render functions to seem a bit more clever.

// pretty much the whole source code
import { h, Teleport } from 'vue'

export default {
  install(app, name) {
    app.component(name || 'head-slot', {
      render() {
        return h(
          Teleport,
          { to: 'head' },
          this.$slots.default()
        )
      }
    })
  }
}

Trivia

This was almost called vue-head-shot. I expect credit if someone creates a fork with that name.

License

Copyright 2020 nick-somebody

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependents (0)

Package Sidebar

Install

npm i vue-head-slot

Weekly Downloads

4

Version

0.1.4

License

MIT

Unpacked Size

56.8 kB

Total Files

13

Last publish

Collaborators

  • nick-somebody