Git Product home page Git Product logo

nuxt-canonical-ogurl's Introduction

nuxt-canonical-ogurl

A Nuxt module to automatically generate canonical and og:url meta tags

Deprecation Notice

This module is deprecated because it only works with Nuxt 2. On Nuxt 3, this module is not required to automatically add canonical and og:url.

To automatically add canonical and og:url, try following code in app.vue:

<template>
  <div>
    <NuxtLayout>
      <NuxtPage/>
    </NuxtLayout>
  </div>
</template>

<script setup>
const route = useRoute();

const canonical = `https://example.com${route.path.replace(/\/$/, "")}`; // without trailing slash
const canonicalWithSlash = `https://example.com${route.path.replace(/\/$/, "")}/`; // with trailing slash

useHead({
  meta: [
    { name: "og:url", content: canonical },
  ],
  link: [
    { rel: "canonical", href: canonical },
  ],
});
</script>

Set up

  1. Add nuxt-canonical-ogurl to the dependency in your project
$ npm install nuxt-canonical-ogurl

or

$ yarn add nuxt-canonical-ogurl
  1. Add nuxt-canonical-ogurl to buildModules in nuxt.config.js, then configure options
export default {
  // ...
  buildModules: [
    "nuxt-canonical-ogurl",
  ],
  canonicalOgUrl: {
    baseURL: "https://example.com",
  },
  // You need to set router.trailingSlash true or false to use this module
  router: {
    trailingSlash: true,
  },
}

To complete set up nuxt-canonical-ogurl, you need to set router.trailingSlash. If you set router.trailingSlash: true, trailing slashes are added to the canonical URLs. (e.g. https://example.com/foo/) If you set router.trailingSlash: false, trailing slashes are stripped from the canonical URLs. (e.g. https://example.com/foo) If you don't set router.trailingSlash, nuxt-canonical-ogurl raises an error.

Options

export default {
  // ...
  canonicalOgUrl: {
    baseURL: "https://example.com",
  },
}

baseURL

type: String

Base URL for canonical tags. If you set baseURL: "https://example.com", canonical URL for pages/foo.vue would be "https://example.com/foo/" (in case router.trailingSlash: true).

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.