Comments (7)
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.
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.
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.
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.
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.
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.
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:
from vue-router.
Related Issues (20)
- Vue 2.7 useRouter & Storybook HOT 1
- In navigation guards, it is possible to distinguish whether push triggers navigation or replace triggers navigation?
- 后面异步append加入的script没有插入iframe的head HOT 1
- vue-router is very nice HOT 1
- npm 和yarn 安装vue-router,useRoute 数据不一致 HOT 1
- 在微前端qiankun为基座的情况下,手动修改浏览器地址栏,两个子应用间设置的createWebHashHistory base会互相叠加 HOT 3
- Use dynamic routing prompts Uncaught Error: [vue-router] "path" is required in a route configuration HOT 1
- requested module '/node_modules/.vite/deps/vue-router.js?v=85d94ed7' does not provide an export named 'default' HOT 1
- Illegal character leads to lost all query data in "history" mode
- The history.state object lost certain properties because they were overwritten by the pushState function. HOT 1
- 两层iframe嵌套,在hash模式下,第二层iframe不渲染 HOT 2
- Page refreshing after replacing the current route ( Component getting unmounted and mounted again) HOT 2
- watch router or router.currentRoute.path question HOT 2
- 4.1.5版本以上,单个路由配置beforeEnter 无效 HOT 1
- Detect when the user changes the URL on the address bar by manually typing it HOT 1
- check path 404 HOT 1
- In Vue vite, router.push() does not render page although it does navigate to a new URL HOT 1
- The type of query is wrong and may be undefined. HOT 1
- Misfiled issue. Ignore/delete HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-router.