2

I'm building an app where different users can register and get a frontpage. I then assign each user a subdomain (say userA.mydomain.com) and would like to display the user's frontpage when we access his subdomain.

I already got it working that the different subdomains route to the NextJS app, but now I need to display the user's data on that frontpage. So when we go to userA.mydomain.com I need to display userA's data.

I know that I can generate the static pages for each user by creating a page under /pages/frontpages/[userId].tsx, but then the domain would look like this: userA.mydomain.com/frontpages/userA. This is redundant, because we already know the userId because of the subdomain. Also we can access other user's frontpages from another user's subdomain.

I also know that I can fetch the data inside the index page, but then we loose all benefits of statically generated pages.


My questions are:

  1. Is there a way to get what we would have with pages/[userId].tsx but for the index page? I don't want to fetch the data inside the index page itself because then I loose the benefit of statically generated pages. Maybe there's something that we can do with getStaticPaths?

  2. For example is it possible to prerender the frontpages using getStaticPaths for pages/frontpages/[userId].tsx and then inside the index page we just render the content of the generated frontpage page (based on the subdomain of the url)?

Gumbee
  • 53
  • 1
  • 5
  • What would be the desired URL for a user's frontpage? Simply `userA.mydomain.com/`? How would Next.js know which user's data to render in that case, since it doesn't know about the subdomain? – juliomalves Jan 15 '21 at 11:03
  • Yes, the frontpage would be at `userA.mydomain.com/`. Is there no way for NextJS to know about the subdomain when the request is made and then serve based on this subdomain? – Gumbee Jan 15 '21 at 17:36

0 Answers0