Dynamic Subdomain Routing With NextJS
Asked Answered
O

2

7

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.

Obligato answered 26/6, 2020 at 8:22 Comment(4)
I dont see how next.js can be related to automatic change or add a CNAME record.Jurkoic
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.Obligato
Yes i know, but next.js is not meant to do such thingsJurkoic
@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.Obligato
G
7

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!

Gondola answered 19/10, 2020 at 12:56 Comment(1)
Is there no other way of doing this? I am thing of writing a custom server.Sexpot
C
5

I just found this blog post https://demo.vercel.pub/platforms-starter-kit, which was published 6 days ago.

It announces https://platformize.co/, a product that does just that.

Multi-tenant applications serve multiple customers across different subdomains/custom domains with a single unified codebase.

For example, this blog is a multi-tenant application:

  • Subdomain: demo.vercel.pub
  • Custom domain: platformize.co (maps to demo.vercel.pub)
  • Build your own: app.vercel.pub

Another example is Hashnode, a popular blogging platform. Each writer has their own unique .hashnode.dev subdomain for their blog:

  • eda.hashnode.dev
  • katycodesstuff.hashnode.dev
  • pit.hashnode.dev

Users can also map custom domains to their .hashnode.dev subdomain:

catalins.tech → pit.hashnode.dev

Coitus answered 26/1, 2022 at 15:4 Comment(1)
This is their official recommendation nowShoop

© 2022 - 2024 — McMap. All rights reserved.