Giter Site home page Giter Site logo

Comments (7)

posva avatar posva commented on May 24, 2024

Hi, thanks for your interest but Github issues are for bug reports and feature requests only. You can ask questions on the forum, the Discord server or StackOverflow.

from vue-router.

chrisspiegl avatar chrisspiegl commented on May 24, 2024

Dear @posva, I actually consider this a Feature request if not present at this time.

Having the a canonical url path (as there is a default path anyways) could be very helpful for this specific purpose.

If that is not something planned for Vue-Router, I am going to leave this be.

from vue-router.

posva avatar posva commented on May 24, 2024

Having out-of-the-box canonical paths is not planned because it goes beyond the scope of Vue Router (even if that might sound weird). I think you should use https://github.com/vueuse/head to add the head tags based on the current location. You can use the meta fields to define extra properties or use router.resolve() with the name and params of a route to always get the default path version 😉 (this implies you name all your routes that have aliases)

from vue-router.

chrisspiegl avatar chrisspiegl commented on May 24, 2024

Thank you for your response, @posva. This is understandable and I see the reasoning. I will look into alternative solutions with vueuse/head 👍.

from vue-router.

chrisspiegl avatar chrisspiegl commented on May 24, 2024

For anyone stumbling upon this issue in the future. I was able to implement this via this snippet inside my app.vue which also sets the header.

It resolves the path based on the current route name if it has one and falls back to just rendering the path of the route that was loaded if there is no name given.

const router = useRouter()
const route = useRoute()
const runtimeConfig = useRuntimeConfig()
const canonical = `${runtimeConfig.public.siteUrl}${router.resolve(route.name ? { name: route.name } : route).path}`
useServerHead({
    link: [
      { rel: 'canonical', href: canonical },
    ],
  })

from vue-router.

posva avatar posva commented on May 24, 2024

BTW I think you can pass a getter to useHead() so it automatically updates:

const router = useRouter()
const route = useRoute()
const runtimeConfig = useRuntimeConfig()
useHead({
  link: () => [
    {
      rel: 'canonical',
      href: `${runtimeConfig.public.siteUrl}${router.resolve(route.name ? { name: route.name } : route).path}`,
    },
  ],
})

Although this might not be really useful in client side. Which makes me think using useHeadServer() is probably enough

from vue-router.

chrisspiegl avatar chrisspiegl commented on May 24, 2024

Thank you @posva for the added ideas. I have revised my code snippet above to use useServerHead() (useHeadServer() was vueuse/head but it is being sunset in favor of unhead) since you are correct with the assumption that the Canonical is only really relevant on the first call by a search engine.

For anyone trying to find a solution, this is the code snippet I have implemented:

#3869 (comment)

from vue-router.

Related Issues (20)

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.