Wildcard Subdomains rewrite to paths (or how to create a Subdomain SaaS in one NextJS Project) #27889
Unanswered
fairhat
asked this question in
Show and tell
Replies: 1 comment
-
Awesome post! p.s. {
has: [
{
type: 'host',
value: '(?<series>.+)\\.localhost',
}
],
source: '/:locale/user/:path*',
destination: '/:locale/series/:series/user/:path*',
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been researching the whole internet on how to solve this problem.... Here you go.
Let's say you have a wonderful saas and bought a domain on vercel: mysupersaas.com
And now you want to enable Wildcard subdomains for your customers: {user}.mysupersaas.com
But you don't want to manage a separate project for every customer nor lose SSG just to get the host header!
Well, try this then:
Obviously the localhost part is just for dev environments so change it according to your needs.
Now you can create pages in /pages/user/[user] and subpages in that folder and they will be matched with the users!
To get your specific username and keep static optimization you obv. have to provide getStaticProps and getStaticPaths functions like this:
Now your users can see their spaces at {user}.mysupersaas.com (also mysupersaas.com/user/{user}, but who wants that anyways).
Also you can still use mysupersaas.com/whatever/route/you/want
Just make sure that your root pages/index.tsx is now called actual-index.tsx
Also note that for whatever reason any route for your main site (mysupersaas.com) is accessible on subdomains too, eg:
mysupersaas.com/pricing leads to the same page as userx.mysupersaas.com/pricing.
So make sure they dont conflict and hope that your end-users wont accidentally use these urls >.<
Not sure how this behaves on Vercel hosted nextjs projects, but i'm using a VPS to host my nextjs instance and with a reverse-proxy setup (nginx/ i'm using caddy) it seems to work at least.
BRB continuing to build my actual software after fighting this for days
BTW: i18n works too! (as long as you're not using domain locales)
Beta Was this translation helpful? Give feedback.
All reactions