npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue3-head

v1.0.4

Published

Vue 3 plugin to manage head meta tags

Downloads

237

Readme

vue3-head

Manage the head section of your page with ease. Set title, meta description etc. easily by using this Vue 3 Plugin

Vue JS Seminar

Why is there a package?

Typically the <head> section of your app is outside of your Vue JS app. This makes it hard to manage the head section of your app with Vue JS. Use this package to embed or change the head section of your app with ease.

Things you can do with this package:

  • Set the title of your page
  • Set the meta description of your page
  • Set the meta keywords of your page
  • Set the meta author of your page
  • Set the meta robots of your page
  • Set the meta viewport of your page
  • Set the meta charset of your page
  • Set the meta http-equiv of your page
  • Set the meta og:[**] of your page
  • Set the meta twitter:[**] of your page
  • Set the link canonical of your page
  • Set the link alternate of your page
  • Set the link icon of your page
  • Set the link stylesheet of your page
  • Set the link dns-prefetch of your page
  • Set the link preconnect of your page
  • Set the link prerender of your page

etc.

Installation

Run npm or yarn installation of the vue3-head package:

yarn

$ yarn add vue3-head

npm

$ npm install vue3-head

Set Up your Vue Application

// App.vue

import { createApp } from 'vue'
import App from './App.vue'
import { createVue3Head } from 'vue3-head';

const app = createApp(App)
const head = createVue3Head()
app.use(head)

app.mount('#app')

Usage

Basic Usage

<script setup>
import { useHead } from 'vue3-head'

useHead({
    title: 'Your website',
    //...
})
</script>

Reactivity

It is important how you pass the data to the useHead function. If you pass a reactive object, the head will be updated automatically.

<script setup>
  import { ref } from "vue";
  import { useHead } from "vue3-head";

  const newTitle = ref("Vue 3 Head Plugin");
  const newDescription = ref("Vue 3 Head Plugin is a Vue 3 plugin that allows you to manage your document head tags with ease.");
  useHead({
    title: newTitle, // this will update the title tag
    meta: [
      { name: "description", content: newDescription.value }, // newDescription won't update the meta tag
    //...

In this example, even both newTitle and newDescription are reactive, only newTitle will update the title tag. That's because the title is passed as reference object and the content of the meta tag is passed as a string (as .value delivers the string value of the ref object).

Examples

Mixing reactivity and strings

<script setup>
import { ref } from "vue";
import { useHead } from "vue3-head";

const newTitle = ref("Vue 3 Head Plugin");
const newDescription = ref("Vue 3 Head Plugin is a Vue 3 plugin that allows you to manage your document head tags with ease.");
const theCanonical = ref("https://example.com");
const theUrl = ref("https://example.com");
const favicon = ref("https://example.com/favicon.ico");
const newViewport = ref("width=device-width, initial-scale=2");

useHead({
  title: newTitle, // this will update the title tag
  meta: [
    { name: "description", content: newDescription.value }, // newDescription won't update the meta tag
    { name: "keywords", content: "Vue 3, Head, Plugin" },
    { name: "viewport", content: newViewport.value },
  ],
  link: [
    {rel: "icon", href: favicon.value},
    {rel: "canonical", href: theCanonical.value},
    {rel: "preload", href: "https://www.funnerix.com/js/widget_loader.js?v=2.0", as: "script"},
    {rel: "stylesheet", href: "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css", integrity: "sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH", crossorigin: "anonymous"},
  ],
  og: [
    {property: "og:title", content: newTitle.value},
    {property: "og:description", content: newDescription.value},
    {property: "og:url", content: theUrl.value},
  ],
  styles: [
    "body { background-color: #f0f0f0; }",
  ],
  scripts: [
    { src: "https://www.funnerix.com/js/widget_loader.js?v=2.0", defer: true },
    'console.log("Hello from Vue 3 Head Plugin")',
  ],
});
</script>

Setting the Title onClick

In case you want to change the title of your page on a button click, you can use the following code:

<template>
  <button @click="changeTitle">Change Title</button>
</template>
<script setup>
import { setTitle } from "./plugins/Vue3Head";
const changeTitle = () => {
  setTitle("New Title");
};
</script>

Props

| Name | Type | Default | Description | | --- | --- | --- | --- | | title | String | | The title of the page | | meta | Array | | An array of meta tags | | link | Array | | An array of link tags | | og | Array | | An array of Open Graph tags | | twitter | Array | | An array of Twitter tags | | styles | Array | | An array of style tags | | scripts | Array | | An array of script tags |

License

MIT

Resources

Software Tips