Hi everybody. Our first vuejs project next to end. But I faced a problem with routing subdomains. Users can use their user names as subdomain to show their contact informations for example : http://kamuransonecek.exlample.com
how can I detect username from subdomain to routing the site pages?
'http://:userName.example.com': {
component: require('./views/userinfo')
},
This codes not work as I espected. I wait for your helps. Thanks
If this is posible I'll be very surprised.
+1
You shouldn't need vue-router to get this. Just use window.location.host and use regex to extract it yourself.
My solution is this :
'/': {
component: function() {
var reg = new RegExp("www|sitename|test|localhost:8000");
var parts = window.location.host.split(".");
return reg.test(parts[0]) ? require('./views/home') : require('./views/subdomain');
}()
},
I hope it will be helpful for others.
What about SSR? How to get domain name on server?
For SSR see https://github.com/nuxt/nuxt.js/issues/2378
@zolotov88 if you are using ssr you will only want use window when it is available and if you are using express use the 'req' param of your middleware to get the subdomain on first request and the use that to match routes with.
in nuxt with dev server I'm getting window is not defined even if I'm using !process.server. How to handle that?
Most helpful comment
My solution is this :
I hope it will be helpful for others.