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

@m-media/vue3-meta-tags

v0.0.7

Published

Add support for meta tags in your Vue3 Router

Downloads

471

Readme

Vue3 Router Meta Tags

Automatically handle meta tags in your Vue3 Router.

Table of Contents

Installation

npm i @m-media/vue3-meta-tags

Usage

First, import the plugin and install it in your Vue app:

// main.js
import { metaTagPlugin } from "@m-media/vue3-meta-tags";

app.use(
  metaTagPlugin,
  {
    /* optional configuration here */
  },
  router
);

The third parameter is the router instance. The second parameter is an optional configuration object.

The package will automatically set some defaults on every page, but you can set your own meta attributes in a route too:

{
      path: "/",
      name: "home",
      meta: {
        title: "Home",
        description: "This is the home page",
        image: "https://example.com/image.png",
        locale: "en_US",
        metaTags:
          {
            "og:title": "Home",
            ... // other meta tags
          },
      },
      component: Home,
},

Configuration

The full configuration object that can be passed looks like this:

app.use(
  metaTagPlugin,
  {
    defaultName: import.meta.env.VITE_APP_NAME,
    defaultLocale: i18n.global.locale.value,
    locales: SUPPORT_LOCALES,
    preconnect: [import.meta.env.VITE_API_URL, "https://js.stripe.com"],
    textCallback: (text: string) => {
      return i18n.global.t(text);
    },
  },
  router
);

defaultName

The default name of your app. This will be used if no name/title is specified in the meta tags, and also for some tags like og:site_name.

defaultLocale

The default locale of your app. This sets the lang attribute of the html tag, as well as some other tags like og:locale. If the set locale is a right-to-left language, the dir attribute of the html tag will be set to rtl, otherwise its set to ltr.

locales

An array of locales that your app supports. This is used to generate the hreflang meta tags.

preconnect

An array of URLs that you want to preconnect to. This is used to generate the link tags with rel="preconnect".

textCallback

A callback function that is used to translate the text in the meta tags. This is useful if you are using a library like Vue I18n.

Manually setting meta tags

You can run

import { setMetaAttributes } from "@m-media/vue3-meta-tags";

const to = router.currentRoute.value;
const from = router.currentRoute.value; // or whatever route you want to set the meta tags for

// This will set the meta tags based on the route
setMetaAttributes(to, from);

Updating the current locale

import { setLocaleToUse } from "@m-media/vue3-meta-tags";

setLocaleToUse("en");

Setting your own JSON schema

You can set your own JSON schema for the page using the jsonSchema function.

import { updateOrCreateSchema } from "@m-media/vue3-meta-tags";

const structuredData = {
  "@context": "https://schema.org/",
  "@type": "Dataset",
  name: Maps.map.value?.title ?? "Untitled map",
  description: Maps.map.value?.description,
  url: window.location.href,
  license: "https://creativecommons.org/publicdomain/zero/1.0/",
  isAccessibleForFree: true,
  creator: {
    "@type": "Organization",
    url: "https://app.cartes.io/",
    name: "Cartes.io",
  },
  distribution: [
    {
      "@type": "DataDownload",
      encodingFormat: "JSON",
      contentUrl:
        import.meta.env.VITE_API_URL + "/api/maps/" + Maps.map.value?.uuid,
    },
  ],
  temporalCoverage: Maps.map.value?.created_at + "/..",
};

updateOrCreateSchema(structuredData);

Set a custom title

You can set a custom title for the page using the setTitle function.

import { setTitle } from "@m-media/vue3-meta-tags";

setTitle("My custom title");

Note that this function does not run through the textCallback function, so you should translate the text yourself.

Set a custom description

You can set a custom description for the page using the setDescription function.

import { setDescription } from "@m-media/vue3-meta-tags";

setDescription("My custom description");

Note that this function does not run through the textCallback function, so you should translate the text yourself.

Set follow and index

You can set the robots meta tag using the setFollowAndIndex function.

import { setFollow } from "@m-media/vue3-meta-tags";

setFollow(true);

Skipping auto meta tag resolution

You can skip the auto meta tag resolution by setting the skipMetaTagsHandler meta tag to true:

{
      path: "/",
      name: "home",
      meta: {
        skipMetaTagsHandler: true,
      },
      component: Home,
},

You should then manually set the meta tags using the setMetaAttributes function.