2

How can the Dynamic Sub domain routing feature be implementing in NextJS?

Example: If a user comes with username abc in site xyz then he can access his site on abc.xyz.com

Also, if the user have abc.com domain then he can point abc.com to abc.xyz.com So in future if someone opens abc.com then abc.xyz.com is served. And in URL also the abc.com is shown.

I have investigated few plugin in NPM like vhost and wildcard-subdomains but not sure that is right way to take on this issue.

The vhost requires changes in system hosts in local system and wildcard-subdomain solves the issue purely with routing.

The Local System Setting I have customized Server.js With Code Which Works Temporarily, but does't seems to be a solution which can be used in production :

Server.js

  ...
    if (pathname === "/demo.demo.com") {
          app.render(req, res, "/demo.demo.com", query);
    }
    ...

And in _app.js

static async getInitialProps(appArgument) {
   ...
    return {
      ...
      renderFrom: "demo.demo.com"
    };
  }

Also in my host I have demo.demo.com point to localhost.

The site works for me in demo.demo.com:3000 but how to generalise it in production scenarios with Database and CNAME Records and add/change CNAME Record automatically with User Action.

Ajay
  • 121
  • 1
  • 10
  • I dont see how next.js can be related to automatic change or add a CNAME record. – Nico Jun 26 '20 at 11:55
  • To perform the above solution, we need to have a host added to local system. The equivalent of host in local System in Internet is CNAME record. – Ajay Jun 26 '20 at 12:00
  • Yes i know, but next.js is not meant to do such things – Nico Jun 26 '20 at 12:06
  • @Nico: Are you referring that Dynamic Subdomain Routing can not be done with NextJS as I am using NextJS for my application and this is the requirement. The Solution works but how to make it generic with DB and Server Properly. – Ajay Jun 26 '20 at 13:27

1 Answers1

2

On Vercel (the creators of Next.js), we support Wildcard Domains out of the box. Within Next.js, you then only need to read the Domain from the headers of the incoming request, parse it and then respond with the right content.

I hope that was helpful!