I have a website where I can host different shop profiles and currently I'm generating the route with the help of shop name's slug, but instead of https://example.com/shop/abc-shop I wanted the route to be https://abc-shop.example.com/ and upon looking up on the internet I can't find a way to do it with Vue3 router. Please suggest some approaches.
2 Answers
Old, but I do something like this with a similar need. (blogs)
const router = createRouter({
history: createWebHistory(),
routes,
})
router.beforeEach((to, from, next) => {
let subdomain = window.location.host.split('.')[0]
console.log(subdomain)
if (subdomain != 'www' && subdomain != 'localhost:3000') {
to.params['subdomain']= subdomain
}
next()
})
then you can handle it in your template , for me I use a custom component called blogRouterView that contains specific paths and then parses this.$route to know what component to include using async components. (<component :is="currentURLComponent"></component>)
Comments
Like Estus Flask wrote in comments subdomain is a completly diferent server. Browser treat it like it is separate www page. You need to write for this subdomain another vue project and in root domain redirect to this subdomain. Or you can prepere sub domain to be able to use in for example <iframe> tag.
location.https://example.com/shop/abc-shopbut I want to view that onhttps://abc-shop.example.com/