Search code examples
node.jsreactjsnext.jssubdomain

How to assign Subdomain for every user in Next Js


Can any one guide me how to create a subdomain for every user in NEXT JS. If a user is registered with a username 'john' and the website domain is www.example.com, Then I want to show the profile details of the user at john.example.com.


Solution

  • I'm working on a similar application. I don't have the exact answer, I just want to give you a good lead, so you can Google it if you're stuck.

    What I know is, that this subdomain you want for each user (john.example.com) is called "multi-tenancy".

    Check out these links:

    If you want to simulate the subdomain behavior on localhost, then you have to edit your /etc/hosts file (Mac) - link and for Windows, it is inside the System folder I guess (please google it).

    (Below point is not related to the question)

    One more thing I would like to share is -- in the case your application has auth, then you have to be careful with the cookies. In my case, I have to maintain the user session on both, the main domain and subdomain. So my cookie domain name is ".localhost" or ".exmaple". Please check the (.) dot in front of localhost.

    That's all that I know.